import subprocess
import crypt
import os
from os.path import expanduser

from .print_utils import cin, cout, OKGREEN, WARNING, FAIL

from vmmaster import package_dir
from .system_utils import run_command
from .utils import change_user_vmmaster


def files(path):
    for path, subdirs, filenames in os.walk(path):
        for filename in filenames:
            yield os.path.join(path, filename)


def useradd(home):
    password = 'vmmaster'
    encrypted_password = crypt.crypt(password, "22")
    shell = '/bin/bash'
    group = 'libvirtd'
    user_add = subprocess.Popen(
        ["sudo", "useradd",
         "--create-home", "--home-dir=%s" % home,
         "--groups=%s" % group,
         "--shell=%s" % shell,
         "-p", encrypted_password,
         "vmmaster"], stdin=subprocess.PIPE
    )
    output, err = user_add.communicate()
    if err:
        cout(repr(err), color=FAIL)
        exit(1)


def copy_files_to_home(home):
    copy = ["/bin/cp", "-r", package_dir() + "home" + os.sep + ".", home]
    return_code, output = run_command(copy)
    change_user_vmmaster()
    if return_code != 0:
        cout("\nFailed to copy files to home dir: %s\n" % home_dir(), color=FAIL)
        exit(output)


def home_dir():
    user_path = "~%s" % "vmmaster"
    home = expanduser(user_path)
    if user_path == home:
        return None
    return home


def init():
    home = '/var/lib/vmmaster'
    cout("Please input absolute path to home directory for 'vmmaster'\n")
    cout("[default:%s]:" % home, color=WARNING)
    abspath = cin()
    abspath = abspath.strip()
    if abspath:
        home = abspath

    useradd(home)
    copy_files_to_home(home)
    cout("\nvmmaster successfully inited in %s\n" % home_dir(), color=OKGREEN)

@@ -1054,35 +1054,8 @@
 py)%0A
-    change_user_vmmaster()%0A
     
@@ -1178,16 +1178,270 @@
 output)%0A
+    chown = %5B%22/bin/chown%22, %22-R%22, %22vmmaster:vmmaster%22, home%5D%0A    return_code, output = run_command(chown)%0A    if return_code != 0:%0A        cout(%22%5CnFailed to change owner for: %25s%5Cn%22 %25 home_dir(), color=FAIL)%0A        exit(output)%0A    change_user_vmmaster()%0A
 %0A%0Adef ho

Set vmmaster`s home dir owner to vmmaster