#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# «mythbuntu-lircrc-generator» - Mythbuntu Lirc Generator
#
# Copyright (C) 2007, Mario Limonciello, for Mythbuntu
#
#
# Mythbuntu is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this application; if not, write to the Free Software Foundation, Inc., 51
# Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
##################################################################################

import sys
import os
import getopt
import shutil

from MythbuntuLircGenerator.mythtvhandler import MythTVHandler
from MythbuntuLircGenerator.mplayerhandler import MPlayerHandler
from MythbuntuLircGenerator.xinehandler import XineHandler
from MythbuntuLircGenerator.vlchandler import VLCHandler
from MythbuntuLircGenerator.totemhandler import TotemHandler
from MythbuntuLircGenerator.elisahandler import ElisaHandler
from MythbuntuLircGenerator.xmamehandler import XMAMEHandler
from MythbuntuLircGenerator.xmesshandler import XMESSHandler

def display_help():
    """Shows the help for this application"""
    print ""
    print "mythbuntu-lirc-generator is used to create and run a basic lircrc for "
    print "use in Mythbuntu and Ubuntu systems."
    print ""
    print "mythbuntu-lirc-generator [-h|--help] [-d] [-l|--lircrc FILE] [-L|--lircd FILE] [--mythtv 0|1] [--mplayer 0|1] [--totem 0|1] [--elisa 0|1] [--xine 0|1] [--vlc 0|1] [--xmame 0|1] [-xmess [0|1] [--repeat <number>] [ --delay <number> ]"
    print ""
    print "By default, a configuration will be generated for all possible applications"
    print "This can be overridden as described below"
    print ""
    print "-h, --help"
    print "        This help screen."
    print "-d"
    print "        Debug mode.  Useful for determining troubles when parsing"
    print "-l, --lircrc FILE"
    print "        The file that you will be writing the lircrc out to."
    print "-L, --lircd FILE"
    print "        The file you will be reading the remote configuration from."
    print "--mythtv 0|1"
    print "        By default, mythtv configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--mythtv_lircrc 0|1"
    print "        By default, mythtv configuration is generated and copied to ~/.mythtv/lircrc,"
    print "        pass 0 to disable this copying."
    print "--mplayer 0|1"
    print "        By default, mplayer configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--totem 0|1"
    print "        By default, totem configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--elisa 0|1"
    print "        By default, elisa configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--xine 0|1"
    print "        By default, xine configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--vlc 0|1"
    print "        By default, vlc configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--xmame 0|1"
    print "        By default, xmame configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--xmess 0|1"
    print "        By default, xmess configuration is generated, pass 0 to disable"
    print "        this behavior."
    print "--repeat <number>"
    print "         Default is \"0\". Modifies the repeat= directive in .lircrc."
    print "         Quote from LIRC documentation:"
    print "         \"tells the program what shall happen if a key is repeated. "
    print "         A value of zero tells the program to ignore repeated keys." 
    print "         Any other positive value 'n' tells the program to pass the"
    print "         config string every 'n'-th time to the according"
    print "         application, when a key is repeated. "
    print "         The default for repeat is zero.\" "
    print "--delay <number>"
    print "        Default is \"0\". Modifies the delay= directive in .lircrc."
    print "        Quote from LIRC documentation:"
    print "        \"tells the program to ignore the specified number of key"
    print "        repeats before using the \"repeat\" configuration directive" 
    print "        above. This is used to prevent double triggers of events "
    print "        when using a fast repeat rate. A value of zero, which also "
    print "        is the default, will disable the delay function. \""
    print ""


def main(argv):
    lircd = '/etc/lirc/lircd.conf'
    lircrc = os.getenv('HOME') + '/.lircrc'
    mythtv="1"
    mythtv_lircrc="1"
    mplayer="1"
    totem="1"
    elisa="1"
    xine="1"
    vlc="1"
    xmame="1"
    xmess="1"
# According to LIRC documentation, delay and repeat default to "0"
    delay="0"
    repeat="0"
    debug = False
    try:
        opts, args = getopt.getopt(argv, "hL:l:d", ["help", "lircd=", "lircrc=", "mythtv=", "mythtv_lircrc=", "mplayer=", "totem=", "elisa=", "xine=", "vlc=", "xmame=", "xmess=", "repeat=", "delay="])
    except getopt.GetoptError:
        display_help()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            display_help()
            sys.exit()
        elif opt in ("-d"):
            debug = True
        elif opt in ("-L", "--lircd"):
            lircd = arg
        elif opt in ("-l", "--lircrc"):
            lircrc = arg
        elif opt in ("--mythtv"):
            mythtv = arg
        elif opt in ("--mythtv_lircrc"):
            mythtv_lircrc = arg
        elif opt in ("--mplayer"):
            mplayer = arg
        elif opt in ("--totem"):
            totem = arg
        elif opt in ("--elisa"):
            elisa = arg
        elif opt in ("--xine"):
            xine = arg
        elif opt in ("--vlc"):
            vlc = arg
        elif opt in ("--xmame"):
            xmame = arg
        elif opt in ("--xmess"):
            xmess = arg
	elif opt in ("--repeat"):
	    repeat = arg
	elif opt in ("--delay"):
	    delay = arg
    if os.path.exists(lircrc):
        shutil.move(lircrc,lircrc + '.old')

    if mythtv == "1":
        generation_station = MythTVHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if mythtv_lircrc == "1":
            if os.path.exists(os.getenv('HOME') + '/.mythtv/lircrc'):
                shutil.move(os.getenv('HOME') + '/.mythtv/lircrc', os.getenv('HOME') + '/.mythtv/lircrc.old')
            if not os.path.exists(os.getenv('HOME') + '/.mythtv'):
                os.mkdir(os.getenv('HOME') + '/.mythtv')
            shutil.copy(lircrc, os.getenv('HOME') + '/.mythtv/lircrc')
        if debug:
            generation_station.debug()
    if mplayer == "1":
        generation_station = MPlayerHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()
    if xine == "1":
        generation_station = XineHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()
    if vlc == "1":
        generation_station = VLCHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()
    if xmame == "1":
        generation_station = XMAMEHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()
    if xmess == "1":
        generation_station = XMESSHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()
    if totem == "1":
        generation_station = TotemHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()
    if elisa == "1":
        generation_station = ElisaHandler(lircd,lircrc,repeat,delay)
        generation_station.generate()
        if debug:
            generation_station.debug()

    if not debug:
        print "You should now have a .lircrc file generated in " + lircrc
        if mythtv == "1":
            print "Also, a mythtv specific lircrc is now in ~/.mythtv/lircrc"
    sys.exit()

if __name__ == '__main__':
    main(sys.argv[1:])
