#!/usr/bin/env bash

set -euo pipefail

source_dir="${EQL_GAME_PATH:-${HOME}/Games/EverQuestLegends}"
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)

if [[ ! -d "${source_dir}" ]]; then
    echo "Error: EverQuest Legends directory not found: ${source_dir}" >&2
    echo "Set EQL_GAME_PATH to the game install directory." >&2
    exit 1
fi

if [[ ! -f "${source_dir}/eqclient.ini" ]]; then
    echo "Error: eqclient.ini not found in ${source_dir}" >&2
    exit 1
fi

shopt -s nullglob
player_configs=(
    "${source_dir}"/[!_]*_*.ini
)

# Player settings use Character_Server.ini, Character_Server_Class.ini, and
# UI_Character_Server_Class.ini names. Launcher INIs do not match this shape.
configs=("${source_dir}/eqclient.ini" "${player_configs[@]}")

for config in "${configs[@]}"; do
    destination="${script_dir}/$(basename -- "${config}")"
    cp -- "${config}" "${destination}"
    chmod 0644 "${destination}"
    echo "Updated eql/$(basename -- "${config}")"
done

echo "Captured ${#configs[@]} config file(s) from ${source_dir}"
