#!/usr/bin/env bash
# shellcheck disable=SC2016

if [ -z "${TMPDIR}" ]; then
  tmpdir_infix="btb"
  tmpdir_root="/tmp"
  tmpdir_prefix="tmp.__omw-"
  tmpdir_pattern="${tmpdir_prefix}${tmpdir_infix}-XXXXXXXXXX"
  fd -t d --changed-before 1h "${tmpdir_prefix}" "${tmpdir_root}" -x rm -r {}
  TMPDIR="$(mktemp --directory --tmpdir=${tmpdir_root} "${tmpdir_pattern}")"
  export TMPDIR
fi

main() {
# btb check and copy, not really needed though
if [ ! -d "3" ]; then ./btb init || return 1; fi
# yandl check and copy
if [ ! -d "4" ]; then ./yandl init || return 1; fi
cp -v "4/01 Modular/"* . || return 1
cp -v "4/02 Merged/"* . || return 1

tlog="$(mktemp)"
if [ "${1}" = "1" ] || [ -z "${1}" ]; then
  cp -v "../../mods/Patches/FMUniqueItemsPFP/0/FM - Unique Items v1.0 - PfP.ESP" . 
  if [ -d "01 FM Unique Items - Replacement Patch" ]; then rm -r "01 FM Unique Items - Replacement Patch"; fi
  ./01_avp_BTBGI-NE_FM_Unique_Items.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi
if [ "${1}" = "2" ] || [ -z "${1}" ]; then
  if [ -d "02 No MWSE - Modular" ]; then rm -r "02 No MWSE - Modular"; fi
  if [ -d "02 No MWSE - Merged" ]; then rm -r "02 No MWSE - Merged"; fi
  ./02_avp_BTBGI-No_MWSE-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  ./02_avp_BTBGI-No_MWSE-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi
if [ "${1}" = "3" ] || [ -z "${1}" ]; then
  if [ -d "03 Various - Modular" ]; then rm -r "03 Various - Modular"; fi
  if [ -d "03 Various - Merged" ]; then rm -r "03 Various - Merged"; fi
  ./03_avp_BTBGI-NE_Various-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  ./03_avp_BTBGI-NE_Various-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi
if [ "${1}" = "4" ] || [ -z "${1}" ]; then
  if [ -d "04 Misc" ]; then rm -r "04 Misc"; fi
  ./04_avp_BTBGI-NE_Misc.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi
if [ "${1}" = "5" ] || [ -z "${1}" ]; then
  if [ -d "05 Sensible Races and Birthsigns - Custom edit" ]; then rm -r "05 Sensible Races and Birthsigns - Custom edit"; fi
  ./05_avp_BTBGI-NE_Sensible_Races_and_Birthsigns-Custom_edit.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi
if [ "${1}" = "6" ] || [ -z "${1}" ]; then
  if [ -d "06 Factions - Modular" ]; then rm -r "06 Factions - Modular"; fi
  if [ -d "06 Factions - Merged" ]; then rm -r "06 Factions - Merged"; fi
  if [ -d "06 Factions - More Exclusive Factions - Modular" ]; then rm -r "06 Factions - More Exclusive Factions - Modular"; fi
  if [ -d "06 Factions - More Exclusive Factions - Merged" ]; then rm -r "06 Factions - More Exclusive Factions - Merged"; fi
  if [ -d "06 NCGD - MBSP - Modular" ]; then rm -r "06 NCGD - MBSP - Modular"; fi
  if [ -d "06 NCGD - MBSP - Merged" ]; then rm -r "06 NCGD - MBSP - Merged"; fi
  if [ -d "06 NCGD - MBSP - More Exclusive Factions - Modular" ]; then rm -r "06 NCGD - MBSP - More Exclusive Factions - Modular"; fi
  if [ -d "06 NCGD - MBSP - More Exclusive Factions - Merged" ]; then rm -r "06 NCGD - MBSP - More Exclusive Factions - Merged"; fi
  ./06_avp_BTBGI-NE_Factions-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  ./06_avp_BTBGI-NE_Factions-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  ./06_avp_BTBGI-NE_NCGD-MBSP.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi
if [ "${1}" = "7" ] || [ -z "${1}" ]; then
  if [ -d "07 Game Settings - Modular" ]; then rm -r "07 Game Settings - Modular"; fi
  if [ -d "07 Game Settings - Merged" ]; then rm -r "07 Game Settings - Merged"; fi
  if [ -d "97 Game Settings - Merged" ]; then rm -r "97 Game Settings - Merged"; fi
  ./07_avp_BTBGI-NE_Game_Settings.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi

# copy required files
if [ "${1}" = "8" ] || [ -z "${1}" ]; then

  if [ "${1}" = "8" ]; then
    cp -v "3/03 Various - Modular/avp BTBGI-NE - Beware the Sixth House.esp" . || return 1
  else
    cp -v "03 Various - Modular/avp BTBGI-NE - Beware the Sixth House.esp" . || return 1
  fi

  if [ -d "08 Creature Buffs - Modular" ]; then rm -r "08 Creature Buffs - Modular"; fi
  ./08_avp_BTBGI-NE_Creature_Buffs-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  if [ -d "08 Creature Buffs - Merged" ]; then rm -r "08 Creature Buffs - Merged"; fi
  ./08_avp_BTBGI-NE_Creature_Buffs-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  if [ -d "08 Creature Buffs - No Disease Labels - Modular" ]; then rm -r "08 Creature Buffs - No Disease Labels - Modular"; fi
  ./08_avp_BTBGI-NE_Creature_Buffs-NDL-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  if [ -d "08 Creature Buffs - No Disease Labels - Merged" ]; then rm -r "08 Creature Buffs - No Disease Labels - Merged"; fi
  ./08_avp_BTBGI-NE_Creature_Buffs-NDL-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  if [ -d "08 No Creature Buffs - Modular" ]; then rm -r "08 No Creature Buffs - Modular"; fi
  if [ -d "08 No Creature Buffs - Merged" ]; then rm -r "08 No Creature Buffs - Merged"; fi
  ./08_avp_BTBGI-NE_No_Creature_Buffs-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  ./08_avp_BTBGI-NE_No_Creature_Buffs-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  if [ -d "08 No Creature Buffs - No Disease Labels - Modular" ]; then rm -r "08 No Creature Buffs - No Disease Labels - Modular"; fi
  if [ -d "08 No Creature Buffs - No Disease Labels - Merged" ]; then rm -r "08 No Creature Buffs - No Disease Labels - Merged"; fi
  ./08_avp_BTBGI-NE_No_Creature_Buffs-NDL-Modular.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
  ./08_avp_BTBGI-NE_No_Creature_Buffs-NDL-Merged.sh 2>&1 | tee -a "${tlog}"; if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi
fi

if rg -i 'error|warning|not modified' "${tlog}"; then echo "error"; return 1; fi

printf '\n\n\tCOMPARE AND MOVE:\n\n'
if [ "${1}" = "1" ] || [ -z "${1}" ]; then
  fd . -t f "3/01 FM Unique Items - Replacement Patch" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "2" ] || [ -z "${1}" ]; then
  fd . -t f "3/02 No MWSE - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/02 No MWSE - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "3" ] || [ -z "${1}" ]; then
  fd . -t f "3/03 Various - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/03 Various - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "4" ] || [ -z "${1}" ]; then
  fd . -t f "3/04 Misc" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "5" ] || [ -z "${1}" ]; then
  fd . -t f "3/05 Sensible Races and Birthsigns - Custom edit" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "6" ] || [ -z "${1}" ]; then
  fd . -t f "3/06 Factions - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 Factions - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 Factions - More Exclusive Factions - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 Factions - More Exclusive Factions - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 NCGD - MBSP - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 NCGD - MBSP - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 NCGD - MBSP - More Exclusive Factions - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/06 NCGD - MBSP - More Exclusive Factions - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "7" ] || [ -z "${1}" ]; then
  fd . -t f "3/07 Game Settings - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/07 Game Settings - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/97 Game Settings - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
if [ "${1}" = "8" ] || [ -z "${1}" ]; then
  fd . -t f "3/08 Creature Buffs - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 Creature Buffs - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 Creature Buffs - No Disease Labels - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 Creature Buffs - No Disease Labels - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 No Creature Buffs - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 No Creature Buffs - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 No Creature Buffs - No Disease Labels - Modular" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
  fd . -t f "3/08 No Creature Buffs - No Disease Labels - Merged" -x sh -c 'if ! diff -q "${0}" "${0#*/}" >/dev/null 2>&1; then cp -v "${0#*/}" "${0}"; fi' "{}"
fi
}
main "$@" || { echo "error"; exit 1; }
