#!/bin/bash

# --- Globals ---

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

H2PATH="${HOME}/.config/h2path"
MODS_DIR=""
DB_FILE=""

# --- Utility Functions ---

function get_filename_without_path() {
	echo $(echo "$1" | awk -F/ '{print $NF}')
}

function get_basename() {
	echo $(get_filename_without_path "$1" | sed -E 's/\.+.*//')
}

function find_game_directory() {
	local search_dir="${HOME}"
	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
		game_dir=$(eval echo "$game_dir")
		if [[ ! -d "$game_dir" ]]; then
			echo -e "${RED}Error${NC}: Provided path is not a valid directory." >&2
			exit 1
		fi
	fi

	echo "$game_dir" > "$H2PATH"

	if [[ $? -eq 0 ]]; then
		echo -e "Game directory ${GREEN}saved${NC}: $game_dir" >&2
	else
		echo -e "${RED}Error${NC}: Could not save game directory." >&2
		exit 1
	fi
	
	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"
		if [[ $? -eq 0 ]]; then
			echo "Database file created at: $DB_FILE"
		else
			echo -e "${RED}Error${NC}: Could not create database file." >&2
			exit 1
		fi
	fi

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

# --- Help Functions ---

function display_help() {
	echo "Helldivers 2 Mod Manager v0.1.4"
	echo "Usage: h2mm [command] [options]"
	echo "Commands:"
	echo "	install					Install a mod with files (short form: h2mm i)."
	echo "	uninstall				Uninstall a mod by name (short form: h2mm u)."
	echo "	list					List all installed mods (short form: h2mm l)."
	echo "	export <zip_name>			Export installed mods to a zip file (short form: h2mm ex)."
	echo "	import <zip_name>			Import mods from a zip file (short form: h2mm im)."
	echo "	reset					Reset all installed mods (short form: h2mm rr)."
	echo "	help					Display this help message (short form: h2mm h)."
	echo "For more information on usage, use h2mm [command] --help, available for install and uninstall."
	echo "Basic Usage:"
	echo "	h2mm install -z /path/to/mod.zip"
	echo "	h2mm install -d /path/to/mod/files"
	echo "	h2mm uninstall \"Example mod\""
}

function display_install_help() {
	echo "Usage: h2mm install [options] <mod_files|mod_dir|mod_zip>"
	echo "Short form: h2mm i"
	echo "Options:"
	echo "	-n \"<mod_name>\"		Name the mod yourself, inside double quotes."
	echo "	<mod_files>		Multiple mod files, accepts wildcards."
	echo "	<mod_dir>		Directory containing mod files."
	echo "	<mod_zip>		Zip file containing mod files."
	echo "Usage:"
	echo "	h2mm install /path/to/mod.zip"
	echo "	h2mm install /path/to/mod/files"
	echo "	h2mm install -n \"Example mod\" mod.patch_0 mod.patch_0.stream # -n is mandatory when using files"
	echo "	h2mm install -n \"Example mod\" mod* # using a wildcard to include all files"
}

function display_uninstall_help() {
	echo "Usage: h2mm uninstall [options] \"<mod_name>\""
	echo "Short form: h2mm u"
	echo "Options:"
	echo "	-i <index>		Index of the mod to uninstall."
	echo "Usage:"
	echo "	h2mm uninstall \"Example mod\""
	echo "	h2mm uninstall -i 1 # uninstall mod with index 1"
}

function display_list_help() {
	echo "Usage: h2mm list"
	echo "Short form: h2mm l"
	echo "List all installed mods."
	echo "Database of mods is stored in Steam/steamapps/common/Helldivers\ 2/data/mods.csv"
	echo "You can rename, delete, or edit this file to manage mods manually."
}

function display_reset_help() {
	echo "Usage: h2mm reset"
	echo "Short form: h2mm r"
	echo "Reset all installed mods."
	echo "Deletes all installed mods and the database file."
	echo "Database of mods is stored in Steam/steamapps/common/Helldivers\ 2/data/mods.csv, along with the mods."
}

function display_export_help() {
	echo "Usage: h2mm export"
	echo "Short form: h2mm ex"
	echo "Export installed mods and database to a zip file."
}

function display_import_help() {
	echo "Usage: h2mm import"
	echo "Short form: h2mm im"
	echo "Import mods and database from a zip file (coming from h2mm)."
}

# --- Main Functions ---

function mod_reset() {
	if [[ "$1" == "--help" || "$1" == "-h" ]]; then
		display_reset_help
		exit 0
	fi

	read -p "Are you sure you want to reset all installed mods? (Y/n): " confirm
	if [[ "$confirm" == "y" || "$confirm" == "Y" || "$confirm" = "" ]]; then
		rm -f "$MODS_DIR"/*.patch_*
		rm -f "$DB_FILE"
		rm -f "$H2PATH"
		echo "Mods and database file deleted."
	fi
}

function mod_install() {
	local mod_name=""
	local mod_files=()
	local mod_dir=""

	if [[ $# -eq 0 ]]; then
		display_install_help
		exit 0
	fi

	# parse arguments
	while [[ $# -gt 0 ]]; do
		case "$1" in
        -n)
            mod_name="$2"
            shift 2
            ;;
        --help|-h)
            display_install_help
            exit 0
            ;;
        *)
            if [[ -f "$1" && "$1" == *.zip ]]; then
                mod_zip="$1"
            elif [[ -d "$1" ]]; then
                mod_dir="$1"
            else
                mod_files+=("$1")
            fi
            shift
            ;;
    esac
	done

	# zip file containing mod files
	if [[ -n "$mod_zip" ]]; then
		if ! command -v unzip &> /dev/null; then
			echo -e "${RED}Error${NC}: unzip is not installed, please install the package and try again." >&2
			exit 1
		fi

		if [[ ! -f "$mod_zip" ]]; then
			echo -e "${RED}Error${NC}: Zip file $mod_zip does not exist." >&2
			exit 1
		fi

		# check if mod name was provided, otherwise use the zip file name, get rid of .zip and version numbers
		if [[ -z "$mod_name" ]]; then
			mod_name=$(basename "$mod_zip" | sed -E 's/\.zip//' | awk -F/ '{print $NF}' | sed -E 's/-[0-9]+-.*//')
			echo "$mod_name" 
		fi

		mod_dir=$(mktemp -d)
		unzip -qq "$mod_zip" -d "$mod_dir"
	fi

	# directory containing mod files
	if [[ -n "$mod_dir" ]]; then
		if [[ ! -d "$mod_dir" ]]; then
			echo -e "${RED}Error${NC}: Directory $mod_dir does not exist." >&2
			exit 1
		fi

		readarray -d '' mod_files < <(find "$mod_dir" -type f -name "*.patch_*" -print0)
		if [[ -z "$mod_name" ]]; then
			mod_name=$(echo "$mod_dir" | sed 's:/*$::' | awk -F/ '{print $NF}' | sed -E 's/-[0-9]+-.*//')
		fi
	fi
	
	# verify minimum information required
	if [[ -z "$mod_name" || ${#mod_files[@]} -eq 0 ]]; then
		echo -e "${RED}Error${NC}: Mod name and files are required." >&2
		exit 1
	fi

	# verify mod files exist and is not directory
	for file in "${mod_files[@]}"; do
		if [[ ! -f "$file" ]]; then
			if [[ ! -d "$file" ]]; then
				echo -e "${RED}Error${NC}: File $file does not exist." >&2
				exit 1
			else
				mod_files=(${mod_files[@]/$file})
			fi
		fi
	done

	# hash table - in case multiple named files are needed for 1 mod install, store the patch count
	declare -A patch_count
	# store the target files so we can put them in the database later
	target_files=()
	# sort the mod files because with the below logic, the .stream and .gpu_resources files need to come after their respective patch files
	IFS=$'\n' mod_files=($(printf "%s\n" "${mod_files[@]}" | sort -t. -k1,1 -k2,2n)); unset IFS

	for file in "${mod_files[@]}"; do
		base_name=$(get_basename "$file")
		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[$file]+unset}" ]]; then
			patch_count["$file"]=$count 
		fi
		patch_count["$base_name"]=$count

		# if the file has an extension, look for the last patch number and use that
		extension=$(echo "$file" | sed -E 's/.*patch_[0-9]+//')
		if [[ -n "$extension" ]]; then
			target_file="${base_name}.patch_$((patch_count[$base_name] - 1))${extension}"
		else
			target_file="${base_name}.patch_${patch_count[$file]}"
		fi
		
		target_files+=($target_file)

		cp "$file" "$MODS_DIR/$target_file"
		if [[ $? -eq 0 ]]; then
			echo -e "Mod file ${ORANGE}$file${NC} installed at ${GREEN}\$MODS_DIR/$target_file${NC}." >&2
		else
			echo -e "${RED}Error${NC}: Could not install mod file $file." >&2
			exit 1
		fi
	done

	# add entry to database
	next_id=$(awk -F, 'END {print $1 + 1}' "$DB_FILE")
	echo "$next_id,$mod_name,${target_files[*]}" >> "$DB_FILE"
	echo -e "Mod $mod_name ($base_name) ${GREEN}installed successfully${NC}." >&2
}

function mod_list() {
	if [[ "$1" == "--help" || "$1" == "-h" ]]; then
		display_list_help
		exit 0
	fi

	if [[ ! -s "$DB_FILE" ]]; then
		echo "No mods installed."
		return
	fi

	awk -F, '{ if (length($3) > 150) $3 = substr($3, 1, 147) "..."; printf "%2s. %s (%s)\n", $1, $2, $3 }' "$DB_FILE"
}

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

	if [[ $# -eq 0 ]]; then
		display_uninstall_help
		exit 0
	fi

	# parse arguments
	while [[ $# -gt 0 ]]; do
		case "$1" in
			-i)
				mod_index="$2"
				shift 2
				;;
			--help|-h)
				display_uninstall_help
				exit 0
				;;
			*)
				mod_name="$1"
				shift 1
				;;
		esac
	done

	if [[ -z "$mod_name" && -z "$mod_index" ]]; then
		echo -e "${RED}Error${NC}: Mod name or index is required to uninstall." >&2
		exit 1
	fi

	# find mod files
	if [[ -n "$mod_index" ]]; then
		entry=$(grep "^${mod_index}," "$DB_FILE")
		mod_name=$(echo "$entry" | awk -F, '{print $2}')
	elif [[ -n "$mod_name" ]]; then
		entry=$(grep -i ",$mod_name," "$DB_FILE")
		mod_index=$(echo "$entry" | awk -F, '{print $1}' | head -1)
	fi

	if [[ -z "$entry" ]]; then
		echo -e "${RED}Error${NC}: Mod not found." >&2
		exit 1
	fi

	# delete mod files
	files=$(echo "$entry" | cut -d',' -f3- | tr ',' ' ' | head -1)
	declare -A downgrades
	for file in $files; do
		if [[ ! -f "$MODS_DIR/$file" ]]; then
			echo -e "${RED}Error${NC}: Mod file $file does not exist." >&2
			exit 1
		else
			echo -e "Removing ${ORANGE}\$MODS_DIR/$file${NC}." >&2
			rm -f "$MODS_DIR/$file"

			base_name=$(get_basename "$file")
			current_version=$(echo $file | grep -oP '(?<=patch_)\d+')
			downgrades["$base_name"]=current_version
		fi
	done

	# downgrade any necessary mods
	for file in "${!downgrades[@]}"; do
		# find all files that have the same base name, and are greater than the current version, and downgrade them
		base_name=$(get_basename "$file")
		same_patches=$(ls "$MODS_DIR/${base_name}.patch_"* 2>/dev/null | grep -Eo "$base_name.*")

		for patch in $same_patches; do
			patch_version=$(echo $patch | grep -oP '(?<=patch_)\d+')
			if [[ $patch_version -gt ${downgrades[$file]} ]]; then
				new_version=$((patch_version - 1))
				extension=$(echo "$patch" | sed -E 's/.*patch_[0-9]+//')

				new_patch="${base_name}.patch_${new_version}${extension}"
				mv "$MODS_DIR/$patch" "$MODS_DIR/$new_patch"
				echo -e "Downgraded ${ORANGE}$patch${NC} to ${GREEN}\$MODS_DIR/$new_patch${NC}." >&2
				
				# save changes in database as well
				sed -i "s/$patch/$new_patch/" "$DB_FILE"
			fi
		done
	done

	# remove entry from database
	sed -i "/^$mod_index/d" "$DB_FILE"

	echo -e "Mod ${GREEN}uninstalled successfully${NC}." >&2
}

function mod_export() {
	if [[ "$1" == "--help" || "$1" == "-h" ]]; then
		display_export_help
		exit 0
	fi

	if ! command -v zip &> /dev/null; then
		echo -e "${RED}Error${NC}: zip is not installed, please install the package and try again." >&2
		exit 1
	fi

	echo -ne "Zip file will be saved in the current directory ($(pwd)). Continue? (Y/n): "
    read -r confirm
	if [[ "$confirm" == "y" || "$confirm" == "Y" || "$confirm" = "" ]]; then
		OUT_DIR=$(mktemp -d)
		MODS_EXPORT_DIR="$OUT_DIR/Helldivers 2 Mods"
		mkdir -p "$MODS_EXPORT_DIR"
		cp "$DB_FILE" "$MODS_EXPORT_DIR"
		ls "$MODS_DIR/" 2>/dev/null | grep -E 'patch_.*' | xargs -I {} cp "$MODS_DIR/{}" "$MODS_EXPORT_DIR"

		if [[ $? -ne 0 ]]; then
			echo -e "${RED}Error${NC}: Could not export mods. Possibly because no mods are present." >&2
			exit 1
		fi

		current_path=$(pwd)
		zip_name="Helldivers_2_Mods_$(date +%Y-%m-%d_%H-%M-%S).zip"
		cd "$OUT_DIR"
		zip -r "$zip_name" "Helldivers 2 Mods"
		mv "$zip_name" "$current_path"

		if [[ $? -eq 0 ]]; then
			echo -e "Mods exported to ${GREEN}$current_path/$zip_name${NC}." >&2
		else
			echo -e "${RED}Error${NC}: Failed to export mods." >&2
		fi
	fi
}

function mod_import() {
	if [[ "$1" == "--help" || "$1" == "-h" ]]; then
		display_import_help
		exit 0
	fi

	if [[ ! -f "$1" ]]; then
		echo -e "${RED}Error${NC}: File $1 does not exist." >&2
		exit 1
	fi

	if ! command -v unzip &> /dev/null; then
		echo -e "${RED}Error${NC}: unzip is not installed, please install the package and try again." >&2
		exit 1
	fi

	OUT_DIR=$(mktemp -d)
	unzip -qq "$1" -d "$OUT_DIR"

	if [[ $? -ne 0 ]]; then
		echo -e "${RED}Error${NC}: Could not import mods. Possibly because the zip file is invalid." >&2
		exit 1
	fi

	MODS_EXPORT_DIR="$OUT_DIR/Helldivers 2 Mods"
	if [[ ! -d "$MODS_EXPORT_DIR" ]]; then
		echo -e "${RED}Error${NC}: Could not import mods. Possibly because the zip file is invalid." >&2
		exit 1
	fi

	# copy mods verbosely
	cp -v "$MODS_EXPORT_DIR"/* "$MODS_DIR"
	if [[ $? -eq 0 ]]; then
		echo -e "Mods imported ${GREEN}successfully${NC}." >&2
	else
		echo -e "${RED}Error${NC}: Failed to import mods." >&2
	fi
}

# --- Main ---

function main() {
	if [[ $# -lt 1 ]]; then
		display_help
		exit 1
	fi

	command="$1"
	shift
	initialize_directories

	case "$command" in
		install|i)
			mod_install "$@"
			;;
		list|l)
			mod_list "$@"
			;;
		uninstall|u)
			mod_uninstall "$@"
			;;
		export|ex)
			mod_export "$@"
			;;
		import|im)
			mod_import "$@"
			;;
		reset|r)
			mod_reset "$@"
			;;
		help|--help|-h|h)
			display_help
			;;
		*)
			display_help
			;;
	esac

	echo "--- /// END /// ---"
}

main "$@"
