#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
NC='\033[0m'

H2PATH="./h2path"
MODS_DIR=""
DB_FILE=""

function find_game_directory() {
    local search_dir="/"
    local target_dir="Steam/steamapps/common/Helldivers\ 2/data"

    echo "--- /// HELLDIVERS 2 MOD MANAGER /// ---" >&2

    # check if path is saved
    if [[ -f "$H2PATH" ]]; then
        saved_dir=$(cat "$H2PATH")
        if [[ -d "$saved_dir" ]]; then
            echo "Using saved game directory \$MODS_DIR: $saved_dir" >&2
            echo "$saved_dir"
            return
        else
            echo "Saved game directory is invalid"
        fi
    fi

    # first time setup, or directory is not valid anymore
    echo "Searching for the Helldivers 2 data directory..." >&2
    game_dir=$(find "$search_dir" -type d -path "*/$target_dir" 2>/dev/null | head -n 1)

    if [[ -z "$game_dir" ]]; then
        echo "Could not find the Helldivers 2 data directory automatically" >&2
        read -p "Please enter the path to the Helldivers 2 data directory: " game_dir
        if [[ ! -d "$game_dir" ]]; then
            echo "Error: Provided path is not a valid directory"
            exit 1
        fi
    fi

    echo "$game_dir" > "$H2PATH"
    echo "Game directory saved to: $H2PATH" >&2
    echo "--- /// ---" >&2
    echo "$game_dir"
}

function initialize_directories() {
    MODS_DIR=$(find_game_directory)
    DB_FILE="$MODS_DIR/mods.csv"

    if [[ ! -f "$DB_FILE" ]]; then
        touch "$DB_FILE"
        echo "Database file created at: $DB_FILE"
    fi

    echo "--- /// MODS /// ---" >&2
}

function display_help() {
    echo "Helldivers 2 Mod Manager"
    echo "Usage: h2mm [command] [options]"
    echo "Commands:"
    echo "  install -n \"<mod_name>\" <mod_files>       Install a mod with a name and files"
    echo "  list                                        List all installed mods"
    echo "  uninstall -n \"<mod_name>\"                 Uninstall a mod by name"
    echo "  uninstall -i <index>                        Uninstall a mod by index"
    echo "Usage:"
    echo "  h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream"
    echo "  h2mm uninstall -n \"Example mod\""
    echo "  h2mm uninstall -i 1"
}

function install_mod() {
    local mod_name=""
    local mod_files=()

    # parse arguments
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -n)
                mod_name="$2"
                shift 2
                ;;
            *)
                mod_files+=("$1")
                shift
                ;;
        esac
    done

    if [[ -z "$mod_name" || ${#mod_files[@]} -eq 0 ]]; then
        echo "Error: Mod name and files are required."
        exit 1
    fi

    # verify mod files exist
    for file in "${mod_files[@]}"; do
        if [[ ! -f "$file" ]]; then
            echo "Error: File $file does not exist."
            exit 1
        fi
    done

    declare -A patch_count # hash table - in case multiple named files are needed for 1 mod install, store the patch count
    for file in "${mod_files[@]}"; do
        base_name=$(echo "$file" | sed -E 's/\.+.*//')
        patch_prefix="$MODS_DIR/${base_name}.patch_"
        count=$(ls "${patch_prefix}"* 2>/dev/null | grep -E '([0-9]+$)' 2>/dev/null | wc -l) # count installed patches

        # set patch count for file name
        if [[ -z "${patch_count[$base_name]+unset}" ]]; then
            patch_count["$base_name"]=$count 
        fi

        extension=$(echo "$file" | sed -E 's/.*patch_[0-9]+//')
        target_file="${base_name}.patch_${patch_count[$base_name]}${extension}"

        cp "$file" "$MODS_DIR/$target_file"
        echo "Mod file $file installed at \$MODS_DIR/$target_file"
    done

    # add entry to database
    echo "$(($(wc -l < "$DB_FILE") + 1)),$mod_name,${mod_files[*]}" >> "$DB_FILE"
    echo "Mod $mod_name ($base_name) installed successfully"
}

function list_mods() {
    if [[ ! -s "$DB_FILE" ]]; then
        echo "No mods installed"
        return
    fi

    echo "Installed Mods:"
    cat "$DB_FILE"
}

function uninstall_mod() {
    local mod_name=""
    local mod_index=""

    # parse arguments
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -n)
                mod_name="$2"
                shift 2
                ;;
            -i)
                mod_index="$2"
                shift 2
                ;;
            *)
                echo "Error: Invalid argument $1"
                exit 1
                ;;
        esac
    done

    if [[ -z "$mod_name" && -z "$mod_index" ]]; then
        echo "Error: Mod name or index is required to uninstall"
        exit 1
    fi

    # find mod files
    if [[ -n "$mod_index" ]]; then
        entry=$(sed -n "${mod_index}p" "$DB_FILE")
    elif [[ -n "$mod_name" ]]; then
        entry=$(grep -i ",$mod_name," "$DB_FILE")
    fi

    if [[ -z "$entry" ]]; then
        echo "Error: Mod not found"
        exit 1
    fi

    # delete mod files
    files=$(echo "$entry" | cut -d',' -f3- | tr ',' ' ')
    for file in $files; do
        echo "Removing \$MODS_DIR/$file"
        rm -f "$MODS_DIR/$file"
    done

    # remove entry from database
    if [[ -n "$mod_index" ]]; then
        sed -i "/$mod_index/d" "$DB_FILE"
    elif [[ -n "$mod_name" ]]; then
        sed -i "/$mod_name/d" "$DB_FILE"
    fi
    echo "Mod uninstalled successfully"
}

# main
if [[ $# -lt 1 ]]; then
    display_help
    exit 1
fi

command="$1"
shift

initialize_directories

case "$command" in
    install)
        install_mod "$@"
        echo "--- /// ---"
        ;;
    list)
        list_mods
        echo "--- /// ---"
        ;;
    uninstall)
        uninstall_mod "$@"
        echo "--- /// ---"
        ;;
    *)
        display_help
        ;;
esac

