#!/usr/bin/env bash
# Changelog:
# 2023-11-03
#   - Add Tamriel_Data patch for Tamriel_Data v10(instead of previous TR_Factions patch)
#
# 2022-11-27
#   - Add TR_Factions patch for Tamriel Rebuilt v22.11
#
# 2022-05-11
#   - Remove Special Occasions Patch due to being improved by author
#
# 2022-05-09
#   - Add Raym's Vampire Rumors Patch
#
# 2022-05-08
#   - Remove QCVL patch, it should not be here
#   - Add Special Occasions Patch
#   - Add Vampire Clans - Clutter Monkey Overhaul Patch
#   - Remake Merged version
#

tes3cmd="./tes3cmd"

# list patch sources
source_quarra="quarraclan_refreshed.esp"
source_pfp="Patch for Purists.esm"
source_raym="Raym's Vampire Rumors.esp"
# source_tr_factions="TR_Factions.esp"
source_tr_factions="Tamriel_Data.esm"

# 1. Factions

result="quarraclan_refreshed - Faction Stats Override.esp"
source1="${result}"

$tes3cmd dump "${source_quarra}" --raw-with-header "${result}" --type FACT --exact-id "clan quarra"

# 2. TR_Factions

result="quarraclan_refreshed - Tamriel_Data.esp"
source2="${result}"

$tes3cmd dump "${source_tr_factions}" --raw-with-header "${result}" --type FACT --exact-id "clan quarra"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "clan quarra" --run "
  \$R->set({t=>'FADT', f=>'At1_9'},  '80'), \
  \$R->set({t=>'FADT', f=>'At1_10'}, '90'), \
  \$R->set({t=>'FADT', f=>'Sk1_2'},  '20'), \
  \$R->set({t=>'FADT', f=>'Sk1_3'},  '30'), \
  \$R->set({t=>'FADT', f=>'Sk1_4'},  '40'), \
  \$R->set({t=>'FADT', f=>'Sk1_5'},  '50'), \
  \$R->set({t=>'FADT', f=>'Sk1_6'},  '60'), \
  \$R->set({t=>'FADT', f=>'Sk1_7'},  '70'), \
  \$R->set({t=>'FADT', f=>'Sk1_8'},  '80'), \
  \$R->set({t=>'FADT', f=>'Sk1_9'},  '100'), \
  \$R->set({t=>'FADT', f=>'Sk1_10'}, '100'), \
  \$R->set({t=>'FADT', f=>'Sk2_2'},  '15'), \
  \$R->set({t=>'FADT', f=>'Sk2_3'},  '25'), \
  \$R->set({t=>'FADT', f=>'Sk2_4'},  '30'), \
  \$R->set({t=>'FADT', f=>'Sk2_5'},  '35'), \
  \$R->set({t=>'FADT', f=>'Sk2_6'},  '40'), \
  \$R->set({t=>'FADT', f=>'Sk2_7'},  '50'), \
  \$R->set({t=>'FADT', f=>'Sk2_8'},  '60'), \
  \$R->set({t=>'FADT', f=>'Sk2_9'},  '80'), \
  \$R->set({t=>'FADT', f=>'Sk2_10'},  '100'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},  '110'), \
  \$R->set({t=>'FADT', f=>'Rep_10'}, '150'), \
  \$R->set({t=>'FADT', f=>'sk_4'},  '8'), \
  \$R->set({t=>'FADT', f=>'sk_5'},  '5'), \
  \$R->set({t=>'FADT', f=>'sk_6'},  '3')"

# 3. Morrowind Anti-Cheese

result="quarraclan_refreshed - Morrowind Anti-Cheese.esp"
source3="${result}"

$tes3cmd dump "${source_quarra}" --raw-with-header "${result}" --type NPC_ \
  --exact-id "volrina quarra" \
  --exact-id "siri"
$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "volrina quarra" --replace '/^daedric claymore$/glass poisonsword/'
$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "siri"           --replace '/^ebony_cuirass$/steel_cuirass/'
$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "siri"           --replace '/^ebony_boots$/steel_boots/'
$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "siri"           --replace '/^ebony_bracer_right$/iron_bracer_right/'
$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "siri"           --replace '/^ebony_bracer_left$/iron_bracer_left/'

# 4. Vampire Clans - Clutter Monkey Overhaul

result="quarraclan_refreshed - Vampire Clans - Clutter Monkey Overhaul.esp"
source4="${result}"

$tes3cmd dump "${source_quarra}" --raw-with-header "${result}" --type CELL\
  --exact-id "Druscashti, Lower Level"\
  --instance-match "Name:varnis stieve"
$tes3cmd modify "${result}" --hide-backups --type TES3\
  --run "\
  \$R->append(TES3::MAST->new({'master', 'Morrowind.esm'})),\
  \$R->append(TES3::DATA->new({'length', '79837557'})),\
  \$R->append(TES3::MAST->new({'master', '${source_quarra}'})),\
  \$R->append(TES3::DATA->new({'length', '1987098'}))"
