#!/usr/bin/env bash
# Changelog:
# 2021-01-10
#   - Remove OAAB_Grazelands changes due to not using it
# 2021-07-10
#   - Add Old Mournhold Overhaul
#   - Split into "Merged" and "Merged - Cell Changes"
# 2021-09-21
#   - Remove Persistent Corpse Disposal after making it more compatible
# 2021-10-16
#   - Fix weapon quantity
# 2021-12-19
#   - Add Wyrmhaven
# 2022-11-06
#   - Add Havish
#   - Switch to "Strike at the Camonna Tong" regular version instead of Fast
# 2021-12-14
#   - Remove Old Mournhold Overhaul
# 
# command used for formatting text: '<,'>EasyAlign 4/\-\-/r0
result="avp There Can Be Only One - Merged.esp"
source_strikecam="Strike at the Camonna Tong.ESP"
source_tribunalr="tribunal rebalance.ESP"
# source_oldmournh="OldMournholdOverhaul.ESP"
source_wyrmhaven="Wyrmhaven.esm"
source_havishesm="Havish.esm"
# will be used as a source for a patch with cell edits
source_merged_nc="${result}"
tes3cmd="./tes3cmd"

# 1. Strike at the Camonna Tong

$tes3cmd dump "${source_strikecam}" --raw-with-header "${result}" --type NPC_ \
  --exact-id "arver rethul"

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "arver rethul"\
  --replace '/daedric longsword$/silver longsword/'

# 2. tribunal rebalance

$tes3cmd dump "${source_tribunalr}" --raw "${result}" --type NPC_ \
  --exact-id "king hlaalu helseth"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "king hlaalu helseth"\
  --replace '/daedric dai-katana$/glass claymore/'

# # 3. Old Mournhold Overhaul

# # 3.1. Creature edit

# $tes3cmd dump "${source_oldmournh}" --raw "${result}" --type CREA \
#   --exact-id "dagoth_vyrdros"
# $tes3cmd header "${result}" --hide-backups --update-record-count
# $tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "dagoth_vyrdros"\
#   --replace '/daedric dagger$/ebony shortsword/'

# 4. Wyrmhaven

# 4.1. Creature edit

$tes3cmd dump "${source_wyrmhaven}" --raw "${result}" --type CREA \
  --exact-id "_wyrm_dremora01"\
  --exact-id "_wyrm_dremora02"

$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "_wyrm_dremora01" --run "\$R->delete({i=>[7..8]})"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "_wyrm_dremora01" --run "\
 \$R->append(CREA::NPCS->new({'spell', 'necro_abl_bound_shield'})), \
 \$R->append(CREA::NPCS->new({'spell', 'necro_abl_bound_spear'}))"

$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "_wyrm_dremora02" --run "\$R->delete({i=>[8]})"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "_wyrm_dremora02" --run "\
 \$R->append(CREA::NPCS->new({'spell', 'necro_abl_bound_shield'}))"

# 4.2. NPC edit
$tes3cmd dump "${source_wyrmhaven}" --raw "${result}" --type NPC_ \
  --exact-id "_wyrm_rosalind"\
  --exact-id "_wyrm_manfred"\
  --exact-id "_wyrm_oliver"

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "_wyrm_rosalind"\
  --replace '/daedric longsword$/t_com_daedsteel_longsword_01/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "_wyrm_manfred"\
  --replace '/daedric shortsword$/t_com_daedsteel_shortsword_01/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "_wyrm_oliver"\
  --replace '/daedric shortsword$/t_com_daedsteel_shortsword_01/'

# 4.3. CONT edit
$tes3cmd dump "${source_wyrmhaven}" --raw "${result}" --type CONT \
  --exact-id "_wyrm_burial_corpse"

$tes3cmd modify "${result}" --hide-backups --type CONT --exact-id "_wyrm_burial_corpse"\
  --replace '/daedric dagger$/t_com_daedsteel_dagger_01/'

# 5. Havish

# 5.1. Creature edit

