#!/usr/bin/env bash
# Changelog:
# 2021-11-13
#   - Add SLF_AM_Writer02_02
#

# This script moves 5 NPCs to appear sitting at chairs/benches as normal people :-)

original="Library of Vivec Overhaul - Full.esp"
result="Library of Vivec Overhaul - Full - OpenMW Patch.esp"

./tes3cmd dump "$original" --raw-with-header "$result" \
  --type CELL \
  --exact-id "Vivec, Library of Vivec" \
  --instance-match "Name:SLF_AM_.*(02_01|r2_03|02_02|02_03|r2_04)"

./tes3cmd modify "$result" --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', 'Tamriel_Data.esm'})), \
  \$R->append(TES3::DATA->new({'length', '8787443'})), \
  \$R->append(TES3::MAST->new({'master', 'OAAB_Data.esm'})), \
  \$R->append(TES3::DATA->new({'length', '1124372'})), \
  \$R->append(TES3::MAST->new({'master', 'Library of Vivec Overhaul - Full.esp'})), \
  \$R->append(TES3::DATA->new({'length', '1064748'}))"

./tes3cmd header "$result" --synchronize

./tes3cmd modify "$result" \
  --exact-id "Vivec, Library of Vivec" \
  --sub-match "SLF_AM_Writer02_01" \
  --run "\
  \$R->set({i=>[4], f=>'Y'}, '1283'), \
  \$R->set({i=>[4], f=>'Z'}, '167'), \
  \$R->set({i=>[2], f=>'MastIdx'}, '6')"
./tes3cmd modify "$result" \
  --exact-id "Vivec, Library of Vivec" \
  --sub-match "Name:SLF_AM_reader2_03" \
  --run "\
  \$R->set({i=>[7],f=>'Y'}, '1741'), \
  \$R->set({i=>[7],f=>'Z'}, '-82'), \
  \$R->set({i=>[5], f=>'MastIdx'}, '6')"
./tes3cmd modify "$result" \
  --exact-id "Vivec, Library of Vivec" \
  --sub-match "Name:SLF_AM_Writer02_02" \
  --run "\
  \$R->set({i=>[10],f=>'Z'}, '-82'), \
  \$R->set({i=>[8], f=>'MastIdx'}, '6')"
./tes3cmd modify "$result" \
  --exact-id "Vivec, Library of Vivec" \
  --sub-match "Name:SLF_AM_Writer02_03" \
  --run "\
  \$R->set({i=>[13],f=>'X'}, '971'), \
  \$R->set({i=>[13],f=>'Y'}, '253'), \
  \$R->set({i=>[13],f=>'Z'}, '167'), \
  \$R->set({i=>[11], f=>'MastIdx'}, '6')"
./tes3cmd modify "$result" \
  --exact-id "Vivec, Library of Vivec" \
  --sub-match "Name:SLF_AM_reader2_04" \
  --run "\
  \$R->set({i=>[16],f=>'X'}, '-272'), \
  \$R->set({i=>[16],f=>'Z'}, '-82'), \
  \$R->set({i=>[14], f=>'MastIdx'}, '6')"

./tes3cmd clean "$result" --overwrite
