#!/usr/bin/python3

import array
import fcntl
import os
import sys
import termios

TIOCL_SETKMSGREDIRECT = 11

if len(sys.argv) != 2:
    sys.stderr.write(sys.argv[0] + " <VT number>\n")
    sys.exit(1)

try:
    vt_num = int(sys.argv[1])
except Exception as e:
    sys.stderr.write("Error: " + str(e) + "\n")
    sys.exit(1)

try:
    fd = os.open("/dev/tty0", os.O_RDWR)

    buf = array.array("B", [TIOCL_SETKMSGREDIRECT, vt_num])
    fcntl.ioctl(fd, termios.TIOCLINUX, buf, True)

    os.close(fd)

except OSError as e:
    sys.stderr.write("Error: " + str(e) + "\n")
