#!/usr/bin/env bash
# Move few notes to stop them levitating in air when used with additional mods

original="Correspondances_of_Morrowind.esp"
tes3cmd="./tes3cmd"
result1="Correspondances_of_Morrowind - Balmora Guilds Expanded.esp"
result2="Correspondances_of_Morrowind - Balmora Waterworks.esp"
result3="Correspondances_of_Morrowind - Balmora Guilds Expanded - Balmora Waterworks.esp"

# 1. Balmora Guilds Expanded
result="$result1"

# ALL the notes require moving, so dump whole cell record
$tes3cmd dump "$original" --raw-with-header "$result" \
  --type CELL \
  --exact-id "balmora, guild of mages"
$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'})), \
  \$R->append(TES3::MAST->new({'master', '$original'})), \
  \$R->append(TES3::DATA->new({'length', '154194'}))"
$tes3cmd header "$result" --hide-backups --synchronize

# 1.1. Caldebir
$tes3cmd modify "$result" --hide-backups --type CELL --exact-id "balmora, guild of mages" --run "
 \$R->set({i=>[3], f=>'MastIdx'}, '4'),
 \$R->set({i=>[5], t=>'DATA', f=>'Z'}, '88.561')"

# 1.2. Sharn
$tes3cmd modify "$result" --hide-backups --type CELL --exact-id "balmora, guild of mages" --run "
 \$R->set({i=>[6], f=>'MastIdx'}, '4'),
 \$R->set({i=>[8], t=>'DATA', f=>'Y'}, '-1587.31'),
 \$R->set({i=>[8], t=>'DATA', f=>'Z'}, '-700.172')"

# 1.3. Globe
$tes3cmd modify "$result" --hide-backups --type CELL --exact-id "balmora, guild of mages" --run "
 \$R->set({i=>[9], f=>'MastIdx'}, '4'),
 \$R->set({i=>[11], t=>'DATA', f=>'X'}, '-174.086'),
 \$R->set({i=>[11], t=>'DATA', f=>'Y'}, '-766.964'),
 \$R->set({i=>[11], t=>'DATA', f=>'Z'}, '-704.581')"

# 2. Balmora Waterworks
result="$result2"

# ALL the notes require moving, so dump whole cell record
$tes3cmd dump "$original" --raw-with-header "$result" \
  --type CELL \
  --exact-id "Balmora, Caius Cosades' House"
$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'})), \
  \$R->append(TES3::MAST->new({'master', '$original'})), \
  \$R->append(TES3::DATA->new({'length', '154194'}))"
$tes3cmd header "$result" --hide-backups --synchronize

# 2.1. Table in Caius House is moved, so move note a bit:
$tes3cmd modify "$result" --hide-backups --type cell --exact-id "Balmora, Caius Cosades' House" --run "
 \$R->set({i=>[3], f=>'MastIdx'}, '4'),
 \$R->set({i=>[5], f=>'X'}, '154.470'),
 \$R->set({i=>[5], f=>'Y'}, '-158.011')"

# 3. Balmora Guilds Expanded and Balmora Waterworks
result="$result3"

# Simply dump changed cells from corresponding patches
$tes3cmd dump "$result1" --raw-with-header "$result" --type CELL
$tes3cmd dump "$result2" --raw "$result" --type CELL
$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'})), \
  \$R->append(TES3::MAST->new({'master', '$original'})), \
  \$R->append(TES3::DATA->new({'length', '154194'}))"
$tes3cmd header "$result" --hide-backups --synchronize

# 99. Create directory structure
result_dir="Correspondances_of_Morrowind_Patches"
mkdir -p "$result_dir/01 Balmora Guilds Expanded"
mv -v "$result1" "$result_dir/01 Balmora Guilds Expanded"/
mkdir -p "$result_dir/02 Balmora Waterworks"
mv -v "$result2" "$result_dir/02 Balmora Waterworks"/
mkdir -p "$result_dir/03 Balmora Guilds Expanded - Balmora Waterworks"
mv -v "$result3" "$result_dir/03 Balmora Guilds Expanded - Balmora Waterworks"/
