#!/usr/bin/python3.5

try: # Python2 compat first
    import ConfigParser as configparser
except ImportError as e:
    import configparser # Okay, python3's stdlib version it is.

import sys, subprocess, shutil, os, re, argparse
sys.path.append("/usr/share/customizer")

import lib.message as message

import actions.extract as extract
import actions.chroot as chroot
import actions.xnest as xnest
import actions.pkgm as pkgm
import actions.deb as deb
import actions.hook as hook
import actions.rebuild as rebuild
import actions.qemu as qemu
import actions.clean as clean

try:
    class OverrideDebug(argparse.Action):
        def __call__(self, parser, namespace, values, option_string=None):
            message.DEBUG = True
            setattr(namespace, self.dest, values)

    parser = argparse.ArgumentParser(prog='customizer', \
        description='Ubuntu Live CD remastering tool')
    parser.add_argument('-e', '--extract', action='store_true', \
        help='Extract ISO image')
    parser.add_argument('-c', '--chroot', action='store_true', \
        help='Chroot into the filesystem')
    parser.add_argument('-x', '--xnest', action='store_true', \
        help='Execute nested X-session')
    parser.add_argument('-p', '--pkgm', action='store_true', \
        help='Execute package manager')
    parser.add_argument('-d', '--deb', action='store_true', \
        help='Install Debian package')
    parser.add_argument('-k', '--hook', action='store_true', \
        help='Execute hook')
    parser.add_argument('-r', '--rebuild', action='store_true', \
        help='Rebuild the ISO image')
    parser.add_argument('-q', '--qemu', action='store_true', \
        help='Test the built ISO image with QEMU')
    parser.add_argument('-t', '--clean', action='store_true', \
        help='Clean all temporary files and folders')
    parser.add_argument('-D', '--debug', nargs=0, action=OverrideDebug, \
        help='Enable debug messages')
    parser.add_argument('-v', '--version', action='version', \
        version='Customizer 4.2.0 (Zip Release)', \
        help='Show Customizer version and exit')
    ARGS = parser.parse_args()

    if not os.geteuid() == 0:
        message.critical('You are not root')
        sys.exit(2)
    elif int(sys.version_info[0]) < 3:
        message.critical('You are attempting to run Customizer with Python 2')

    if ARGS.extract:
        message.info('Extracting...')
        extract.main()

    if ARGS.chroot:
        message.info('Chrooting...')
        chroot.main()

    if ARGS.xnest:
        message.info('Running nested X session...')
        xnest.main()

    if ARGS.pkgm:
        message.info('Running package manager...')
        pkgm.main()

    if ARGS.deb:
        message.info('Installing Debian package...')
        deb.main()

    if ARGS.hook:
        message.info('Running chroot hook...')
        hook.main()


    if ARGS.rebuild:
        message.info('Rebuilding ISO...')
        rebuild.main()

    if ARGS.qemu:
        message.info('Running QEMU...')
        qemu.main()

    if ARGS.clean:
        message.info('Cleaning up...')
        clean.main()

except configparser.Error as detail:
    message.critical('CONFIGPARSER: ' + str(detail))
    sys.exit(3)
except subprocess.CalledProcessError as detail:
    message.critical('SUBPROCESS: ' + str(detail))
    sys.exit(4)
except shutil.Error as detail:
    message.critical('SHUTIL: ' + str(detail))
    sys.exit(5)
except OSError as detail:
    message.critical('OS: ' + str(detail))
    sys.exit(6)
except IOError as detail:
    message.critical('IO: ' + str(detail))
    sys.exit(7)
except re.error as detail:
    message.critical('REGEXP: ' + str(detail))
    sys.exit(8)
except KeyboardInterrupt:
    message.critical('Interupt signal received')
    sys.exit(9)
except message.exception as detail:
    message.critical('CUSTOMIZER: ' + str(detail))
    sys.exit(10)
except SystemExit:
    sys.exit(2)
except Exception as detail:
    message.critical('Unexpected error', detail)
    sys.exit(1)
#finally:
#    raise
