#!/usr/bin/env bash
# Master Tool installer for Linux:
#
#   curl -fsSL https://cybertoken.io/master-tool/install.sh | bash
#
# Downloads the newest Master Tool from GitLab (software1015210/master-tool, branch main)
# and runs its install.sh: a .deb or .rpm in ~/.master-tool, and the master-tool command
# in ~/.local/bin. Missing programs it needs are offered for install first (sudo).
#
# Environment: MASTER_TOOL_REPO_URL  where to download from (default: the GitLab repo)

# Everything is inside main(), which runs on the last line: a download cut off half-way
# runs nothing.
main() {
    set -eu
    local repo="${MASTER_TOOL_REPO_URL:-https://gitlab.com/software1015210/master-tool}"
    local source="$HOME/restore/source"
    local archive="$source/master-tool-main.tar.gz"

    echo "== Master Tool installer"
    if [ "$(id -u)" -eq 0 ]; then
        echo "Run this as your normal user, not as root: it installs into your home folder"
        echo "and asks for sudo itself where needed."
        return 1
    fi

    # What install.sh and the tool need, per package manager.
    local missing="" manager=""
    have() { command -v "$1" >/dev/null 2>&1; }
    if have apt-get; then
        manager=apt
        have rsync || missing="$missing rsync"
        have dpkg-deb || missing="$missing dpkg"
        have python3 || missing="$missing python3"
        python3 -c "import ensurepip, venv" 2>/dev/null || missing="$missing python3-venv"
        if [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ] && ! python3 -c "import tkinter" 2>/dev/null; then
            missing="$missing python3-tk"
        fi
    elif have dnf; then
        manager=dnf
        have rsync || missing="$missing rsync"
        have rpmbuild || missing="$missing rpm-build"
        have python3 || missing="$missing python3"
        if [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ] && ! python3 -c "import tkinter" 2>/dev/null; then
            missing="$missing python3-tkinter"
        fi
    else
        echo "This installer supports Debian / Ubuntu / Mint (apt) and Fedora (dnf)."
        echo "Elsewhere: download $repo/-/archive/main/master-tool-main.tar.gz and run redesign/run.sh."
        return 1
    fi
    have sudo || { echo "sudo is needed to install the package."; return 1; }
    have tar && have gzip || missing="$missing tar gzip"
    have wget || have curl || missing="$missing wget"

    if [ -n "$missing" ]; then
        echo "Needed first:$missing"
        local answer=y
        if [ -r /dev/tty ]; then
            printf 'Install them now with: sudo %s install%s ? [Y/n] ' "$manager" "$missing"
            read -r answer < /dev/tty || answer=n
        fi
        case "$answer" in
            ""|[yY]*)
                if [ "$manager" = apt ]; then
                    sudo apt-get update
                    # shellcheck disable=SC2086
                    sudo apt-get install -y $missing
                else
                    # shellcheck disable=SC2086
                    sudo dnf install -y $missing
                fi ;;
            *) echo "Install them, then run this again."; return 1 ;;
        esac
    fi

    echo "== Downloading $repo/-/archive/main/master-tool-main.tar.gz"
    mkdir -p "$source"
    rm -rf "$archive" "$source/master-tool-main"
    if have wget; then
        wget -q --show-progress -O "$archive" "$repo/-/archive/main/master-tool-main.tar.gz" 2>&1 \
            || wget -q -O "$archive" "$repo/-/archive/main/master-tool-main.tar.gz"
    else
        curl -fL --progress-bar -o "$archive" "$repo/-/archive/main/master-tool-main.tar.gz"
    fi
    tar -xzf "$archive" -C "$source"
    [ -f "$source/master-tool-main/install.sh" ] || { echo "The download has no install.sh."; return 1; }

    echo "== Installing (this asks for your sudo password for the package)"
    bash "$source/master-tool-main/install.sh" < /dev/null

    echo
    echo "Done. Start it with:  master-tool"
    case ":$PATH:" in
        *":$HOME/.local/bin:"*) ;;
        *) echo "(In this terminal first run:  export PATH=\"\$PATH:$HOME/.local/bin\"  - new terminals have it.)" ;;
    esac
}

main "$@"
