#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
import subprocess
from time import sleep


default_server = None  # 加域的服务器地址
default_username = None  # 加域账户名
default_password = None  # 加域账户密码
default_client_name = None  # 客户端主机名

# default_server = 'server.example.local'  # 加域的服务器地址
# default_client_name = 'client.example.local'  # 客户端主机名

# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------

default_password_encryption = False  # 当该值为True时,脚本会对账户密码进行解码操作
default_is_reboot_machine = False  # 当该值为True时,仅当加域成功后,重启机器


#在加域前需要额外进行的操作，例如自动生成主机名等,由于部分调用方会解析标准输入信息，请避免提供的版本在下面函数中print中间信息
def preinstall():
    pass


#在加域后需要额外进行的操作，例如重启服务等,由于部分调用方会解析标准输入信息，请避免提供的版本在下面函数中print中间信息
def postinstall():
    pass


# ---------------------------------------------------------------------------------------------
# ---------------------------------以下代码非必要不要修改------------------------------------------
# ---------------------------------------------------------------------------------------------

short_command = None

if len(sys.argv) == 2:
    if sys.argv[1] in ('install', 'uninstall'):
        short_command = sys.argv[1]

args = sys.argv[1:]

full_args = []
full_args.append('kim-joinc')

if short_command is None:
    full_args += args
else:
    if short_command == 'install':
        preinstall()
    full_args.append(short_command)
    if default_server:
        full_args.append('--server')
        full_args.append(default_server)
    if default_username:
        full_args.append('--username')
        full_args.append(default_username)
    if default_password:
        full_args.append('--password')
        full_args.append(default_password)
    if default_password_encryption:
        full_args.append('--password-encryption')
    if short_command == 'install':
        if default_client_name:
            full_args.append('--client-name')
            full_args.append(default_client_name)
    #if default_is_reboot_machine:
    #    full_args.append('--is-reboot-machine')

#print(full_args)
#result = subprocess.run(full_args, capture_output=True,text=True)
result = subprocess.Popen(full_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
#if result.stdout:
#    print(result.stdout)
#if result.stderr:
#    print(result.stderr)
while True:
    out_line = result.stdout.readline()
    if out_line:
        print(out_line.strip())
    err_line = result.stderr.readline()
    if err_line:
        print(err_line.strip(), file=sys.stderr)
    if result.poll() is not None and not out_line and not err_line:
        break
if result.returncode == 0 and short_command == 'install':
    postinstall()
if result.returncode == 0 and default_is_reboot_machine and short_command is not None:
    try:
        print("\033[033m系统将在十秒后重启, 按\033[031mCtrl + C\033[0m \033[033m取消.\033[0m")
        sleep(10)
        os.system("reboot")
    except KeyboardInterrupt:
        exit()
exit(result.returncode)
