#!/bin/bash

if [ -z "$1" ] || [ "$1" == '-v' ] || [ "$1" == '--verbose' ]; then
    # Check if GUI is already started
    if ! pgrep -f 'python3 '.*iso-constructor &>/dev/null; then
        pkexec iso-constructor-pkexec $@
    fi
    exit 0
fi

function usage() {
    printf "
ISO constructor Help
Usage: iso-constructor [OPTIONS] [DISTRIBUTION_PATH]

DISTRIBUTION_PATH contains the unpacked distribution with a boot and root directory.

-b                      Build the distribution.
-e                      Edit the distribution.
-h                      This help screen.
-i                      Test Qemu image
-t                      Test the ISO in Qemu
-u                      Update the distribution.
-U [iso_path]           Unpack ISO

No parameters           Start the GUI
-v                      Starts GUI with verbose output
"
}

# Set some variables
BUILD=false
EDIT=false
LOCALIZE=false
UPGRADE=false
TESTISO=false

# Get parameters
while getopts 'beituU:' OPT; do
    case $OPT in
        b)
            # Build distribution
            BUILD=true
            ;;
        e)
            # Edit distribution
            EDIT=true
            ;;
        i)
            # Test the Qemu image
            sudo -u $(logname) /usr/share/iso-constructor/qemu-test.sh
            exit 0
            ;;
        t)
            # Test the ISO in Qemu
            TESTISO=true
            ;;
        u)
            # Upgrade distribution
            UPGRADE=true
            ;;
        U)
            # Unpack ISO
            ISO=$OPTARG
            ;;
        h)
            # Help
            usage
            exit 0
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done
# Get required positional argument
shift $(( OPTIND - 1 ))
DISTPATH=${1?$( echo 'Missing distribution path.' )}

# Path to chroot script
CHROOT='/usr/share/iso-constructor/chroot-dir.sh'

if [ ! -z "$ISO" ]; then
    if [ ! -e "$ISO" ]; then
        echo "$ISO does not exist - exiting"
        exit 3
    fi
    eval "sudo bash /usr/share/iso-constructor/unpack.sh \"$ISO\" \"$DISTPATH\""
fi

# Check that it is a valid distribution path
if [ ! -d "$DISTPATH/boot" ] && [ ! -d "$DISTPATH/root" ]; then
    echo "$DISTPATH is not a valid distribution path - exiting"
    exit 2
fi

if $EDIT; then
    eval "sudo bash $CHROOT \"$DISTPATH/root\""
fi

if $TESTISO; then
    sudo -u $(logname) /usr/share/iso-constructor/qemu-test.sh "$DISTPATH"
fi

if $UPGRADE; then
    sudo cp '/usr/share/iso-constructor/upgrade.sh' "$DISTPATH/root/"
    eval "sudo bash $CHROOT \"$DISTPATH/root\" \"bash /upgrade.sh\""
    sudo rm -f "$DISTPATH/root/upgrade.sh"
fi

if $BUILD; then
    eval "sudo bash /usr/share/iso-constructor/build.sh \"$DISTPATH\""
fi
