#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Name:     multibootusb
# Purpose:  Main file which will determine if cli or gui is to be opened
# Authors:  Sundar
# Licence:  This file is a part of multibootusb package. You can redistribute it or modify
# under the terms of GNU General Public License, v.2 or above

import logging
import getopt
import sys
import os
import platform

# The following line is required for distros based on rpm so as to avoid import errors when running from installed system
sys.path.append('/usr/local/lib/python3.4/dist-packages')
# print(sys.path)

# Had trouble in importing scripts directory. Had to add few lines below to ensure it works on source as well as
# post install
try:
    from scripts.mbusb_cli import *
    from scripts import admin
    from scripts import gen
    from scripts import config
except:
    try:
        from .scripts.mbusb_cli import *
        from .scripts import admin
        from .scripts import gen
        from .scripts import config
    except:
        import scripts

gui = True
uninstall = False


def running_from():
    """
    Print version and path location (installed or source code) info for debugging later.
    """
    if os.path.exists('scripts'):
        gen.log('Running multibootusb version ' + gen.mbusb_version() + ' from source...')
    else:
        gen.log('Running multibootusb version ' + gen.mbusb_version() + ' from installed system...')


def usage():
    print('\n An advanced multiboot live usb creator using command line.')
    print('\n Usage: python3 multibootusb [option(s)]\n')
    print(' Options:\n')
    print('  -h or --help        :   Print this help message and exit')
    print('  -i or --iso         :   Path to ISO file()s. If may ISOs are supplied, it should be seperated by \',\' and no space is allowed between ISOs.')
    print('  -t or --target      :   Path to target USB device partition (example /dev/sdb1)')
    print('  -c or --command     :   Invoke command line usage. This option is must')
    print('  -y or --yes         :   Default yes for user input during install. Will not wait for user')
    print('  -u or --uninstall   :   List and uninstall distro from an USB disk')
    print('  -d or --debug       :   Enable debug messages (very verbose!)\n')
    print(' Eexample for making a bootable USB from command line should look like this:\n')
    print('\tpython3 multibootusb -c -i ../../favourite.iso -t /dev/sdb1 on Linux and \n')
    print('\tpython3 multibootusb -c -i ../../favourite.iso -t G: on Windows\n')
    print(' Example for uninstalling distro from an USB should look like this:\n')
    print('\tpython3 multibootusb -c -u -t /dev/sdb1 on Linux and \n')
    print('\tpython3 multibootusb -c -u -t G: on Windows\n')
    print(' Example for installing multiple distro without user intervention look like this:\n')
    print('\tpython3 multibootusb -c -y -i ../../favourite.iso,../../other-distro.iso -t /dev/sdb1 on Linux and \n')
    print('\tpython3 multibootusb -c -i ../../favourite.iso,../../other-distro.iso -t G: on Windows\n')
    exit(2)


def start_gui():
    from scripts import mbusb_gui
    gen.log('Starting multibootusb GUI...')
    if platform.system() == 'Linux':
        if os.getuid() != 0:
            gen.log('\n\nAdmin privilege is required to run multibootusb.\n If you are running from source try '
                  '\'sudo python3 ./multibootusb\'\n or you can try \'multibootusb-pkexec\' (post install)\n\n',
                    info=False, debug=True)
    mbusb_gui.main_gui()


if __name__ == '__main__':
    running_from()
    if platform.system() == 'Windows':
        if not admin.isUserAdmin():
            admin.runAsAdmin()
            sys.exit(0)
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'i:t:yvhcud',
                                   ['iso=', 'target=', 'yes', 'version', 'help', 'command', 'uninstall', 'debug'])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(2)
        elif opt in ('-v', '--version'):
            print_version()
            sys.exit()
        elif opt in ('-i', '--iso'):
            if ',' not in arg:
                config.image_path = arg
            else:
                config.image_path = arg.split(',')
        elif opt in ('-t', '--target'):
            config.usb_disk = arg
        elif opt in ('-c', '--command'):
            gui = False
        elif opt in ('-u', '--uninstall'):
            uninstall = True
        elif opt in ('-d', '--debug'):
            config.debug = True
        elif opt in ('-y', '--yes'):
            config.yes = True
        else:
            gui = True
            #start_gui()
            '''
            usage()
            sys.exit()
            '''

if config.debug is True:
    from scripts.debug import colors
    log(colors.HEADER + "=== DEBUG ENABLED ===")


if gui is False:
    if uninstall is True and config.usb_disk is not '':
        cli_uninstall_distro()
    elif uninstall is True and config.usb_disk is '':
        log('\nYou must provide \'-t\' option to point to your USB disk for uninstalling a distro.\n'
            'See the usage example below.')
        usage()
    elif config.image_path is '' and config.usb_disk is '':
        log('\nNo option provided. See the usage below.')
        usage()
    elif config.image_path is '' or config.usb_disk is '':
        log('\nOptions \'-i\' and \'-t\' must be supplied together. See the usage below.')
        usage()
    else:
        cli_install_distro()

elif gui is True:
    start_gui()
