#!/usr/bin/env bash
tamriel_data="Tamriel_Data.esm"
tr_factions="TR_Factions.esp"
patch_dir="TD-TR_Factions-Patch"
merged_dir="${patch_dir}/01 Merged"
modular_dir="${patch_dir}/02 Modular"
td_suffix=" - Tamriel_Data Patch.esp"
tr_suffix=" - TR_Factions Patch.esp"
merged="Tamriel_Data - TR_Factions - Factions Patch - Merged.esp"
tes3cmd="./tes3cmd"
dump="$tes3cmd dump --raw-with-header"
sync="$tes3cmd header --synchronize --hide-backups"
log="log.log"

main() {
  true > "${log}"
  {
  if [ -d "${patch_dir}" ]; then rm -rv "${patch_dir}" || echo "error"; fi
  if [ -d "${modular_dir}" ]; then rm -rv "${modular_dir}" || echo "error"; fi
  mkdir -pv "${modular_dir}" || echo "error"
  if [ -d "${merged_dir}" ]; then rm -rv "${merged_dir}" || echo "error"; fi
  mkdir -pv "${merged_dir}" || echo "error"
  } 2>&1 | tee -a "${log}"; if rg -i 'error|warning|not modified' "${log}"; then echo "error"; return 1; fi


  origin="Census_and_Excise_Office_Faction.ESP"
  result="${origin%.*}${td_suffix}" 
  origin01="${modular_dir}/${result}"
  { $dump "${result}" --type fact --exact-id "census and excise" "${tamriel_data}"\
    && $sync "${result}" && mv -v "${result}" "${modular_dir}"
  } 2>&1 | tee -a "${log}"; if rg -i 'error|warning|not modified' "${log}"; then echo "error"; return 1; fi

  origin="Tel_Aruhn_Chronicles.ESP"
  result="${origin%.*}${td_suffix}" 
  origin02="${modular_dir}/${result}"
  { $dump "${result}" --type fact --exact-id "hands of almalexia" "${tamriel_data}"\
    && $sync "${result}" && mv -v "${result}" "${modular_dir}"
  } 2>&1 | tee -a "${log}"; if rg -i 'error|warning|not modified' "${log}"; then echo "error"; return 1; fi

  origin="OAAB Brother Junipers Twin Lamps.esp"
  result="${origin%.*}${td_suffix}" 
  origin03="${modular_dir}/${result}"
  { $dump "${result}" --type fact --exact-id "twin lamps" "${origin}"\
    && $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "twin lamps" --run "\
      \$R->append(FACT::ANAM->new({'faction', 'T_Mw_HouseDres'})), \
      \$R->append(FACT::INTV->new({'reaction', '-3'}))"\
    && $sync "${result}" && mv -v "${result}" "${modular_dir}"
  } 2>&1 | tee -a "${log}"; if rg -i 'error|warning|not modified' "${log}"; then echo "error"; return 1; fi

  origin="Vvardenfell Brotherhood.esp"
  result="${origin%.*}${tr_suffix}" 
  origin04="${modular_dir}/${result}"
  { $dump "${result}" --type fact --exact-id "morag tong" "${tr_factions}"\
    && $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "morag tong" --run "\
      \$R->append(FACT::ANAM->new({'faction', 'Vvardenfell Brotherhood'})), \
      \$R->append(FACT::INTV->new({'reaction', '-3'}))"\
    && $sync "${result}" && mv -v "${result}" "${modular_dir}"
  } 2>&1 | tee -a "${log}"; if rg -i 'error|warning|not modified' "${log}"; then echo "error"; return 1; fi

  { $dump "${merged}" --type fact "${origin01}"\
    && $tes3cmd dump --raw "${merged}" --type fact "${origin02}"\
    && $tes3cmd dump --raw "${merged}" --type fact "${origin03}"\
    && $tes3cmd dump --raw "${merged}" --type fact "${origin04}"\
    && $sync "${merged}"\
    && mv -v "${merged}" "${merged_dir}"
  } 2>&1 | tee -a "${log}"; if rg -i 'error|warning|not modified' "${log}"; then echo "error"; return 1; fi
}

main "@" || echo "Error"