$tes3cmd header "${result}" --hide-backups --synchronize
# from: -78.7532 -936.475
# to:   -163.682 -1008.9
$tes3cmd modify "${result}" --hide-backups --type CELL\
  --exact-id "Druscashti, Lower Level"\
  --run "\
  \$R->set({i=>[4], f=>'X'}, '-163.7'),\
  \$R->set({i=>[4], f=>'Y'}, '-1008.9')"
$tes3cmd modify "${result}" --hide-backups\
  --type CELL\
  --exact-id "Druscashti, Lower Level"\
  --run "\$R->set({i=>[2], f=>'MastIdx'}, '2')"\

# 5. Raym's Vampire Rumors 

result="quarraclan_refreshed - Raym's Vampire Rumors.esp"
source5="${result}"

# greeting 2 comes from PfP patch

# topic greeting 2
$tes3cmd dump --raw-with-header "${result}" --type DIAL\
 --exact-id "greeting 2"\
 "${source_pfp}" 
# sinyaramen
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "23288195233207216141"\
 --exact-id "180039401103830822"\
 --exact-id "30079310442701032282"\
 "${source_pfp}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "2800538312175613916"\
 "${source_raym}" 
# irarak
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "2611018385464224428"\
 "${source_pfp}" 
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "30079310442701032282" --run "\
  \$R->set({t=>'NNAM',f=>'Next_ID'}, '2800538312175613916')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "2800538312175613916" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '30079310442701032282')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "2611018385464224428" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '442510890497329826')"

# vampires topic
$tes3cmd dump --raw "${result}" --type DIAL\
 --exact-id "vampires"\
 "${source_quarra}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "26303211681180729678"\
 "${source_quarra}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "31023252461875913303"\
 --exact-id "59652446472530099"\
 --exact-id "73464803109920450"\
 "${source_raym}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "43968095295613136"\
 "${source_quarra}" 

$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "26303211681180729678" --run "\
  \$R->set({t=>'NNAM',f=>'Next_ID'}, '31023252461875913303')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "31023252461875913303" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '26303211681180729678')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "43968095295613136" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '73464803109920450')"

# aundae topic
$tes3cmd dump --raw "${result}" --type DIAL\
 --exact-id "aundae"\
 "${source_raym}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "354677841240725887"\
 --exact-id "1726212673874022562"\
 "${source_raym}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "20592327572965816671"\
 "${source_quarra}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "1439182632961329964"\
 "${source_raym}" 

$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "1726212673874022562" --run "\
  \$R->set({t=>'NNAM',f=>'Next_ID'}, '20592327572965816671'),\
  \$R->append(INFO::SCVR->new({'index', '0', 'type', '4', 'function', 'JX', 'comparison', '4', 'name', 'QC_Aundae'})),\
  \$R->append(INFO::INTV->new({'compare_value', '20'}))"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "20592327572965816671" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '1726212673874022562'),\
  \$R->set({t=>'NNAM',f=>'Next_ID'}, '1439182632961329964')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "1439182632961329964" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '20592327572965816671')"

# topic quarra
$tes3cmd dump --raw "${result}" --type DIAL\
 --exact-id "quarra"\
 "${source_raym}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "24480637466627054"\
 "${source_raym}" 

# berne topic, same changes as aundae
$tes3cmd dump --raw "${result}" --type DIAL\
 --exact-id "berne"\
 "${source_raym}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "2710822319292754514"\
 --exact-id "320572372212111172"\
 "${source_raym}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "379712645296077057"\
 "${source_quarra}" 
$tes3cmd dump --raw "${result}" --type INFO\
 --exact-id "21464866666427800"\
 "${source_raym}" 

$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "320572372212111172" --run "\
  \$R->set({t=>'NNAM',f=>'Next_ID'}, '379712645296077057'),\
  \$R->append(INFO::SCVR->new({'index', '0', 'type', '4', 'function', 'JX', 'comparison', '4', 'name', 'QC_Berne'})),\
  \$R->append(INFO::INTV->new({'compare_value', '20'}))"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "379712645296077057" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '320572372212111172'),\
  \$R->set({t=>'NNAM',f=>'Next_ID'}, '21464866666427800')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "21464866666427800" --run "\
  \$R->set({t=>'PNAM',f=>'Prev_ID'}, '379712645296077057')"

# 99. Merged

result="quarraclan_refreshed - Merged Patches.esp"
if [ -f "${result}" ]; then rm -v "${result}"; fi

$tes3cmd dump "${source4}" --raw "${result}" --type TES3 --type CELL
$tes3cmd dump "${source2}" --raw "${result}" --type FACT
$tes3cmd dump "${source3}" --raw "${result}" --type NPC_
$tes3cmd dump "${source5}" --raw "${result}" --type DIAL --type INFO
$tes3cmd header "${result}" --hide-backups --synchronize

# 16337111081828622674 # fixed in bloodmoon

# Create dir and move files there

mkdir -v "02 Optional Patches - Modular"
mv -v \
  "${source1}"\
  "${source2}"\
  "${source3}"\
  "${source4}"\
  "${source5}"\
  "02 Optional Patches - Modular"/

mkdir -v "02 Optional Patches - Merged"
mv -v \
  "${result}"\
  "02 Optional Patches - Merged"/