$tes3cmd dump "${source_havishesm}" --raw "${result}" --type CREA \
  --exact-id "zh_grim_lich_alnestor"\
  --exact-id "zh_grim_lich_pesquil"

$tes3cmd modify "${result}" --hide-backups --type CREA \
  --exact-id "zh_grim_lich_alnestor" \
  --exact-id "zh_grim_lich_pesquil" \
  --replace '/daedric staff$/ebony staff/'

# 5.2. NPC edit
$tes3cmd dump "${source_havishesm}" --raw "${result}" --type NPC_ \
  --exact-id "zh_bank_guard_1"\
  --exact-id "zh_demilich"\
  --exact-id "zh_fargoth"\
  --exact-id "zh_hermit"\
  --exact-id "zh_mg_steward"\
  --exact-id "zh_mg_vampire"\
  --exact-id "zh_t_grebash gra-ghorak"\
  --exact-id "zh_tg_cugius"\
  --exact-id "zh_tg_eleret"\
  --exact-id "zh_tg_erdenis"

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_bank_guard_1"\
  --replace '/daedric battle axe$/BM nordic silver battleaxe/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_demilich"\
  --replace '/daedric_cuirass$/BM_NordicMail_cuirass/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_fargoth"\
  --replace '/daedric warhammer$/orcish warhammer/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_hermit"\
  --replace '/daedric staff$/dreugh staff/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_mg_steward"\
  --replace '/daedric war axe$/BM nordic silver axe/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_mg_vampire"\
  --replace '/daedric tanto$/glass dagger/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_t_grebash gra-ghorak"\
  --replace '/daedric warhammer$/orcish warhammer/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_tg_cugius"\
  --replace '/daedric mace$/BM nordic silver mace/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_tg_eleret"\
  --replace '/daedric wakizashi$/ebony shortsword/'

$tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "zh_tg_erdenis"\
  --replace '/daedric dagger$/BM nordic silver dagger/'

# 5.3. CONT edit
$tes3cmd dump "${source_havishesm}" --raw "${result}" --type CONT \
  --exact-id "zh_chest_bowyer"

$tes3cmd delete "${result}" --hide-backups --type CONT --exact-id "zh_chest_bowyer" --sub-match "Object:daedric"

# 3.2. Cells edit

result="avp There Can Be Only One - Merged - Cell Changes.esp"
$tes3cmd dump "${source_merged_nc}" --raw-with-header "${result}" --type NPC_ --type CREA --type CONT

# $tes3cmd dump "${source_oldmournh}" --raw "${result}" --type CELL --instance-match "Name:daedric .*"
$tes3cmd dump "${source_wyrmhaven}" --raw "${result}" --type CELL --instance-match "Name:daedric [ckls].*"\
  --exact-id "wyrmhaven, governor's manor: private quarters"\
  --exact-id "wyrmhaven, manfred's hideout"\
  --exact-id "chimeranyon, burial caverns"\
  --exact-id "wyrmhaven, forgotten cellar"\
  --exact-id "wyrmhaven, benji: fletcher"
$tes3cmd dump "${source_havishesm}" --raw "${result}" --type CELL --instance-match "Name:daedric dagger"\
  --exact-id "kathaer island region (-36, 24)"\
  --exact-id "havish,  nitteric ramiele's house"
$tes3cmd dump "${source_havishesm}" --raw "${result}" --type CELL --instance-match "Name:daedric long bow"\
  --exact-id "havish, elwing's superior bows"

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

$tes3cmd delete "${result}" --hide-backups --type CELL --sub-match "Water_Height:"
$tes3cmd delete "${result}" --hide-backups --type CELL --sub-match "Ambient:" 

