#!/bin/bash
set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
cd "${SCRIPT_DIR}"

INSTALL=0
UNINSTALL=0
INSTALL_FOR_ALL=0
INSTALL_PATH=""

_msboost_log() {
    local level="$1"
    shift
    local msg="$*"
    local ts
    ts=$(date "+%Y-%m-%d %H:%M:%S")
    if [[ "${level}" == "ERROR" || "${level}" == "WARNING" ]]; then
        echo "[msboost][${ts}][${level}]: ${msg}" >&2
    else
        echo "[msboost][${ts}][${level}]: ${msg}"
    fi
}

log_info() { _msboost_log "INFO" "$@"; }
log_warning() { _msboost_log "WARNING" "$@"; }
log_error() { _msboost_log "ERROR" "$@"; }

is_makeself_meta_arg() {
    case "$1" in
        --*.run|*".run")
            return 0
            ;;
        --/*|--\"*)
            return 0
            ;;
    esac
    return 1
}

# makeself prepends archive name and working directory before user args.
while [[ $# -gt 0 ]] && is_makeself_meta_arg "$1"; do
    shift
done

while [[ $# -gt 0 ]]; do
    case "$1" in
        --install|--full|--upgrade)
            INSTALL=1
            shift
            ;;
        --uninstall)
            UNINSTALL=1
            shift
            ;;
        --install-path=*)
            INSTALL_PATH="${1#--install-path=}"
            if [[ -z "${INSTALL_PATH}" ]]; then
                log_error "--install-path must not be empty"
                exit 1
            fi
            shift
            ;;
        --install-path)
            if [[ $# -lt 2 || -z "${2:-}" ]]; then
                log_error "--install-path requires a path"
                exit 1
            fi
            INSTALL_PATH="$2"
            shift 2
            ;;
        --install-for-all)
            INSTALL_FOR_ALL=1
            shift
            ;;
        --quiet|-q|--nox11|--pylocal|--force|--devel|--version)
            shift
            ;;
        --nox11=*|--install-username=*|--install-usergroup=*)
            shift
            ;;
        --install-username|--install-usergroup)
            shift 2
            ;;
        *)
            if is_makeself_meta_arg "$1"; then
                shift
            else
                log_warning "Unknown argument '${1}'"
                shift
            fi
            ;;
    esac
done

if [[ ${INSTALL} -eq 1 && ${UNINSTALL} -eq 1 ]]; then
    log_error "--install and --uninstall cannot be used together"
    exit 1
fi

if [[ ${INSTALL} -eq 0 && ${UNINSTALL} -eq 0 ]]; then
    log_error "Either --install or --uninstall must be specified"
    exit 1
fi

if [[ -z "${INSTALL_PATH}" ]]; then
    log_error "--install-path must be specified"
    exit 1
fi

if [[ ${UNINSTALL} -eq 1 ]]; then
    log_info "Running uninstallation for affinity-sched"
    python3 "${SCRIPT_DIR}/uninstall.py" --install-path="${INSTALL_PATH}"
else
    log_info "Running installation for affinity-sched"
    install_args=(--install-path="${INSTALL_PATH}")
    if [[ ${INSTALL_FOR_ALL} -eq 1 ]]; then
        install_args+=(--install-for-all)
    fi
    python3 "${SCRIPT_DIR}/install.py" "${install_args[@]}"
fi