#!/usr/bin/env bash
# vim: nowrap

tes3cmd="./tes3cmd"
dir_patch="ReputationFixesPatch"

# 0001. Persistent Corpse Disposal
 
source_pcd="PCD_Gnisis-Docks_Grandfather-Frost_CCM-Guard-Patrols.esp"
result0001_pcd="RF-Persistent_Corpse_Disposal.esp"
result="${result0001_pcd}"
${tes3cmd} dump "${source_pcd}" --raw-with-header "${result}" --type DIAL --exact-id "nads tharen"
${tes3cmd} dump "${source_pcd}" --raw "${result}" --type INFO --exact-id "24802279512844221153"
${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'}))"

${tes3cmd} modify "${result}" --hide-backups --type INFO --exact-id "24802279512844221153" --run "\$R->set({t=>'BNAM', f=>'Result'},\
'Journal TG_BrotherThief 110
Player->RemoveItem key_tgbt 1
\"stacey\"->AddItem key_tgbt 1
player->modReputation 1
Player->AddItem Gold_001 1000
ModPCFacRep 5 \"Thieves Guild\"
ModDisposition 15
\"nads tharen\"->disable')"

# 0002. Imperial Legion Expansion
 
source_ile="Imperial Legion Expansion.esp"
result0002_ile="RF-Imperial_Legion_Expansion.esp"
result="${result0002_ile}"
${tes3cmd} dump "${source_ile}" --raw-with-header "${result}" --type DIAL --exact-id "sorkvild the raven"
${tes3cmd} dump "${source_ile}" --raw "${result}" --type INFO --exact-id "32709198381821212557"
${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'}))"

${tes3cmd} modify "${result}" --hide-backups --type INFO --exact-id "32709198381821212557" --run "\$R->set({t=>'BNAM', f=>'Result'},\
'Journal IL_Necromancer 100
ModPCFacRep 5 \"Imperial Legion\"
player->modReputation 1
ModDisposition 10')"

# 0003. Mournhold Courtiers
 
source_moc="Mournhold Courtiers.esp"
result0003_moc="RF-Mournhold_Courtiers.esp"
result="${result0003_moc}"
${tes3cmd} dump "${source_moc}" --raw-with-header "${result}" --type DIAL --exact-id "fight my champion"
${tes3cmd} dump "${source_moc}" --raw "${result}" --type INFO --exact-id "14707155682221223996"
${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'}))"

${tes3cmd} modify "${result}" --hide-backups --type INFO --exact-id "14707155682221223996" --run "\$R->set({t=>'BNAM', f=>'Result'},\
'Player->AddItem \"glass dagger_symmachus_unique\" 1
RemoveItem \"glass dagger_symmachus_unique\" 1
player->modReputation 1
StartScript HelsMoveScript
Journal TR_Champion 80
_hlaalurep->Enable')"

# 0004. Creeping Blight

# Here it just takes previously edited and recompiled script and recreates plugin recursively :-)

result0004_crb="RF-Creeping_Blight.esp"
result="${result0004_crb}"

source_crb="RF-Creeping_Blight-Recompiled_Script.esp"
cp -v "../../mods/Patches/${dir_patch}/0/04 Creeping Blight/${result}" "${source_crb}"

${tes3cmd} dump "${source_crb}" --raw-with-header "${result}" --type SCPT --exact-id "tr_weathmachine"

${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'}))"

${tes3cmd} header "${result}" --hide-backups --synchronize

# Actually real work was done with following command and then editing, saving and recompiling script in CS
#
# source_crb="Creeping Blight.esp"
#
# ${tes3cmd} dump "${source_crb}" --raw-with-header "${result}" --type SCPT\
#   --exact-id "tr_weathmachine"
#
# ${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'}))"
#
# ${tes3cmd} header "${result}" --hide-backups --synchronize
#
# (Edit, save, recompile in CS)
#
# insert following block and recompile the script
#
# if ( GetJournalIndex TR_ShowPower < 95 )
#  player->modReputation 1
# endif
#

# 0012. Persistent Corpse Disposal - Imperial Legion Expansion
 
result0012="RF-PCD-Imperial_Legion_Expansion.esp"
result="${result0012}"
${tes3cmd} dump "${result0001_pcd}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0002_ile}" --raw "${result}" --type DIAL --type INFO
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0013. Persistent Corpse Disposal - Mournhold Courtiers
 
result0013="RF-PCD-Mournhold_Courtiers.esp"
result="${result0013}"
${tes3cmd} dump "${result0001_pcd}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0003_moc}" --raw "${result}" --type DIAL --type INFO
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0013. Persistent Corpse Disposal - Creeping Blight
 
result0014="RF-PCD-Creeping_Blight.esp"
result="${result0014}"
${tes3cmd} dump "${result0001_pcd}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0004_crb}" --raw "${result}" --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0023. Imperial Legion Expansion - Mournhold Courtiers
 
result0023="RF-Imperial_Legion_Expansion-Mournhold_Courtiers.esp"
result="${result0023}"
${tes3cmd} dump "${result0002_ile}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0003_moc}" --raw "${result}" --type DIAL --type INFO
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0024. Imperial Legion Expansion - Creeping Blight
 
result0024="RF-Imperial_Legion_Expansion-Creeping_Blight.esp"
result="${result0024}"
${tes3cmd} dump "${result0002_ile}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0004_crb}" --raw "${result}" --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0034. Mournhold Courtiers - Creeping Blight
 
result0034="RF-Mournhold_Courtiers-Creeping_Blight.esp"
result="${result0034}"
${tes3cmd} dump "${result0003_moc}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0004_crb}" --raw "${result}" --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0123. Persistent Corpse Disposal - Imperial Legion Expansion - Mournhold Courtiers
 
result0123="RF-PCD-Imperial_Legion_Expansion-Mournhold_Courtiers.esp"
result="${result0123}"
${tes3cmd} dump "${result0012}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0003_moc}" --raw "${result}" --type DIAL --type INFO
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0124. Persistent Corpse Disposal - Imperial Legion Expansion - Creeping Blight
 
result0124="RF-PCD-Imperial_Legion_Expansion-Creeping_Blight.esp"
result="${result0124}"
${tes3cmd} dump "${result0012}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0004_crb}" --raw "${result}" --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0134. Persistent Corpse Disposal - Mournhold Courtiers- Creeping Blight

result0134="RF-PCD-Mournhold_Courtiers-Creeping_Blight.esp"
result="${result0134}"
${tes3cmd} dump "${result0001_pcd}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0034}" --raw "${result}" --type DIAL --type INFO --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 0234. Imperial Legion Expansion - Mournhold Courtiers - Creeping Blight
 
result0234="RF-Imperial_Legion_Expansion-Mournhold_Courtiers-Creeping_Blight.esp"
result="${result0234}"
${tes3cmd} dump "${result0002_ile}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0034}" --raw "${result}" --type DIAL --type INFO --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 1234. Persistent Corpse Disposal - Imperial Legion Expansion - Mournhold Courtiers - Creeping Blight

result1234="RF-PCD-Imperial_Legion_Expansion-Mournhold_Courtiers-Creeping_Blight.esp"
result="${result1234}"
${tes3cmd} dump "${result0123}" --raw "${result}" --type DIAL --type INFO --type TES3
${tes3cmd} dump "${result0004_crb}" --raw "${result}" --type SCPT
${tes3cmd} header "${result}" --hide-backups --update-record-count

# 9999. Create directory structure
mkdir -p "${dir_patch}/01 Persistent Corpse Disposal"
mv "${result0001_pcd}" "${dir_patch}/01 Persistent Corpse Disposal"
mkdir -p "${dir_patch}/02 Imperial Legion Expansion"
mv "${result0002_ile}" "${dir_patch}/02 Imperial Legion Expansion"
mkdir -p "${dir_patch}/03 Mournhold Courtiers"
mv "${result0003_moc}" "${dir_patch}/03 Mournhold Courtiers"
mkdir -p "${dir_patch}/04 Creeping Blight"
mv "${result0004_crb}" "${dir_patch}/04 Creeping Blight"
mkdir -p "${dir_patch}/05 PCD - ILE"
mv "${result0012}" "${dir_patch}/05 PCD - ILE"
mkdir -p "${dir_patch}/06 PCD - MhC"
mv "${result0013}" "${dir_patch}/06 PCD - MhC"
mkdir -p "${dir_patch}/07 PCD - CrB"
mv "${result0014}" "${dir_patch}/07 PCD - CrB"
mkdir -p "${dir_patch}/08 ILE - MhC"
mv "${result0023}" "${dir_patch}/08 ILE - MhC"
mkdir -p "${dir_patch}/09 ILE - CrB"
mv "${result0024}" "${dir_patch}/09 ILE - CrB"
mkdir -p "${dir_patch}/10 MhC - CrB"
mv "${result0034}" "${dir_patch}/10 MhC - CrB"
mkdir -p "${dir_patch}/11 PCD - ILE - MhC"
mv "${result0123}" "${dir_patch}/11 PCD - ILE - MhC"
mkdir -p "${dir_patch}/12 PCD - ILE - CrB"
mv "${result0124}" "${dir_patch}/12 PCD - ILE - CrB"
mkdir -p "${dir_patch}/13 PCD - MhC - CrB"
mv "${result0134}" "${dir_patch}/13 PCD - MhC - CrB"
mkdir -p "${dir_patch}/14 ILE - MhC - CrB"
mv "${result0234}" "${dir_patch}/14 ILE - MhC - CrB"
mkdir -p "${dir_patch}/15 PCD - ILE - MhC - CrB"
mv "${result1234}" "${dir_patch}/15 PCD - ILE - MhC - CrB"

