#!/usr/bin/python -u

import sys

sys.path.append("/usr/lib/eroaster")

from os import environ, popen
from constants import title, version, year
from main import Application
from tools import striplist, which, TRUE, FALSE
from log4py import Logger, LOGLEVEL_VERBOSE

logger = Logger().get_instance()
logger.set_loglevel(LOGLEVEL_VERBOSE)

# when installing eroaster, do not fire up the interface
if (not environ.has_key("EROASTERINSTALL")):

    mkisofs = FALSE
    readcd = FALSE
    cdrecord = FALSE
    isoinfo = FALSE
    cdda2wav = FALSE

    logger.info("%s %s starting up" % (title, version))

    # Checking username
    uname = environ["USER"]
    if (uname != "root"):
        logger.warn("You are %s - you may want to be root" % uname)
    else:
        logger.info("You are %s - that's fine" % uname)

    logger.info("Checking for required programs")

    if (which("mkisofs")) != "": 
        mkisofs = TRUE
    else:
        logger.error("Couldn't find mkisofs")
    if which("cdrecord") != "": 
        cdrecord = TRUE
    else:
        logger.error("Couldn't find cdrecord")
    if (which("isoinfo") != ""):
        isoinfo = TRUE
    else:
        logger.error("Couldn't find isoinfo")
    if (which("readcd") != ""):
        readcd = TRUE
    else:
        logger.error("Couldn't find readcd")
    if (which("cdda2wav") != ""):
        cdda2wav = TRUE
    else:
        logger.error("Couldn't find cdda2wav")

    # Check for additional programs
    logger.info("Checking for additional programs")

    # Check for mpg123ss
    if (which("mpg123") == ""):
        logger.warn("Couldn't find mpg123")

    # Check for ogg123
    if (which("ogg123") == ""):
        logger.warn("Couldn't find ogg123")

    # Check for ogginfo
    if (which("ogginfo") == ""):
        logger.warn("Couldn't find ogginfo")

    # Check for sox
    if (which("sox") == ""):
        logger.warn("Couldn't find sox")

    # Check for xmms
    if (which("xmms") == ""):
        logger.warn("Couldn't find xmms")

    # Check for freeamp
    if (which("freeamp") == ""):
        logger.warn("Couldn't find freeamp")
        
    # Check for normalize
    if (which("normalize") == ""):
        logger.warn("Couldn't find normalize")

    # Check for lame (mp3 encoder)
    if (which("lame") == ""):
        logger.warn("Couldn't find lame")

    # Check for bchunk (.cue/.bin to .iso converter)
    if (which("bchunk") == ""):
        logger.warn("Couldn't find bchunk")

    if (mkisofs == FALSE) or (cdrecord == FALSE) or (isoinfo == FALSE) or (readcd == FALSE) or (cdda2wav == FALSE):
        logger.error("One or more required programs couldn't be found.")
        sys.exit(1)

    # Start the application
    app = Application()
    app.main()
