#!/usr/bin/env bash
# Changelog:
# 2021-10-24
#   - Recompile vivecvoice script in Construction Set
#   - Remove the changed script from Morrowind patch
#   - Rename legacy non-replacement patch to "...OpenMW"
#
source1="Vivec_Voice_addon TRIBUNAL.esp"
source2="Vivec_Voice_addon TRIBUNAL - Patch for Purists - Speedup.esp"
result="Vivec_Voice_addon TRIBUNAL - Refreshed.esp"
tes3cmd="./tes3cmd"

$tes3cmd dump "${source1}" --raw-with-header "${result}" --type GLOB --type SOUN
$tes3cmd dump "${source2}" --raw "${result}" --type SCPT
$tes3cmd dump "${source1}" --raw "${result}" --type CREA
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "persecute the dissident priests"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:persecute the dissident priests)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "tribunal fight dagoth ur"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:tribunal fight dagoth ur)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "suppress the apographa"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:suppress the apographa)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "cause others to suffer"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:cause others to suffer)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "protector of morrowind"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:protector of morrowind)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "bitter recriminations"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:bitter recriminations)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "war on the nerevarine"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:war on the nerevarine)"
$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "remember being mortal"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:remember being mortal)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "rebuilding the temple"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:rebuilding the temple)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "reorganize the temple"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:reorganize the temple)"
# check '
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "wraithguard's powers"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:wraithguard's powers)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "dagoth ur's servants"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:dagoth ur's servants)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "dagoth ur's defenses"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:dagoth ur's defenses)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "people of morrowind"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:people of morrowind)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "where is dagoth ur"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:where is dagoth ur)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "dagoth ur's powers"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:dagoth ur's powers)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "prepare for battle"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:prepare for battle)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "dagoth ur winning"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:dagoth ur winning)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "dagoth ur's plans"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:dagoth ur's plans)"
$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "defeat dagoth ur"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --exact-id "367316099307276611"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --exact-id "1953121723209921247"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --exact-id "6739307621390017830"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "we have survived"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:we have survived)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "murder nerevar"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:murder nerevar)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "break my oath"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:break my oath)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "divine powers"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:divine powers)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "red mountain"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:red mountain)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "explanations"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:explanations)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "who can help"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:who can help)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "dwemer's sin"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:dwemer's sin)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "to be a god"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:to be a god)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "greeting 1"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Greeting:greeting 1)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "the dwemer"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:the dwemer)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "my library"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:my library)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "much to do"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:much to do)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "dagoth ur"
# $tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:dagoth ur)"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --exact-id "6188310802152324078"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --exact-id "30723317541296328471"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "questions"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:questions)"
$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "almalexia"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:almalexia)"

$tes3cmd dump "${source1}" --raw "${result}" --type DIAL --exact-id "sotha sil"
$tes3cmd dump "${source1}" --raw "${result}" --type INFO --match    "(Topic:sotha sil)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "business"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:business)"

$tes3cmd dump "${source2}" --raw "${result}" --type DIAL --exact-id "counsel"
$tes3cmd dump "${source2}" --raw "${result}" --type INFO --match    "(Topic:counsel)"

$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'}))"
$tes3cmd header "$result" --hide-backups --synchronize

#   - Recompile vivecvoice script in Construction Set
#   - Remove the changed script from Morrowind patch
#   - Rename legacy non-replacement patch to "...OpenMW"
