#!/usr/bin/env bash
vwe="Vivec Waistworks Expansion - GoVH comp.ESP"
rdaa="Racer Dust Arms and Armor.ESP"
result="Vivec Waistworks Expansion - GoVH comp - Racer Dust Arms and Armor.esp"
tes3cmd="./tes3cmd"
# create patch file
$tes3cmd dump "${vwe}" --raw-with-header "${result}" --type CELL --exact-id "Vivec, St. Olms Waistworks" --instance-match "ObjIdx:2113"
$tes3cmd delete "${result}" --hide-backups --type CELL --exact-id "Vivec, St. Olms Waistworks" --instance-match "ObjIdx:2113"
$tes3cmd delete "${result}" --hide-backups --type CELL --exact-id "Vivec, St. Olms Waistworks" --sub-match "Water_Height:"
$tes3cmd delete "${result}" --hide-backups --type CELL --exact-id "Vivec, St. Olms Waistworks" --sub-match "Ambient:"
# set masters to be able to delete subrecords
$tes3cmd modify "$result" --hide-backups --type TES3 --run "\
  \$R->append(TES3::MAST->new({'master', '${vwe}'})), \
  \$R->append(TES3::DATA->new({'length', '384666'})), \
  \$R->append(TES3::MAST->new({'master', '${rdaa}'})), \
  \$R->append(TES3::DATA->new({'length', '66780'}))"
$tes3cmd header "$result" --hide-backups --synchronize
# delete conflicts, move lantern
$tes3cmd modify "${result}" --hide-backups --exact-id "Vivec, St. Olms Waistworks" --run "\
  \$R->append(CELL::FRMR->new({'objidx', '2107', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'in_velothismall_cap_02'})), \
  \$R->append(CELL::DATA->new({'x', '1024.000', 'y', '-1536.000', 'z', '0.000', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '3.1416'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2108', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'ex_vivec_g_r_01'})), \
  \$R->append(CELL::XSCL->new({'scale', '0.50'})), \
  \$R->append(CELL::DATA->new({'x', '1023.461', 'y', '-1435.165', 'z', '-108.075', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '3.1416'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2109', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'furn_de_tapestry_06'})), \
  \$R->append(CELL::DATA->new({'x', '1030.473', 'y', '-1511.287', 'z', '99.243', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '6.2832'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2110', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'flora_bc_fern_02'})), \
  \$R->append(CELL::XSCL->new({'scale', '0.50'})), \
  \$R->append(CELL::DATA->new({'x', '1066.859', 'y', '-1432.544', 'z', '-75.460', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '2.2416'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2111', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'flora_bc_fern_02'})), \
  \$R->append(CELL::XSCL->new({'scale', '0.50'})), \
  \$R->append(CELL::DATA->new({'x', '977.497', 'y', '-1432.559', 'z', '-76.921', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '5.4978'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2112', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'furn_de_rope_06'})), \
  \$R->append(CELL::XSCL->new({'scale', '1.25'})), \
  \$R->append(CELL::DATA->new({'x', '1028.719', 'y', '-1451.266', 'z', '76.396', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '3.1416'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2113', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'light_de_paper_lantern_01_128'})), \
  \$R->append(CELL::DATA->new({'x', '1215', 'y', '-1280', 'z', '80', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '4.7124'})), \
  \$R->append(CELL::FRMR->new({'objidx', '2115', 'mastidx', '1'})), \
  \$R->append(CELL::NAME->new({'name', 'flora_bc_grass_02'})), \
  \$R->append(CELL::DATA->new({'x', '1031.874', 'y', '-1429.217', 'z', '-77.084', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '0.0000'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'}))"
# delete conflicts, move entrance
$tes3cmd modify "${result}" --hide-backups --exact-id "Vivec, St. Olms Waistworks" --run "\
  \$R->append(CELL::FRMR->new({'objidx', '11', 'mastidx', '2'})), \
  \$R->append(CELL::NAME->new({'name', 'in_velothismall_ndoor_01'})), \
  \$R->append(CELL::DODT->new({'x', '4098.36', 'y', '4138.01', 'z', '14352.00', 'x_angle', '0.00', 'y_angle', '0.00', 'z_angle', '3.14'})), \
  \$R->append(CELL::DNAM->new({'destination', 'Vivec, Racer Dust Arms and Armor'})), \
  \$R->append(CELL::DATA->new({'x', '1091.200', 'y', '-1404.724', 'z', '-128.000', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '1.5708'})), \
  \$R->append(CELL::FRMR->new({'objidx', '12', 'mastidx', '2'})), \
  \$R->append(CELL::NAME->new({'name', 'in_velothismall_3way_01'})), \
  \$R->append(CELL::DATA->new({'x', '1056.000', 'y', '-1280.000', 'z', '0.000', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '1.5708'})), \
  \$R->append(CELL::DELE->new({'deleted', '4729956'})), \
  \$R->append(CELL::FRMR->new({'objidx', '13', 'mastidx', '2'})), \
  \$R->append(CELL::NAME->new({'name', 'in_velothismall_dj_01'})), \
  \$R->append(CELL::DATA->new({'x', '1027.050', 'y', '-1408.000', 'z', '-128.000', 'x_angle', '0.0000', 'y_angle', '0.0000', 'z_angle', '1.5708'}))"
