#!/usr/bin/env bash
# vim: nowrap
#
# Changelog:
# 2023-04-21
#   - Rename "Assarnud and Mat Overhaul" patch to "Mines and Caverns"
#
# 2022-12-23
#   - Add "Frostwind" patches
#
# 2022-12-19
#   - Add "SecretsOfTheCrystalCity" patch
#   - Restructure patch
#
# 2022-07-15
#   - Rename and remake patch
#   - Move old version to "old" dir
#   - Remove Glowing Flames patch because it's not longer needed
#   - Add "New Ilunibi" patch
#   - Add "Assarnud and Mat Overhaul" patch
#
# 2021-09-12
#   - Forward TLAD Necro Edit v3.1 time changes
#

tes3cmd="./tes3cmd"

source_tlad_still_nc="TLAD Full - Still - Necro Colors.esp"
source_tlad_flick_nc="TLAD Lights - Logical Flicker - Necro Colors.esp"
source_noct_moth_tld="Nocturnal Moths - TLD.ESP"

result001="TLAD - SecretsOfTheCrystalCity.esp"
result002="TLAD - New Ilunibi.esp"
result003="TLAD - Mines and Caverns.esp"
result011="TLAD Still Necro Colors - Nocturnal Moths.esp"
result012="TLAD LogFl Necro Colors - Nocturnal Moths.esp"
result021="TLAD Still Necro Colors - Merged Patches.esp"
result022="TLAD LogFl Necro Colors - Merged Patches.esp"
result031="TLAD Still Necro Colors - Frostwind.esp"
result041="TLAD Still Necro Colors - Frostwind - Merged Patches.esp"
result042="TLAD LogFl Necro Colors - Frostwind - Merged Patches.esp"

# copy Logical Flicker because it's not active for me
cp -v "../../mods/Gameplay/TrueLightsAndDarknessNecroEdit/0/01 Modular Plugins/TLAD Lights - Logical Flicker - Necro Colors.esp" .

# 001. Create additional patch for "TLAD" and "Secrets of the Crystal City"
result="${result001}"
${tes3cmd} dump "${source_tlad_still_nc}" --raw-with-header "${result}" --type CELL --exact-id "massama cave"
${tes3cmd} modify "${result}" --hide-backups --type CELL --exact-id "massama cave" --run "\$R->set({i=>[1], f=>'Flags'}, '1')"

# 002. Create additional patch for "TLAD" and "New Ilunibi"
result="${result002}"
${tes3cmd} dump "${source_tlad_still_nc}" --raw-with-header "${result}" --type CELL --exact-id "ilunibi, Marowak's Spine"
${tes3cmd} modify "${result}" --hide-backups --type CELL --exact-id "ilunibi, Marowak's Spine" --run "\$R->set({i=>[1], f=>'Flags'}, '3')"

# 003. Create additional patch for "TLAD" and "Mines and Caverns"
result="${result003}"
${tes3cmd} dump "${source_tlad_still_nc}" --raw-with-header "${result}" --type CELL --exact-id "Mat"
${tes3cmd} modify "${result}" --hide-backups --type CELL --exact-id "Mat" --run "\$R->set({i=>[1], f=>'Flags'}, '3')"

# 011. Create additional patch for "TLAD Full - Still - Necro Colors" and "Nocturnal Moths - TLD"
result="${result011}"

${tes3cmd} dump "${source_tlad_still_nc}" --raw-with-header "${result}" --type LIGH\
  --exact-id "light_com_lantern_01"\
  --exact-id "light_com_lantern_01_128"\
  --exact-id "light_com_lantern_02"\
  --exact-id "light_de_lantern_03_200_r"\
  --exact-id "light_de_lantern_13_128"\
  --exact-id "light_de_paper_lantern_01"\
  --exact-id "light_de_paper_lantern_01_200"\
  --exact-id "light_de_paper_lantern_02"\
  --exact-id "light_de_paper_lantern_03"

${tes3cmd} dump "${source_noct_moth_tld}" --raw "${result}" --type LIGH\
  --exact-id "light_com_lantern_02_128"

${tes3cmd} header "${result}" --hide-backups --update-record-count

${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_01"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'})), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_01_128"      --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'})), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_02"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'})), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_lantern_03_200_r"     --run "\$R->set({t=>'SCRI', f=>'Script'}, 'R0_mthplcr_de01r_scr')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_lantern_13_128"       --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de01_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_01"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_01_200" --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_02"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_03"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"

# 012. Create additional patch for "TLAD Lights - Logical Flicker - Necro Colors" and "Nocturnal Moths - TLD"
result="${result012}"

${tes3cmd} dump "${source_tlad_flick_nc}" --raw-with-header "${result}" --type LIGH\
  --exact-id "light_de_lantern_03_200_r"\
  --exact-id "light_de_lantern_13_128"\
  --exact-id "light_de_paper_lantern_01"\
  --exact-id "light_de_paper_lantern_01_200"\
  --exact-id "light_de_paper_lantern_02"\
  --exact-id "light_de_paper_lantern_03"

${tes3cmd} dump "${source_noct_moth_tld}" --raw "${result}" --type LIGH\
  --exact-id "light_com_lantern_01"\
  --exact-id "light_com_lantern_01_128"\
  --exact-id "light_com_lantern_02"\
  --exact-id "light_com_lantern_02_128"

