#!/usr/bin/env bash
# Essentially this is completely nabbed from Geode's linux.sh installer
TEMP_DIR=$(mktemp -d)

# Colors
RED='\033[0;31m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

# This only has verbose for now
while getopts v OPTS; do
    case ${OPTS} in
        v) VERBOSE=1 ;;
    esac
done

verbose_log() {
    if [ ! -z "$1" ] && [ ! -z "$VERBOSE" ]; then
        echo -e "[VERBOSE]: $1"
    fi
}

check_dependencies() {
    if ! [ -x "$(command -v unzip)" ]; then
        echo -e "${RED}Error${NC}: unzip is not installed." >&2
        exit 1
    fi

    if ! [ -x "$(command -v curl)" ]; then
        echo -e "${RED}Error${NC}: curl is not installed." >&2
        exit 1
    fi

    if ! [ -x "$(command -v jq)" ]; then
        echo -e "${RED}Error${NC}: jq is not installed." >&2
        exit 1
    fi
}

install() {

    if [ ! -d "$TEMP_DIR" ]; then
        echo -e "${RED}Error:${NC} Download directory $TEMP_DIR doesn't exist." >&2
        exit 1
    fi

    echo ""
    echo "Downloading Mega Hack V8..."
    if ! curl -L -o "$TEMP_DIR/mhv8.zip" "https://absolutely.b-cdn.net/download/MegaHack_v8_installer.zip"; then
        echo -e "${RED}Error:${NC} Failed to download Mega Hack V8." >&2
        exit 1
    fi

    echo "Unzipping..."

    if ! unzip -qq "$TEMP_DIR/mhv8.zip" -d "$TEMP_DIR/mhv8"; then
        echo -e "${RED}Error:${NC} Failed to unzip Mega Hack V8." >&2
        exit 1
    fi

    if ! [ -x "$(command -v protontricks)" ]; then
        echo -e "Protontricks not installed, attempting to install..." >&2

        # Detect package manager and install protontricks
        if [ -x "$(command -v apt)" ]; then
            verbose_log "Detected apt package manager"
            sudo apt update && sudo apt install -y protontricks
        elif [ -x "$(command -v dnf)" ]; then
            verbose_log "Detected dnf package manager"
            sudo dnf install -y protontricks
        elif [ -x "$(command -v yum)" ]; then
            verbose_log "Detected yum package manager"
            sudo yum install -y protontricks
        elif [ -x "$(command -v pacman)" ]; then
            verbose_log "Detected pacman package manager"
            sudo pacman -S --noconfirm protontricks
        elif [ -x "$(command -v zypper)" ]; then
            verbose_log "Detected zypper package manager"
            sudo zypper install -y protontricks
        elif [ -x "$(command -v emerge)" ]; then
            verbose_log "Detected portage package manager"
            sudo emerge --ask=n games-util/protontricks
        elif [ -x "$(command -v xbps-install)" ]; then
            verbose_log "Detected xbps package manager"
            sudo xbps-install -S protontricks
        elif [ -x "$(command -v apk)" ]; then
            verbose_log "Detected apk package manager"
            sudo apk add protontricks
        else
            echo -e "${YELLOW}Warning:${NC} Could not detect package manager. Please install protontricks manually." >&2
            echo -e "You can install it via pip: ${YELLOW}pip install protontricks${NC}" >&2
        fi

        # Check if installation was successful
        if ! [ -x "$(command -v protontricks)" ]; then
            echo -e "${YELLOW}Warning:${NC} Protontricks installation may have failed. Please install it manually if needed." >&2
        else
            echo "Protontricks installed successfully"
        fi
    fi

    echo "Running MHV8 installer (this may take a second...)"

    # Run UniversalInstaller.exe using protontricks in Geometry Dash's Proton prefix
    verbose_log "Running UniversalInstaller.exe via protontricks..."
    if [ -x "$(command -v protontricks)" ]; then
        # Geometry Dash Steam App ID is 322170
        if ! protontricks-launch --appid 322170 "$TEMP_DIR/mhv8/MegaHackInstaller/UniversalInstaller.exe" &> ${TEMP_DIR}/protontricks.log; then
            echo -e "${YELLOW}Warning:${NC} Protontricks installation may have failed." >&2
            exit 1
        else
            verbose_log "UniversalInstaller.exe completed successfully"
            return 0
        fi
    else
        echo -e "${RED}ERROR:${NC} Protontricks not available." >&2
        exit 1
    fi
}

check_dependencies
verbose_log "${YELLOW}Running in verbose mode.${NC}"
verbose_log "Using $TEMP_DIR as temporary directory for installation."

install

echo -e "MHV8 has been installed successfully!"
echo -e "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo -e "┃ Make sure to set \"${YELLOW}WINEDLLOVERRIDES=\"XINPUT1_4=n,b\" %command%${NC}\" as your ${YELLOW}launch options${NC} for Geometry Dash inside ${BLUE}Steam${NC}. ┃"
echo -e "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
echo ""
