#!/usr/bin/env bash
# Changelog:
# 2021-10-24
#   - Recompile scripts in Construction Set for vanilla engine
#
tes3cmd="./tes3cmd"

original="quarraclan.esp"
source1="Patch for Purists.esm"
source2="Morrowind.esm"
source3="Bloodmoon.esm"
result="quarraclan_refreshed.esp"

cp -v "${original}" "${result}"
# Clean
$tes3cmd clean "${result}" --overwrite --hide-backups
# Delete dirty and unneded changes
$tes3cmd delete "${result}" --hide-backups --type NPC_ \
  --exact-id "aryon" \
  --exact-id "caius cosades" \
  --exact-id "edwinna elbert" \
  --exact-id "falura llervu" \
  --exact-id "fryfnhild" \
  --exact-id "hides_his_foot" \
  --exact-id "huleeya" \
  --exact-id "itermerel" \
  --exact-id "jon hawker" \
  --exact-id "lucan ostorius2" \
  --exact-id "nalcarya of white haven" \
  --exact-id "neloth" \
  --exact-id "nibani maesa" \
  --exact-id "persius mercius" \
  --exact-id "ra'virr" \
  --exact-id "rabinna" \
  --exact-id "ranis athrys" \
  --exact-id "skinkintreesshade" \
  --exact-id "tenyeminwe" \
  --exact-id "therana" \
  --exact-id "tivam sadri" \
  --exact-id "unila berendas" \
  --exact-id "vienne gilelle" \
  --exact-id "viras guls" \
  --exact-id "wayn" \
  --exact-id "zanummu"
$tes3cmd delete "${result}" --hide-backups --type CREA \
  --exact-id "golden saint_staada"
$tes3cmd delete "${result}" --hide-backups --type CONT \
  --exact-id "barrel_01"
$tes3cmd delete "${result}" --hide-backups --type CELL \
  --exact-id "ald-ruhn, cienne sintieve: alchemist" \
  --exact-id "balmora, guild of fighters" \
  --exact-id "ebonheart, argonian mission" \
  --exact-id "sheogorad (-3, 19)"
$tes3cmd delete "${result}" --hide-backups --type SCPT \
  --exact-id "vampire_quarra"
$tes3cmd delete "${result}" --hide-backups --type INFO \
  --exact-id "16027228191418416208"
$tes3cmd delete "${result}" --hide-backups --type DIAL \
  --exact-id "" \
  --exact-id "future rewards" \
  --exact-id "hello" \
  --exact-id "latest rumors" \
  --exact-id "new clothes" \
  --exact-id "telvanni bug musk"
# LDM context matters wording fixes
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "26303211681180729678" \
  --exact-id "43968095295613136" \
  --exact-id "3457226791828626576" --run "\
  \$R->set({t=>'NAME',f=>'Response'}, 'We are the dark walkers, those who prey on the warm blooded. Our gifts are many, and weaknesses few. There are three major clans: Aundae, Berne, and Quarra. There are others in this world as well, living like cattle in tombs under the ground.')"
# PFP
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "147982510814522589" --run "\
  \$R->set({t=>'NAME',f=>'Response'}, 'Ha!')"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "1040515641492015484" --run "\
  \$R->set({t=>'NAME',f=>'Response'}, 'There are a number of them, some not easy to obtain. It could take you many days to find what I need, and you will likely die in the attempt. Do you feel yourself up for the task, Fresh Blood?'), \
  \$R->set({t=>'SCVR',f=>'Comparison'}, '0'), \
  \$R->set({t=>'BNAM',f=>'Result'}, 'Choice \"I will do as you ask.\" 1 \"No, I have no interest in this.\" 2')"
$tes3cmd delete "${result}" --hide-backups --type NPC_ \
  --exact-id "estalenya" \
  --exact-id "igna" \
  --exact-id "pelf" \
  --exact-id "silasson" \
  --exact-id "siri" \
  --exact-id "varnis stieve" \
  --exact-id "volrina quarra" \
  --exact-id "waylas" \
  --sub-match "Model:base"
$tes3cmd modify "${result}" --hide-backups --type CELL \
  --exact-id "druscashti, lower level" \
  --exact-id "druscashti, upper level" --run "\
  \$R->set({t=>'DATA',f=>'Flags'}, '1')"
# reanable future rewards for other clans
$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "future rewards"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --exact-id "250655861367625359"
$tes3cmd dump "${source3}" --raw "${result}" --type INFO --exact-id "16337111081828622674"
$tes3cmd modify "${result}" --hide-backups --type INFO \
  --exact-id "16337111081828622674" \
  --exact-id "250655861367625359" --run "\
  \$R->set({i=>[12],f=>'Comparison'}, '2')"
$tes3cmd dump "${source1}" --raw "${result}" --type SCPT --exact-id "vampire_quarra"
# this is to set cattle passive 
$tes3cmd dump "${source1}" --raw "${result}" --type SCPT --exact-id "cattlescript"

# fix SCPT
# SCPT fixes
# "enablefalsimora": fix minor error (script name)
# "vampire_quarra_moranna": add block from PfP 
# "vampire_quarra_siri": add block from PfP
# "cattlescript": fix minor errors (StopCombat), "VA_VampAmulet" == 40 -> "VA_QuarraClan" >= 30 
# $tes3cmd delete "${result}" --hide-backups --type SCPT \
#   --exact-id "vampire_quarra" \
#   --exact-id "enablefalsimora" \
#   --exact-id "vampire_quarra_moranna" \
#   --exact-id "vampire_quarra_siri" \
#   --exact-id "cattlescript"
# source_temp="${result%%.*}.omwaddon"
# $tes3cmd dump "${source_temp}" --raw "${result}" --type SCPT \
#   --exact-id "vampire_quarra" \
#   --exact-id "enablefalsimora" \
#   --exact-id "vampire_quarra_moranna" \
#   --exact-id "vampire_quarra_siri" \
#   --exact-id "cattlescript"
#
#$tes3cmd header "${result}" --hide-backups --synchronize

# how to test
# player->addspell "vampire blood aundae"
# startscript vampirecheck
# coc "mark's vampire test cell"
# disable x y
# rest 25h x3
# coc "ashmelech"
# journal ...
# addtopic "future rewards"

set -e
false

# recompile scripts and return them to plugin

$tes3cmd delete "${result}" --hide-backups --type SCPT \
  --exact-id "vampire_quarra" \
  --exact-id "enablefalsimora" \
  --exact-id "vampire_quarra_moranna" \
  --exact-id "vampire_quarra_siri" \
  --exact-id "cattlescript"
source_temp="qr.esp"
$tes3cmd dump "${source_temp}" --raw "${result}" --type SCPT \
  --exact-id "vampire_quarra" \
  --exact-id "enablefalsimora" \
  --exact-id "vampire_quarra_moranna" \
  --exact-id "vampire_quarra_siri" \
  --exact-id "cattlescript"

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