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

def setup_web_auth(UseMythWEB_auth):
    if  mv_common.read_config(mv_common.module_config,"webuser")  == False  :
        logging.info("____Skipping of webuser, config disabled____")
        return

    logging.info("____Start of setup_web_auth____")
    include_file="/etc/gen_light_conf.d/auth.conf"
    if UseMythWEB_auth == str(1):
        logging.info("    Enabling authorization for mythweb")
        try:
            fo = open(include_file, "w")
            out_string='''include "/etc/lighttpd/auth-inc.conf"
'''
            fo.write(out_string)
            fo.close()
        except:
            logging.info("    Couldn't open auth include file")

    else:
        #disable auth
        logging.info("    Disabling authorization for mythweb")
        cmd="rm -f %s" %include_file
        mv_common.runcmd(cmd)
    logging.debug("Running gen_light_include.py")
    cmd="/usr/LH/bin/gen_light_include.py"
    mv_common.runcmd(cmd)
    logging.debug("Restarting lighttpd")
    cmd="sudo sv restart lighttpd"
    mv_common.runcmd(cmd)
    logging.info("__End of webauth\n")

