#!/usr/bin/env bash
# Changelog:
# 2021-10-23
#   - Recompile all the scripts in Construction Set
#
# vim: nowrap
# command used for formatting text: '<,'>EasyAlign 4/\-\-/r0 ; '<,'>EasyAlign / '/r0
result="Syc_AtHomeAlchemy - Finished.esp"
source1="Syc_AtHomeAlchemy.esp"
source2="Morrowind.esm"
tes3cmd="./tes3cmd"

# copy original file
cp -v "${source1}" "${result}"

# rename original records
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_alembic_01"         --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_a_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_calcinator_01"      --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_a_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_mortar_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_a_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_retort_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_a_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_alembic_01"         --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_g_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_calcinator_01"      --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_g_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_mortar_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_g_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_retort_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_g_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_alembic_01"         --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_j_alembic_01')"
# pfp change
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_calcinator_01"      --run "\$R->set({t=>'ITEX',f=>'Icon'},         'm\\Tx_calcinator_02.tga')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_calcinator_01"      --run "\$R->set({t=>'MODL',f=>'Model'},        'm\\App_J_Calcinator_01.nif')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_calcinator_01"      --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_j_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_mortar_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_j_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_retort_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_j_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_alembic_01"         --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_m_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_calcinator_01"      --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_m_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_mortar_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_m_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_retort_01"          --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_m_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_alembic_01"        --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_sm_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_calcinator_01"     --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_sm_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_mortar_01"         --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_sm_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_retort_01"         --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_sm_retort_01')"

# create "syc_" apparatus for pipes
$tes3cmd dump "${source2}" --raw "${result}" --type APPA \
 --exact-id "apparatus_a_spipe_01" \
 --exact-id "apparatus_a_spipe_tsiya"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_01"           --run "\$R->set({t=>'AADT',f=>'Weight'},       '0')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_01"           --run "\$R->set({t=>'AADT',f=>'Value'},        '0')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_01"           --run "\$R->set({t=>'NAME',f=>'ID'},           'syc_avp_a_spipe')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_tsiya"        --run "\$R->set({t=>'AADT',f=>'Weight'},       '0')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_tsiya"        --run "\$R->set({t=>'AADT',f=>'Value'},        '0')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_tsiya"        --run "\$R->set({t=>'NAME',f=>'ID'},           'syc_avp_a_spipe_tsiya')"

# now add pipes from Morrowind
$tes3cmd dump "${source2}" --raw "${result}" --type APPA \
 --exact-id "apparatus_a_spipe_01" \
 --exact-id "apparatus_a_spipe_tsiya"
# append the scripts
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_01"           --run "\$R->append(APPA::SCRI->new({'script',  'syc_avp_AHA_a_spipe_01'}))"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_tsiya"        --run "\$R->append(APPA::SCRI->new({'script',  'syc_avp_AHA_a_spipe_tsiya'}))"
# rename
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_01"           --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_a_spipe_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_tsiya"        --run "\$R->set({t=>'NAME',f=>'ID'},           'avp_apparatus_a_spipe_tsiya')"

# get original records again
$tes3cmd dump "${source1}" --raw "${result}" --type APPA \
 --exact-id "apparatus_a_alembic_01" \
 --exact-id "apparatus_a_calcinator_01" \
 --exact-id "apparatus_a_mortar_01" \
 --exact-id "apparatus_a_retort_01" \
 --exact-id "apparatus_g_alembic_01" \
 --exact-id "apparatus_g_calcinator_01" \
 --exact-id "apparatus_g_mortar_01" \
 --exact-id "apparatus_g_retort_01" \
 --exact-id "apparatus_j_alembic_01" \
 --exact-id "apparatus_j_calcinator_01" \
 --exact-id "apparatus_j_mortar_01" \
 --exact-id "apparatus_j_retort_01" \
 --exact-id "apparatus_m_alembic_01" \
 --exact-id "apparatus_m_calcinator_01" \
 --exact-id "apparatus_m_mortar_01" \
 --exact-id "apparatus_m_retort_01" \
 --exact-id "apparatus_sm_alembic_01" \
 --exact-id "apparatus_sm_calcinator_01" \
 --exact-id "apparatus_sm_mortar_01" \
 --exact-id "apparatus_sm_retort_01"

# now add pipes from Morrowind again
$tes3cmd dump "${source2}" --raw "${result}" --type APPA \
 --exact-id "apparatus_a_spipe_01" \
 --exact-id "apparatus_a_spipe_tsiya"

# replace script in original records
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_alembic_01"         --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_a_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_calcinator_01"      --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_a_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_mortar_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_a_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_retort_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_a_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_alembic_01"         --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_g_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_calcinator_01"      --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_g_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_mortar_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_g_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_g_retort_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_g_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_alembic_01"         --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_j_alembic_01')"
# pfp change
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_calcinator_01"      --run "\$R->set({t=>'ITEX',f=>'Icon'},         'm\\Tx_calcinator_02.tga')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_calcinator_01"      --run "\$R->set({t=>'MODL',f=>'Model'},        'm\\App_J_Calcinator_01.nif')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_calcinator_01"      --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_j_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_mortar_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_j_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_j_retort_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_j_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_alembic_01"         --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_m_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_calcinator_01"      --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_m_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_mortar_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_m_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_m_retort_01"          --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_m_retort_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_alembic_01"        --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_sm_alembic_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_calcinator_01"     --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_sm_calcinator_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_mortar_01"         --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_sm_mortar_01')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_retort_01"         --run "\$R->set({t=>'SCRI',f=>'Script'},       'avp_AHA_sm_retort_01')"

# append the scripts
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_01"           --run "\$R->append(APPA::SCRI->new({'script',  'avp_syc_avp_AHA_a_spipe_01'}))"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_a_spipe_tsiya"        --run "\$R->append(APPA::SCRI->new({'script',  'avp_syc_avp_AHA_a_spipe_tsiya'}))"

# Fix names according to PfP
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_alembic_01"        --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Alembic')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_calcinator_01"     --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Calcinator')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_mortar_01"         --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Mortar & Pestle')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "apparatus_sm_retort_01"         --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Retort')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "avp_apparatus_sm_alembic_01"    --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Alembic')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "avp_apparatus_sm_calcinator_01" --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Calcinator')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "avp_apparatus_sm_mortar_01"     --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Mortar & Pestle')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "avp_apparatus_sm_retort_01"     --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Retort')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "syc_sm_alembic"                 --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Alembic')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "syc_sm_calcinator"              --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Calcinator')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "syc_sm_mortar"                  --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Mortar & Pestle')"
$tes3cmd modify "${result}" --hide-backups --type APPA --exact-id "syc_sm_retort"                  --run "\$R->set({t=>'FNAM',f=>'Name'},     'Secret Master\'s Retort')"

set -e
false

# now the "easy" part, modify all "syc_AHA_" scripts, add "avp_AHA_" scripts :-\
# then load into CS, save each script, recompile all
# clean resulting of all added GMSTs and other scripts

$tes3cmd delete "${result}" --hide-backups --type GMST
$tes3cmd delete "${result}" --hide-backups --type SCPT\
  --exact-id "dagothurcreature1"\
  --exact-id "endgame"\
  --exact-id "float"\
  --exact-id "lorkhanheart"\
  --exact-id "signrotate"