$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', 'Tribunal.esm'})), \
  \$R->append(TES3::DATA->new({'length', '4565686'})), \
  \$R->append(TES3::MAST->new({'master', 'Bloodmoon.esm'})), \
  \$R->append(TES3::DATA->new({'length', '9631798'})), \
  \$R->append(TES3::MAST->new({'master', 'Tamriel_Data.esm'})), \
  \$R->append(TES3::DATA->new({'length', '10684550'})), \
  \$R->append(TES3::MAST->new({'master', '${source_wyrmhaven}'})), \
  \$R->append(TES3::DATA->new({'length', '5400614'})), \
  \$R->append(TES3::MAST->new({'master', '${source_havishesm}'})), \
  \$R->append(TES3::DATA->new({'length', '5400614'}))"
  # \$R->append(TES3::MAST->new({'master', '${source_oldmournh}'})), \
  # \$R->append(TES3::DATA->new({'length', '147754'})), \
$tes3cmd header "${result}" --hide-backups --synchronize

# $tes3cmd modify "${result}" --hide-backups --type CELL --sub-match "Name:daedric club"           --run " \
#   \$R->set({i=>[3], f=>'Name'}, 'dreugh club'), \
#   \$R->set({i=>[2], f=>'MastIdx'}, '4')"

# $tes3cmd modify "${result}" --hide-backups --type CELL --sub-match "Name:daedric dagger OM necr" --run " \
#   \$R->set({i=>[3], f=>'Name'}, 'glass dagger'), \
#   \$R->set({i=>[2], f=>'MastIdx'}, '4')"

# 4.4. Cells edit

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "wyrmhaven, governor's manor: private quarters"\
  --sub-match "Name:daedric katana" --run " \
  \$R->set({i=>[3], f=>'Name'}, 't_com_adamant_katana_01'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '5'), \
  \$R->delete({i=>[5]})"

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "wyrmhaven, manfred's hideout"\
  --sub-match "Name:daedric longsword" --run " \
  \$R->set({i=>[3], f=>'Name'}, 't_bre_artisan_longsword_01'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '5'), \
  \$R->delete({i=>[5]})"

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "chimeranyon, burial caverns"\
  --sub-match "Name:daedric shortsword" --run " \
  \$R->set({i=>[3], f=>'Name'}, 't_he_direnni_shortsword_01'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '5')"

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "wyrmhaven, forgotten cellar"\
  --sub-match "Name:daedric claymore" --run " \
  \$R->set({i=>[3], f=>'Name'}, 't_com_daedsteel_claymore_01'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '5')"

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "wyrmhaven, benji: fletcher"\
  --sub-match "Name:daedric long bow" --run " \
  \$R->set({i=>[3], f=>'Name'}, 't_com_adamant_bow_01'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '5'), \
  \$R->delete({i=>[5]})"

# 5.4. Cells edit

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "kathaer island region (-36, 24)"\
  --sub-match "Name:daedric dagger" --run " \
  \$R->set({i=>[4], f=>'Name'}, 'glass dagger'), \
  \$R->set({i=>[3], f=>'MastIdx'}, '6')"

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "havish,  nitteric ramiele's house"\
  --sub-match "Name:daedric dagger" --run " \
  \$R->set({i=>[3], f=>'Name'}, 'BM nordic silver dagger'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '6')"

$tes3cmd modify "${result}" --hide-backups --type CELL --exact-id "havish, elwing's superior bows"\
  --sub-match "Name:daedric long bow" --run " \
  \$R->set({i=>[3], f=>'Name'}, 'bonemold long bow'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '6'), \
  \$R->delete({i=>[5]})"

# Change: 2021-01-10 - Remove OAAB_Grazelands changes due to not using it                            
# $tes3cmd dump "OAAB_Grazelands.ESP" --raw-with-header "${result}" --type NPC_ \
#   --exact-id "turedus talanian"
# $tes3cmd modify "${result}" --hide-backups --type NPC_ --exact-id "turedus talanian"             --run " \
#   \$R->set({i=>[12]}, 'imperial left gauntlet')"

# Create dir and move files there
mkdir -v "01 Merged"
mv -v "avp There Can Be Only One - Merged.esp" "01 Merged"/

mkdir -v "01 Merged - Cell Changes"
mv -v "avp There Can Be Only One - Merged - Cell Changes.esp" "01 Merged - Cell Changes"/
