#!/usr/bin/env bash
tes3cmd=./tes3cmd
original_patch_main="Better_Sounds.esp"
original_patch_bm="BS_BM_WeatherChange.esp"
# Scripts were edited in OpenMW-CS, which changes other records, so I've exported only scripts
scripts_donor="scripts_donor.esp"
result="Yet Another Better Sounds Patch.esp"

rm -v "${result}"
# Assemble the patch from many pieces
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type TES3 --type SOUN
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type MGEF
${tes3cmd} dump "${scripts_donor}" --raw "${result}" --type SCPT
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type REGN
${tes3cmd} dump "${original_patch_bm}" --raw "${result}" --type REGN
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type SSCR
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type CONT
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type ACTI
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type SNDG
${tes3cmd} dump "${original_patch_main}" --raw "${result}" --type CELL

# Add Bloodmoon to masters
${tes3cmd} modify "${result}" --hide-backups --type TES3 --run "\
  \$R->append(TES3::MAST->new({'master', 'Bloodmoon.esm'})), \
  \$R->append(TES3::DATA->new({'length', '9631798'}))"
${tes3cmd} header "${result}" --hide-backups --synchronize

# Clean it
${tes3cmd} clean "${result}" --hide-backups --overwrite

# Now clean manually starting with 0 objects cells
${tes3cmd} delete "${result}" --hide-backups --type CELL\
  --exact-id "ald-ruhn (-2, 6)"\
  --exact-id "tel mora, radras: smith"

# Fix Ref counts in 3 cleaned cells
${tes3cmd} modify "${result}" --hide-backups --type CELL\
  --exact-id "tel mora (13, 14)"\
  --exact-id "an abandoned shack"\
  --run "\$R->set({t=>'NAM0', f=>'Reference_Count'}, '1')"

# Fix region names
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "bitter coast region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Bitter Coast Region')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "molag mar region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Molag Amur Region')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "sheogorad"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Sheogorad Region')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "brodir grove region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Solstheim, Brodir Grove Region')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "felsaad coast region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Solstheim, Felsaad Coast Region')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "hirstaang forest region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Solstheim, Hirstaang Forest')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "isinfier plains region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Solstheim, Isinfier Plains')"
${tes3cmd} modify "${result}" --hide-backups --type REGN --exact-id "moesring mountains region"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Solstheim, Moesring Mountains')"

# Patch for Purists
${tes3cmd} modify "${result}" --hide-backups --type ACTI --exact-id "active_sign_c_guildf_01"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Fighters Guild')"
${tes3cmd} modify "${result}" --hide-backups --type ACTI --exact-id "active_sign_c_guildm_01"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Mages Guild')"
${tes3cmd} modify "${result}" --hide-backups --type ACTI --exact-id "active_sign_c_guildm_caldera"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Mages Guild Caldera')"
# Add illegal to sleep here
${tes3cmd} modify "${result}" --hide-backups --type CELL --exact-id "ald velothi (-11, 15)"\
  --run "\$R->set({i=>[1], f=>'Flags'}, '70')"
${tes3cmd} modify "${result}" --hide-backups --type CONT --exact-id "flora_hackle-lo_02"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Hackle-Lo')"

# Finally fix filename
${tes3cmd} modify "${result}" --hide-backups --type SOUN --exact-id "tawernambient02"\
  --run "\$R->set({t=>'FNAM', f=>'Filename'}, 'Fx\\Gizels_House02.wav')"
