# -*- coding: utf-8 -*-
import logging, mv_common
import os, re
def zoneminder_setup(systemconfig):
    logging.info("____Start of mythzoneminder config____")
    mv_root = systemconfig.get("MVROOT")
    if ( systemconfig.get("SystemType") == "Standalone" or
         systemconfig.get("SystemType") == "Master_backend" ):
             logging.info("    Adding mythzmserver" )
             #add zoneminder
             mv_common.pacinstall("zoneminder")
             mv_common.add_service("zoneminder")
             mv_common.add_service("mythzmserver")

    #now to add the settings.
    cmd = '''%s/bin/myth_settings_wrapper.sh -c ZMCONFIG -h %s -a %s'''%(
                                                   mv_root,
                                                   systemconfig.get("hostname"),
                                                   systemconfig.get("dbhost"))
    mv_common.runcmd(cmd)
    logging.info("____End of mythzoneminder config____")





def setup_software(systemconfig, data_config):
    if  mv_common.read_config(mv_common.module_config,"software")  == False  :
        logging.info("____Skipping of software, config disabled____")
        return

    logging.info("____Start of Software install____")
    postfix=''
    logging.info("    Refreshing pacman package list from server")
    cmd="pacman -Syy"
    mv_common.runcmd(cmd)
    if data_config.SYSTEMTYPE == "MythVantage":
      #look for the installed prefix
      cmd="pacman -Q mythtv-release-fixes"
      rc = mv_common.runcmd(cmd)
      if rc == 0:
        postfix = "-release-fixes"
      else:
        postfix = "-svn"
      #This section is for MythVantage OS, not LINHES
      default_disabled = ("mytharchive", "mythbrowser", "mythnews",
                          "mythgame",  "mythweather", "mythzoneminder","mythnetvision" )

      default_installed=("mythgallery", "mythmusic")
      other_pkg=("miro", "mednafen", "romdb", "xine", "dvdcss", "webmin" , "fuppes", "foldingathome", "mythappletrailers", "mythstream", "mupen64")
    else:
      #this is linhes section
      default_disabled = ("mytharchive",
                          "mythbrowser",
                          "mythnews",
                          "mythgame",
                          "mythweather",
                          "mythzoneminder",
                          "mythnetvision" )

      default_installed=("mythgallery",
                         "mythmusic")
      other_pkg=("romdb",
                 "snes9x",
                 "fceux",
                 "mame",
                 "mednafen",
                 "mupen64",
                 "dolphinemu",
                 "xe",
                 "mythappletrailers",
                 "webmin",
                 "webonlinhes",
                 "huludesktop",
                 "plexhometheater",
                 "plexmediaserver",
                 "kodi",
                 "dvdcss",
                 "foldingathome")


    for pkg in default_disabled:
        pkgname=pkg+postfix
        try:
            if systemconfig[pkg] == "1":
                mv_common.pacinstall(pkgname)
                if pkgname == "mythzoneminder":
                    zoneminder_setup(systemconfig)
            else:
                mv_common.pacremove(pkgname)
                if pkgname == "mythzoneminder":
                    #should be a conditional check on system type
                    #but not worth the effort.
                    mv_common.remove_service("mythzmserver")
        except:
            logging.debug("  ERROR-- %s is not defined", pkg)

    for pkg in default_installed:
        pkgname=pkg+postfix
        try:
            if systemconfig[pkg] == "0":
                mv_common.pacremove(pkgname)
            else:
                mv_common.pacinstall(pkgname)
        except:
            logging.debug("  ERROR-- %s is not defined", pkg)

    for pkg in other_pkg:
        try:
            if systemconfig[pkg] == "1":
                mv_common.pacinstall(pkg)
            elif systemconfig[pkg] == "0":
                mv_common.pacremove(pkg)
        except:
            logging.debug("   ERROR-- %s is not defined", pkg)
    logging.info("__End Software\n ")
