# -*- coding: utf-8 -*-
import logging, mv_common
import os, re

config_file = "mv_config"

def setupvnc_system(systemconfig,data_config):
    logging.info("____Start of vnc setup____")
    mv_common.pacinstall("tigervnc")
    vncdir=data_config.VNCHOME+"/.vnc"
    try:
        os.makedirs(vncdir)
        os.chown(vncdir,78,78)
    except:
        logging.info("    couldn't create vnc dir:%s" %vncdir)

    vncfile="%s/xstartup" %vncdir
    file_contents='''
#!/bin/bash
xhost + localhost
xhost + 127.0.0.1
xhost + si:localuser:root
vncconfig -iconic &
if [ ! -e ~GNUstep ]
then
    wmaker.inst
    cp -f /usr/share/wm_data/GNUstep/Defaults/WindowMaker ~/GNUstep/Defaults/
    cp -f /usr/share/wm_data/GNUstep/Defaults/WMState.vnc ~/GNUstep/Defaults/WMState
    cp -f /usr/share/wm_data/GNUstep/Defaults/WMWindowAttributes ~/GNUstep/Defaults/
    cp -f /usr/share/wm_data/GNUstep/Defaults/WMRootMenu ~/GNUstep/Defaults/
fi
exec wmaker
'''
    try:
        logging.debug("    Writing  %s",vncfile)
        f= open(vncfile,'w')
        f.write(file_contents)
        f.close()
        os.chmod(vncfile,0755)

    except:
        logging.debug("    Couldn't open %s",vncfile)
        logging.debug("    Aborting vnc...")

    try:
        vncuid = pwd.getpwnam('vncsvc')[2]
        vncgid = pwd.getpwnam('vncsvc')[3]
    except:
        logging.critical("*    vncsvc not found")
        vncuid = '78'
        vncgid = '78'


    cmd="setfacl -m u:vncsvc:x %s" %data_config.MYTHHOME
    mv_common.runcmd(cmd)

    srcmyth="%s/.mythtv" %data_config.MYTHHOME
    destmyth="%s/.mythtv" %data_config.VNCHOME

    mv_common.link_file(srcmyth,destmyth)



    #mv_common.mkdir_mythhome(data_config.VNCHOME,vncuid,vncgid)
    #home_xml_file=data_config.VNCHOME + "/.mythtv/config.xml"
    #configxml_file="/usr/share/mythtv/config.xml"
    #mv_common.link_file(configxml_file,home_xml_file)

    logging.info("    Writing out password")
    vncpassfile="%s/passwd" %vncdir
    cmd="echo %s|vncpasswd -f > %s" %(systemconfig.get("vncpassword"),vncpassfile)
    mv_common.runcmd(cmd)
    cmd="chmod 700 %s" %vncpassfile
    mv_common.runcmd(cmd)
    cmd="chown vncsvc %s" %vncpassfile
    mv_common.runcmd(cmd)



    logging.info("__End of vnc \n")



def setupvnc(systemconfig,data_config):
    logging.info("____Start of vnc config ____")
    if  mv_common.read_config(mv_common.module_config,"vnc")  == False  :
        logging.info("____Skipping of vnc, config disabled____")
        return
    #vncservice
    try:
        vnc=systemconfig.get("vncenable")
    except:
        vnc = "0"

    if vnc == "1":
        logging.info("     Installing vnc system\n")
        setupvnc_system(systemconfig,data_config)
        mv_common.add_service("vnc")
    else:
        mv_common.remove_service("vnc")
        mv_common.pacremove("tigervnc")
    #x11vnc
    try:
        xvnc=systemconfig.get("xvncenable")
        xvncpasswd=systemconfig.get("xvncpassword")
    except:
        xvnc = "0"
        xvncpassword="LinHES"

    if xvnc == "1":
        logging.info("     Installing x11vnc system\n")
        mv_common.pacinstall("x11vnc")
        mv_common.add_service("xvnc")
    else:
        mv_common.remove_service("xvnc")
        mv_common.pacremove("x11vnc")


    logging.info("__End of vnc\n")
