# -*- coding: utf-8 -*-
import logging, mv_common
import os, re
from urllib2 import Request, urlopen, URLError, HTTPError

config_file = "mv_config"
data_config = __import__(config_file, globals(), locals(), [])

def setup_zip(MVROOT,zipcode):
    logging.debug("____Setting up the zipcode____")

    if zipcode:
        cmd="%s/bin/restore_default_settings.sh -c ZIP -c %s" %(MVROOT,zipcode)
        mv_common.runcmd(cmd)
        #Let's also speed things up for those in North America
        cmd="mkdir /usr/bin/perlbin/vendor/tv_grabbers_non_na"
        mv_common.runcmd(cmd)
        cmd="mv  /usr/bin/perlbin/vendor/tv_grab_*  /usr/bin/perlbin/vendor/tv_grabbers_non_na"
        mv_common.runcmd(cmd)
        cmd="mv  /usr/bin/perlbin/vendor/tv_grabbers_non_na/tv_grab_na*  /usr/bin/perlbin/vendor/"
        mv_common.runcmd(cmd)
    else:
        logging.debug("    Zipcode is not set")

    logging.debug("__End of zipcode\n")

def setup_tz(timezone,TEMPLATES):
    logging.info("____Setting up the timezone____")

    if not timezone:
        timezone="unknown"
    logging.info("    Setting timezone to %s",timezone)
    mv_common.remove_file("/etc/localtime")
    #try:
        #logging.debug("    Removing /etc/localtime")
        #os.remove("/etc/localtime")
    #except:
        #logging.debug("    Couldn't remove /etc/localtime")
        #pass

    srclink="/usr/share/zoneinfo/%s" %timezone
    logging.debug("    symlinking %s to /etc/localtime",srclink)
    try:
        os.symlink(srclink,"/etc/localtime")
    except:
        logging.critical("    Couldn't make symlink for /etc/localtime")

    #cmd = '''sed -e "s/^TIMEZONE=.*$/TIMEZONE=\\"%s\\" /" /etc/rc.conf > $TEMPLATES/rc.conf''' %re.escape(timezone)
    #mv_common.runcmd(cmd)

    if os.path.exists("/etc/php/php.ini"):
        logging.info("    Changing timezone for php")
        cmd = '''sed -i "s/^.*date.timezone.*$/date.timezone=%s/" ${BASE}/etc/php/php.ini''' %re.escape(timezone)
        mv_common.runcmd(cmd)

    #mv_common.cp_and_log(TEMPLATES+"/rc.conf","/etc/rc.conf")
    logging.info("__End of timezone\n")



def setup_sleep(systemconfig):
    logging.debug("____Setting up sleep____")
    autoshutdown = systemconfig.get("AutoShutdown")
    stime1 = systemconfig.get("Shutdowntime")
    stime2 = systemconfig.get("Shutdowntime2")
    cstime1=''

    if autoshutdown == "1" :
        if  not stime1 == "-1" :
            cstime1 = stime1
            cshutdown = cstime1
        if  not stime2 == "-1" :
            if cstime1 :
                cshutdown = "%s,%s" %(cstime1,stime2)
            else:
                cshutdown = stime2
        logging.debug("    Shutdown time at %s",cshutdown)
        cmd='''sed -e "s/HOUR/%s/g" %s/cron.template | crontab - -u mythtv''' %(cshutdown,systemconfig.get("TEMPLATES"))
        mv_common.runcmd(cmd)
    else:
        logging.info("    Shutdown not enabled")
        cmd='''sed -e "/00 HOUR.*/d" %s/cron.template | crontab - -u mythtv''' %systemconfig.get("TEMPLATES")
        mv_common.runcmd(cmd)

    logging.debug("__End of  sleep\n")