${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_01"          --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400'), \$R->set({t=>'LHDT', f=>'Radius'}, '600')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_01_128"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_02"          --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400'), \$R->set({t=>'LHDT', f=>'Radius'}, '600')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_lantern_03_200_r"     --run "\$R->set({t=>'SCRI', f=>'Script'}, 'R0_mthplcr_de01r_scr')"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_lantern_13_128"       --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de01_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_01"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_01_200" --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_02"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result}" --hide-backups --exact-id "light_de_paper_lantern_03"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"

# 021. Create merged patch for "TLAD Full - Still - Necro Colors" and everything else
result="${result021}"

${tes3cmd} dump "${result011}" --raw-with-header "${result}" --type LIGH
${tes3cmd} dump "${result001}" --raw "${result}" --type CELL
${tes3cmd} dump "${result002}" --raw "${result}" --type CELL
${tes3cmd} dump "${result003}" --raw "${result}" --type CELL
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 022. Create merged patch for "TLAD Full - Logical Flicker - Necro Colors" and everything else
result="${result022}"

${tes3cmd} dump "${result012}" --raw-with-header "${result}" --type LIGH
${tes3cmd} dump "${result001}" --raw "${result}" --type CELL
${tes3cmd} dump "${result002}" --raw "${result}" --type CELL
${tes3cmd} dump "${result003}" --raw "${result}" --type CELL
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 031. Frostwind
result="${result031}"
${tes3cmd} dump "${source_tlad_still_nc}" --raw-with-header "${result}" --type LIGH\
  --exact-id "flame light"\
  --exact-id "flame light_64"\
  --exact-id "furn_de_firepit_f"\
  --exact-id "furn_de_firepit_f_01"\
  --exact-id "furn_de_firepit_f_01_400"\
  --exact-id "furn_de_firepit_f_128"\
  --exact-id "furn_de_firepit_f_200"\
  --exact-id "furn_de_firepit_f_323"\
  --exact-id "furn_de_firepit_f_400"\
  --exact-id "light_logpile"\
  --exact-id "light_logpile10"\
  --exact-id "light_logpile_128"\
  --exact-id "light_logpile_177"\
  --exact-id "light_pitfire00"\
  --exact-id "light_pitfire01"
${tes3cmd} modify "${result}" --hide-backups\
  --exact-id "flame light"\
  --exact-id "flame light_64"\
  --exact-id "furn_de_firepit_f"\
  --exact-id "furn_de_firepit_f_01"\
  --exact-id "furn_de_firepit_f_01_400"\
  --exact-id "furn_de_firepit_f_128"\
  --exact-id "furn_de_firepit_f_200"\
  --exact-id "furn_de_firepit_f_323"\
  --exact-id "furn_de_firepit_f_400"\
  --exact-id "light_logpile"\
  --exact-id "light_logpile10"\
  --exact-id "light_logpile_128"\
  --exact-id "light_logpile_177"\
  --exact-id "light_pitfire00"\
  --exact-id "light_pitfire01"\
  --run "\$R->append(LIGH::SCRI->new({script=>'a_static_firepit'}))"

# 041. Create merged patch for "TLAD Full - Still - Necro Colors" and everything else
result="${result041}"

${tes3cmd} dump "${result021}" --raw-with-header "${result}" --type LIGH --type CELL
${tes3cmd} dump "${result031}" --raw "${result}" --type LIGH
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 042. Create merged patch for "TLAD Full - Logical Flicker - Necro Colors" and everything else
result="${result042}"

${tes3cmd} dump "${result022}" --raw-with-header "${result}" --type LIGH --type CELL
${tes3cmd} dump "${result031}" --raw "${result}" --type LIGH
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 99. Create directory structure and move files
mkdir -pv "TLAD_Necro_Edit_Patch/01 Secrets of the Crystal City"
mv -v "${result001}" "TLAD_Necro_Edit_Patch/01 Secrets of the Crystal City"/
mkdir -pv "TLAD_Necro_Edit_Patch/02 New Ilunibi"
mv -v "${result002}" "TLAD_Necro_Edit_Patch/02 New Ilunibi"/
mkdir -pv "TLAD_Necro_Edit_Patch/03 Mines and Caverns"
mv -v "${result003}" "TLAD_Necro_Edit_Patch/03 Mines and Caverns"/
mkdir -pv "TLAD_Necro_Edit_Patch/10 Nocturnal Moths - Still"
mv -v "${result011}" "TLAD_Necro_Edit_Patch/10 Nocturnal Moths - Still"/
mkdir -pv "TLAD_Necro_Edit_Patch/10 Nocturnal Moths - Flicker"
mv -v "${result012}" "TLAD_Necro_Edit_Patch/10 Nocturnal Moths - Flicker"/
mkdir -pv "TLAD_Necro_Edit_Patch/20 Merged - Still"
mv -v "${result021}" "TLAD_Necro_Edit_Patch/20 Merged - Still"/
mkdir -pv "TLAD_Necro_Edit_Patch/20 Merged - Flicker"
mv -v "${result022}" "TLAD_Necro_Edit_Patch/20 Merged - Flicker"/
mkdir -pv "TLAD_Necro_Edit_Patch/30 Frostwind"
mv -v "${result031}" "TLAD_Necro_Edit_Patch/30 Frostwind"/
mkdir -pv "TLAD_Necro_Edit_Patch/40 Merged - Frostwind - Still"
mv -v "${result041}" "TLAD_Necro_Edit_Patch/40 Merged - Frostwind - Still"/
mkdir -pv "TLAD_Necro_Edit_Patch/40 Merged - Frostwind - Flicker"
mv -v "${result042}" "TLAD_Necro_Edit_Patch/40 Merged - Frostwind - Flicker"/
