#!/usr/bin/env bash
# vim: nowrap
#
# Changelog:
#
# 2021-09-14
#   - Remake patch completely (again)
#     - Separate base and NPC_ patches
#   - Clean
#     - Remove 8 more empty cells
#     - Remove redundant script
#     - Fix description
#   - Compatible
#     - Replace custom lantern with modified vanilla one
#     - Remove 2 added objects from Raven Rock
#     - No longer modify LTEX record (that was my previous step, it's unneeded)
#
# 2021-09-16
#   - Aggressively Compatible
#     - Remove terrain changes (revert back to vanilla)
#     - Remove most silt striders size change
#     - Move items to fit vanilla terrain
#   - Ultra Aggressively Compatible
#     - Remove changes to Ald-Ruhn and Vivec striderports for compat with Nwah Ald-Ruhn and Vivec
#
# 2021-09-17
#   - Make compatibility with both Boys in the Hood and Wear a Mask
#     - Fix 3 NPCs names according to PfP
#
# 2022-11-22
#   - Add "The Strider's Nest" versions to "01 Base"
#   - Add "LGNPC Gnaar Mok" versions to "02 NPC"
#
# 2022-11-27
#   - Remove "TR_Travels" patches(Tamriel Rebuilt v22.11 no longer has TR_Travels)
#   - Remove 13 misc items from Vivec, Ebonheart, Dagon Fel for compatibility with updated Tamriel Rebuilt
#

original="../../mods/ObjectsClutter/PortsOfVvardenfell/0/01 Plugins/Ports Of Vvardenfell V1.6.ESP"
source_hood="Boys in the Hood.ESP"
source_courier="Silt Strider Courier.ESP"

out1_clean="Ports Of Vvardenfell V1.6 - Clean.esp"
out2_compat="Ports Of Vvardenfell V1.6 - Compatible.esp"
out3_aggro="Ports Of Vvardenfell V1.6 - Aggressively Compatible.esp"
out4_aggro_sn="Ports Of Vvardenfell V1.6 - Aggressively Compatible - The Strider's Nest.esp"
out5_ultra="Ports Of Vvardenfell V1.6 - Ultra Aggressively Compatible.esp"
out6_ultra_sn="Ports Of Vvardenfell V1.6 - Ultra Aggressively Compatible - The Strider's Nest.esp"

npc1_totsp="Ports Of Vvardenfell V1.6 - NPC Only - TOTSP.esp"
npc2_hood_mask="Ports Of Vvardenfell V1.6 - NPC Only - Hood and Mask.esp"
npc3_hood_mask_totsp="Ports Of Vvardenfell V1.6 - NPC Only - Hood and Mask - TOTSP.esp"
npc4_lgnpc="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok.esp"
npc5_lgnpc_totsp="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - TOTSP.esp"
npc6_lgnpc_hood_mask="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - Hood and Mask.esp"
npc7_lgnpc_hood_mask_totsp="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - Hood and Mask - TOTSP.esp"

other1_courier="Silt Strider Courier - Ports Of Vvardenfell.esp"

tes3cmd="./tes3cmd"

# 1.1 Clean the plugin, fix description, change Khuul caravaner position

cp -v "${original}" "${out1_clean}"

${tes3cmd} clean "${out1_clean}" --hide-backups --overwrite
# chargenplank is left there for some reason
${tes3cmd} clean "${out1_clean}" --hide-backups --overwrite
# these cells contain 0 objects
${tes3cmd} delete "${out1_clean}" --hide-backups --type CELL\
  --exact-id "tel aruhn (15, 5)"\
  --exact-id "suran (6, -7)"\
  --exact-id "dren plantation (2, -7)"\
  --exact-id "ald-ruhn (-2, 6)"\
  --exact-id "bitter coast region (-7, -5)"\
  --exact-id "clutter warehouse - everything must go!"\
  --exact-id "hirstaang forest region (-25, 18)"\
  --exact-id "raven rock, vacant house"
# redundant script
${tes3cmd} delete "${out1_clean}" --hide-backups --type SCPT --exact-id "chargenclassnpc"
# fix header
${tes3cmd} header "${out1_clean}" --hide-backups --description "This plugin overhauls all of the boat, silt strider and gondolier ports across Vvardenfell and also many travel NPC's.\n\nSee the readme file for all details.\n"
# finally move khuul caravaner a bit otherwise it keeps falling down the hill
${tes3cmd} modify "${out1_clean}" --hide-backups --type CELL --exact-id "khuul (-9, 16)" --sub-match "Name:seldus nerendus" --run "\
  \$R->set({i=>[6], f=>'X'}, '-65858.000'), \
  \$R->set({i=>[6], f=>'Y'}, '135490.000'), \
  \$R->set({i=>[6], f=>'Z'}, '969.000'), \
  \$R->set({i=>[6], f=>'Z_Angle'}, '0.6981')"

# 1.2 Make it more compatible by further removing few items and cells, change lantern to vanilla one for compatibility with TLAD etc

cp -v "${out1_clean}" "${out2_compat}"

# remove for compat with TOTSP
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "fort frostmoth (-22, 16)"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "raven rock (-25, 19)"
# Conflict with Province Cyrodiil, remove 3 crates right in front of Cyrodiil shipmaster
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:442"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:443"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:444"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:445"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:446"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:447"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:448"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:450"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:465"
# tiny conflict with MDP Ebonheart, remove Deadeye clipping with sacks
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:438"
# remove dock hoist, barrels etc from TR shipmaster's location in Ebonheart
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:436"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:441"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:449"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:460"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "ebonheart (2, -13)" --instance-match "ObjIdx:467"
# remove dock hoist etc from TR shipmaster's location in Vivec, Foreign Quarter
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "vivec, foreign quarter (4, -10)" --instance-match "ObjIdx:255"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "vivec, foreign quarter (4, -10)" --instance-match "ObjIdx:256"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "vivec, foreign quarter (4, -10)" --instance-match "ObjIdx:257"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "vivec, foreign quarter (4, -10)" --instance-match "ObjIdx:265"
# remove dock hoist, barrels etc from TR shipmaster's location in Dagon Fel
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "dagon fel (7, 22)" --instance-match "ObjIdx:125"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "dagon fel (7, 22)" --instance-match "ObjIdx:126"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "dagon fel (7, 22)" --instance-match "ObjIdx:146"
${tes3cmd} delete "${out2_compat}" --hide-backups --type CELL --exact-id "dagon fel (7, 22)" --instance-match "ObjIdx:147"
# Remove mike's lantern, add com_lantern_01, increase time, replace it for all NPC_
${tes3cmd} delete "${out2_compat}" --hide-backups --type LIGH --exact-id "Mick083_lantern_infinite_time"
${tes3cmd} dump "Morrowind.esm" --raw "${out2_compat}" --type LIGH --exact-id "light_com_lantern_01"
${tes3cmd} modify "${out2_compat}" --hide-backups --type LIGH --exact-id "light_com_lantern_01" --run "\
  \$R->set({t=>'LHDT', f=>'Time'}, '300')"
# this string requires 0.37 or enabling replace in 0.40 (comment lines 8665-8667 in tes3cmd 0.40)
${tes3cmd} modify "${out2_compat}" --hide-backups --type NPC_ --replace '/Mick083_lantern_infinite_time/light_com_lantern_01/'
# this one is not needed really, so disabled it
# ${tes3cmd} modify "${out2_compat}" --type LTEX --exact-id "sand_02" --run "\
#   \$R->set({t=>'DATA', f=>'Texture_Path'}, 'Tx_sand_02.dds')"

# 1.3 Make it even more compatible by removing LAND edits and moving/removing more objects

cp -v "${out2_compat}" "${out3_aggro}"

# remove all terrain edits
${tes3cmd} delete "${out3_aggro}" --hide-backups --type LAND
# remove changes to silt striders in those CELLs with LAND removed
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --instance-match "Name:a_siltstrider"\
  --exact-id "balmora (-3, -3)"\
  --exact-id "khuul (-9, 16)"\
  --exact-id "maar gan (-3, 12)"\
  --exact-id "molag mar (12, -8)"\
  --exact-id "vivec (3, -9)"
  # --exact-id "gnisis (-11, 10)"\
  # --exact-id "seyda neen (-2, -9)"\

# now go through all affected cells and fix items positions

# Ald-ruhn (-3, 6)
# restore striderport position
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "ald-ruhn (-3, 6)" --instance-match "Name:ex_redoran_striderport_01"
# from here text is rather cryptic
# move strider a bit (for compat with Silt Striders Redone), move items higher because of restored striderport
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "ald-ruhn (-3, 6)" --run "
  \$R->set({i=>[6], f=>'Z'}, '2777.12'),\
  \$R->set({i=>[52], f=>'Scale'}, '1'),\
  \$R->set({i=>[53], f=>'X'}, '-18042.6'),\
  \$R->set({i=>[53], f=>'Y'}, '54731.762'),\
  \$R->set({i=>[53], f=>'Z'}, '1557.066'),\
  \$R->set({i=>[56], f=>'Z'}, '2878.732'),\
  \$R->set({i=>[59], f=>'Z'}, '2878.603'),\
  \$R->set({i=>[68], f=>'Z'}, '2791.377'),\
  \$R->set({i=>[74], f=>'Z'}, '2807.936'),\
  \$R->set({i=>[81], f=>'Z'}, '2802.263'),\
  \$R->set({i=>[90], f=>'Z'}, '2829.829'),\
  \$R->set({i=>[96], f=>'Z'}, '2790.416'),\
  \$R->set({i=>[102], f=>'Z'}, '2838.755'),\
  \$R->set({i=>[108], f=>'Z'}, '2842.889'),\
  \$R->set({i=>[114], f=>'Z'}, '2851.96')"

# balmora (-3, -3)
# remove vivec status in front of strider because i was not able to fit it there with restored terrain
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:light_com_candle"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:ingred_fire_petal"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:ingred_gold_kanet"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:Gold_001"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:In_Hlaalu_Platform_01"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:ex_v_vivecstatue"
# 3 baskets in front of council club would levitate in air
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "ObjIdx:3410(7|8|9)"
# fit those crates and barrels underneath to terrain
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "balmora (-3, -3)" --run "
  \$R->set({i=>[150], f=>'Z'}, '569.5'),\
  \$R->set({i=>[150], f=>'Y_Angle'}, '0.1347'),\
  \$R->set({i=>[157], f=>'X'}, '-21313.8'),\
  \$R->set({i=>[157], f=>'Y_Angle'}, '0.1286'),\
  \$R->set({i=>[294], f=>'Z'}, '596.629'),\
  \$R->set({i=>[294], f=>'Y_Angle'}, '0.0234')"

# khuul (-8, 16), (-9, 16), (-9, 17)
# move platform a bit lower (there is a small gap otherwise)
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "west gash region (-8, 16)" --run "
  \$R->set({i=>[6], f=>'Z'}, '1115')"
# move items a bit lower as well, as platform moved a bit lower
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "khuul (-9, 16)" --run "
  \$R->set({i=>[84], f=>'Z'}, '1010.095'),\
  \$R->set({i=>[120], f=>'Z'}, '1035.507'),\
  \$R->set({i=>[144], f=>'Z'}, '1038.448'),\
  \$R->set({i=>[138], f=>'Z'}, '1038.448'),\
  \$R->set({i=>[132], f=>'Z'}, '1038.448'),\
  \$R->set({i=>[126], f=>'Z'}, '1038.448'),\
  \$R->set({i=>[74], f=>'Z'}, '995.575'),\
  \$R->set({i=>[26], f=>'Z'}, '1003.807'),\
  \$R->set({i=>[40], f=>'Z'}, '1008.282'),\
  \$R->set({i=>[108], f=>'Z'}, '1057.429'),\
  \$R->set({i=>[102], f=>'Z'}, '1046.698'),\
  \$R->set({i=>[96], f=>'Z'}, '1046.698'),\
  \$R->set({i=>[156], f=>'Z'}, '995.061'),\
  \$R->set({i=>[90], f=>'Z'}, '992.510'),\
  \$R->set({i=>[47], f=>'Z'}, '1068.883'),\
  \$R->set({i=>[53], f=>'Z'}, '1008.971'),\
  \$R->set({i=>[150], f=>'Z'}, '1042.210'),\
  \$R->set({i=>[114], f=>'Z'}, '992.304')"

# maar gan (-3, 12)
# readjust items underneath to vanilla terrain
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "maar gan (-3, 12)" --run "
  \$R->set({i=>[80], f=>'X_Angle'}, '6.1086'),\
  \$R->set({i=>[77], f=>'Z'}, '1673'),\
  \$R->set({i=>[74], f=>'Z'}, '1702'),\
  \$R->set({i=>[123], f=>'Z'}, '1686'),\
  \$R->set({i=>[117], f=>'Z'}, '1682'),\
  \$R->set({i=>[129], f=>'Z'}, '1655'),\
  \$R->set({i=>[17], f=>'Z'}, '1702'),\
  \$R->set({i=>[17], f=>'X'}, '-22293.3'),\
  \$R->set({i=>[17], f=>'X'}, '100667'),\
  \$R->set({i=>[141], f=>'X'}, '-22422'),\
  \$R->set({i=>[141], f=>'Y'}, '100720'),\
  \$R->set({i=>[141], f=>'Z'}, '1622'),\
  \$R->set({i=>[135], f=>'X'}, '-22381.5'),\
  \$R->set({i=>[135], f=>'Y'}, '100637'),\
  \$R->set({i=>[135], f=>'Z'}, '1633')"

# molag mar (12, -7), (12, -8), (13, -8)
# move few items higher (barrels etc)
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "molag mar (12, -8)" --run "
  \$R->set({i=>[128], f=>'Z'}, '1448.07'),\
  \$R->set({i=>[239], f=>'Z'}, '1456.55'),\
  \$R->set({i=>[258], f=>'Z'}, '1480.76'),\
  \$R->set({i=>[264], f=>'Z'}, '1414.28'),\
  \$R->set({i=>[282], f=>'Z'}, '1427')"

# suran (6, -6)
# again move strider a bit, move logs on the floor due to reverted terrain
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "suran (6, -6)" --run "
  \$R->set({i=>[10], f=>'Scale'}, '1'),\
  \$R->set({i=>[11], f=>'X'}, '53158.773'),\
  \$R->set({i=>[11], f=>'Y'}, '-47352.527'),\
  \$R->set({i=>[11], f=>'Z'}, '-21.659'),\
  \$R->set({i=>[37], f=>'Z'}, '556'),\
  \$R->set({i=>[40], f=>'Z'}, '556'),\
  \$R->set({i=>[43], f=>'Z'}, '575')"

# vivec (4, -9), (3, -9), (4, -10), (3, -10), (3, -12), (3, -13), (2, -11), (2, -12) (boat site), (2, -13)
# don't remember what this item is exactly but it required a slight move :-)
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "vivec (3, -9)" --run "
  \$R->set({i=>[19], f=>'Z'}, '164')"

# tried to move those stones, but then decided to simply delete
# ${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "ascadian isles region (4, -9)" --run "
#   \$R->set({i=>[7], f=>'X'}, '32944'),\
#   \$R->set({i=>[7], f=>'Z'}, '-72433'),\
#   \$R->set({i=>[7], f=>'Z'}, '33'),\
#   \$R->set({i=>[11], f=>'X'}, '32836'),\
#   \$R->set({i=>[11], f=>'Z'}, '-72549'),\
#   \$R->set({i=>[11], f=>'Z'}, '94')"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "ascadian isles region (4, -9)" --instance-match "Name:terrain_rock_ai_0"
# stone platform doesn't fit IMO
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "ascadian isles region (2, -12)" --instance-match "Name:ex_imp_plat_01"
# restore rock and flowers to vanilla state due to restored terrain
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "ascadian isles region (2, -12)" --instance-match "Name:flora_gold_kanet"
${tes3cmd} delete "${out3_aggro}" --hide-backups --type CELL --exact-id "ascadian isles region (2, -12)" --instance-match "Name:Terrain_rocks_AI_03"
# this is boat construction site - took me a load of time to readjust
${tes3cmd} modify "${out3_aggro}" --hide-backups --type CELL --exact-id "ascadian isles region (2, -12)" --run "
  \$R->set({i=>[102], f=>'X'}, '20189.627'),\
  \$R->set({i=>[102], f=>'X_Angle'}, '0.1791'),\
  \$R->set({i=>[102], f=>'Y_Angle'}, '0.0321'),\
  \$R->set({i=>[102], f=>'Z_Angle'}, '4.9742'),\
  \$R->set({i=>[102], f=>'Y'}, '-91820.289'),\
  \$R->set({i=>[102], f=>'Z'}, '44.422'),\
  \$R->set({i=>[105], f=>'X'}, '20214.010'),\
  \$R->set({i=>[105], f=>'X_Angle'}, '0.436'),\
  \$R->set({i=>[105], f=>'Y'}, '-91760.219'),\
  \$R->set({i=>[105], f=>'Y_Angle'}, '0'),\
  \$R->set({i=>[105], f=>'Z'}, '12.120'),\
  \$R->set({i=>[105], f=>'Z_Angle'}, '4.8869'),\
  \$R->set({i=>[108], f=>'X'}, '20237.812'),\
  \$R->set({i=>[108], f=>'X_Angle'}, '0.5236'),\
  \$R->set({i=>[108], f=>'Y'}, '-91697.133'),\
  \$R->set({i=>[108], f=>'Y_Angle'}, '0'),\
  \$R->set({i=>[108], f=>'Z'}, '-36.932'),\
  \$R->set({i=>[108], f=>'Z_Angle'}, '4.8869'),\
  \$R->set({i=>[111], f=>'X'}, '20263.521'),\
  \$R->set({i=>[111], f=>'X_Angle'}, '0.5236'),\
  \$R->set({i=>[111], f=>'Y'}, '-91640.742'),\
  \$R->set({i=>[111], f=>'Y_Angle'}, '0'),\
  \$R->set({i=>[111], f=>'Z'}, '-83.824'),\
  \$R->set({i=>[111], f=>'Z_Angle'}, '4.8869'),\
  \$R->set({i=>[114], f=>'X'}, '20284.342'),\
  \$R->set({i=>[114], f=>'X_Angle'}, '0.5236'),\
  \$R->set({i=>[114], f=>'Y'}, '-91583.719'),\
  \$R->set({i=>[114], f=>'Y_Angle'}, '0'),\
  \$R->set({i=>[114], f=>'Z'}, '-139.248'),\
  \$R->set({i=>[114], f=>'Z_Angle'}, '4.8869'),\
  \$R->set({i=>[117], f=>'X'}, '20655.203'),\
  \$R->set({i=>[117], f=>'Y'}, '-92189.766'),\
  \$R->set({i=>[117], f=>'Z'}, '169.470'),\
  \$R->set({i=>[117], f=>'X_Angle'}, '0.035'),\
  \$R->set({i=>[120], f=>'X'}, '20608.932'),\
  \$R->set({i=>[120], f=>'Y'}, '-92203.086'),\
  \$R->set({i=>[120], f=>'Z'}, '166.604'),\
  \$R->set({i=>[120], f=>'X_Angle'}, '0.035'),\
  \$R->set({i=>[123], f=>'X'}, '20658.348'),\
  \$R->set({i=>[123], f=>'Y'}, '-92191.672'),\
  \$R->set({i=>[123], f=>'Z'}, '178.073'),\
  \$R->set({i=>[123], f=>'X_Angle'}, '0.015'),\
  \$R->set({i=>[126], f=>'X'}, '20607.164'),\
  \$R->set({i=>[126], f=>'Y'}, '-92199.172'),\
  \$R->set({i=>[126], f=>'Z'}, '178.043'),\
  \$R->set({i=>[126], f=>'X_Angle'}, '0.052'),\
  \$R->set({i=>[129], f=>'X'}, '20636.805'),\
  \$R->set({i=>[129], f=>'Y'}, '-92203.539'),\
  \$R->set({i=>[129], f=>'Z'}, '186.770'),\
  \$R->set({i=>[129], f=>'X_Angle'}, '0.052'),\
  \$R->set({i=>[132], f=>'X'}, '19913.342'),\
  \$R->set({i=>[132], f=>'Y'}, '-92094.531'),\
  \$R->set({i=>[132], f=>'Z'}, '178.972'),\
  \$R->set({i=>[132], f=>'X_Angle'}, '5.7595'),\
  \$R->set({i=>[132], f=>'Y_Angle'}, '2.7925'),\
  \$R->set({i=>[135], f=>'X'}, '20177.557'),\
  \$R->set({i=>[135], f=>'Y'}, '-92209.188'),\
  \$R->set({i=>[135], f=>'Z'}, '182.077'),\
  \$R->set({i=>[135], f=>'Y_Angle'}, '3.84'),\
  \$R->set({i=>[138], f=>'X'}, '20128.357'),\
  \$R->set({i=>[138], f=>'Y'}, '-92395.438'),\
  \$R->set({i=>[138], f=>'Z'}, '164.161'),\
  \$R->set({i=>[138], f=>'X_Angle'}, '0.3491'),\
  \$R->set({i=>[138], f=>'Y_Angle'}, '3.6651'),\
  \$R->set({i=>[13], f=>'X'}, '19638.178'),\
  \$R->set({i=>[13], f=>'Y'}, '-92161.078'),\
  \$R->set({i=>[13], f=>'Z'}, '98.250'),\
  \$R->set({i=>[141], f=>'X'}, '20028.354'),\
  \$R->set({i=>[141], f=>'Y'}, '-92619.867'),\
  \$R->set({i=>[141], f=>'Z'}, '173.944'),\
  \$R->set({i=>[141], f=>'Y_Angle'}, '3.6652'),\
  \$R->set({i=>[147], f=>'X'}, '19980.232'),\
  \$R->set({i=>[147], f=>'Y'}, '-92301.617'),\
  \$R->set({i=>[147], f=>'Z'}, '222.658'),\
  \$R->set({i=>[153], f=>'X'}, '20035.887'),\
  \$R->set({i=>[153], f=>'Y'}, '-92787.188'),\
  \$R->set({i=>[153], f=>'Z'}, '212.150'),\
  \$R->set({i=>[159], f=>'X'}, '20018.158'),\
  \$R->set({i=>[159], f=>'Y'}, '-92754.523'),\
  \$R->set({i=>[159], f=>'Z'}, '212.297'),\
  \$R->set({i=>[162], f=>'Z'}, '113'),\
  \$R->set({i=>[165], f=>'Z'}, '112'),\
  \$R->set({i=>[168], f=>'Z'}, '111'),\
  \$R->set({i=>[16], f=>'X'}, '20291.5'),\
  \$R->set({i=>[16], f=>'X_Angle'}, '0.1227'),\
  \$R->set({i=>[16], f=>'Y'}, '-92842.469'),\
  \$R->set({i=>[16], f=>'Z'}, '234'),\
  \$R->set({i=>[171], f=>'Z'}, '138.742'),\
  \$R->set({i=>[181], f=>'X'}, '19971.715'),\
  \$R->set({i=>[181], f=>'Y'}, '-92809.805'),\
  \$R->set({i=>[181], f=>'Z'}, '212.442'),\
  \$R->set({i=>[185], f=>'X'}, '19536.662'),\
  \$R->set({i=>[185], f=>'Y'}, '-93212.453'),\
  \$R->set({i=>[185], f=>'Z'}, '190.000'),\
  \$R->set({i=>[196], f=>'X'}, '20198.645'),\
  \$R->set({i=>[196], f=>'Y'}, '-92571.711'),\
  \$R->set({i=>[196], f=>'Z'}, '164.367'),\
  \$R->set({i=>[19], f=>'X'}, '20326.2'),\
  \$R->set({i=>[19], f=>'Y'}, '-92829.469'),\
  \$R->set({i=>[19], f=>'Z'}, '239'),\
  \$R->set({i=>[19], f=>'X_Angle'}, '0.1326'),\
  \$R->set({i=>[202], f=>'X'}, '20048.645'),\
  \$R->set({i=>[202], f=>'Y'}, '-92759.945'),\
  \$R->set({i=>[202], f=>'Z'}, '212.775'),\
  \$R->set({i=>[208], f=>'X'}, '19713.264'),\
  \$R->set({i=>[208], f=>'Y'}, '-92102.875'),\
  \$R->set({i=>[208], f=>'Z'}, '116.183'),\
  \$R->set({i=>[22], f=>'X'}, '19969.695'),\
  \$R->set({i=>[22], f=>'Y'}, '-92818.398'),\
  \$R->set({i=>[22], f=>'Z'}, '180.718'),\
  \$R->set({i=>[25], f=>'X'}, '19481.342'),\
  \$R->set({i=>[25], f=>'Y'}, '-92737.633'),\
  \$R->set({i=>[25], f=>'Z'}, '168.470'),\
  \$R->set({i=>[25], f=>'X_Angle'}, '6.2482'),\
  \$R->set({i=>[28], f=>'X'}, '19367.746'),\
  \$R->set({i=>[28], f=>'Y'}, '-92763.531'),\
  \$R->set({i=>[28], f=>'Z'}, '172.470'),\
  \$R->set({i=>[28], f=>'X_Angle'}, '0.0523'),\
  \$R->set({i=>[28], f=>'Z_Angle'}, '0.3491'),\
  \$R->set({i=>[31], f=>'X'}, '19411.379'),\
  \$R->set({i=>[31], f=>'Y'}, '-92770.516'),\
  \$R->set({i=>[31], f=>'Z'}, '164.197'),\
  \$R->set({i=>[31], f=>'Y_Angle'}, '6.2310'),\
  \$R->set({i=>[34], f=>'X'}, '19506.656'),\
  \$R->set({i=>[34], f=>'Y'}, '-92626.547'),\
  \$R->set({i=>[34], f=>'Z'}, '207.012'),\
  \$R->set({i=>[37], f=>'X'}, '19909.475'),\
  \$R->set({i=>[37], f=>'Y'}, '-92884.188'),\
  \$R->set({i=>[37], f=>'Z'}, '223.410'),\
  \$R->set({i=>[40], f=>'X'}, '19571.736'),\
  \$R->set({i=>[40], f=>'Y'}, '-92876.562'),\
  \$R->set({i=>[40], f=>'Z'}, '160.537'),\
  \$R->set({i=>[43], f=>'X'}, '19587.998'),\
  \$R->set({i=>[43], f=>'Y'}, '-92706.219'),\
  \$R->set({i=>[43], f=>'Z'}, '187.199'),\
  \$R->set({i=>[43], f=>'Y_Angle'}, '2.618'),\
  \$R->set({i=>[46], f=>'X'}, '19648.193'),\
  \$R->set({i=>[46], f=>'Y'}, '-92426.344'),\
  \$R->set({i=>[46], f=>'Z'}, '183.598'),\
  \$R->set({i=>[46], f=>'X_Angle'}, '5.8468'),\
  \$R->set({i=>[46], f=>'Y_Angle'}, '2.3562'),\
  \$R->set({i=>[49], f=>'X'}, '19789.449'),\
  \$R->set({i=>[49], f=>'Y'}, '-92199.016'),\
  \$R->set({i=>[49], f=>'Z'}, '182.797'),\
  \$R->set({i=>[49], f=>'X_Angle'}, '5.7596'),\
  \$R->set({i=>[49], f=>'Y_Angle'}, '2.7925'),\
  \$R->set({i=>[52], f=>'X'}, '19738.340'),\
  \$R->set({i=>[52], f=>'Y'}, '-92923.844'),\
  \$R->set({i=>[52], f=>'Z'}, '180.063'),\
  \$R->set({i=>[52], f=>'X_Angle'}, '3.3652'),\
  \$R->set({i=>[52], f=>'Y_Angle'}, '2.618'),\
  \$R->set({i=>[58], f=>'X'}, '19906.631'),\
  \$R->set({i=>[58], f=>'Y'}, '-92882.805'),\
  \$R->set({i=>[58], f=>'Z'}, '218.769'),\
  \$R->set({i=>[64], f=>'X'}, '19906.570'),\
  \$R->set({i=>[64], f=>'Y'}, '-92871.883'),\
  \$R->set({i=>[64], f=>'Z'}, '217.810'),\
  \$R->set({i=>[70], f=>'X'}, '19918.672'),\
  \$R->set({i=>[70], f=>'Y'}, '-92889.867'),\
  \$R->set({i=>[70], f=>'Z'}, '217.925'),\
  \$R->set({i=>[76], f=>'X'}, '20014.945'),\
  \$R->set({i=>[76], f=>'Y'}, '-92719.203'),\
  \$R->set({i=>[76], f=>'Z'}, '213.204'),\
  \$R->set({i=>[83], f=>'X'}, '19641.895'),\
  \$R->set({i=>[83], f=>'Y'}, '-92164.469'),\
  \$R->set({i=>[83], f=>'Z'}, '101.351'),\
  \$R->set({i=>[87], f=>'X'}, '19810.205'),\
  \$R->set({i=>[87], f=>'Y'}, '-92492.688'),\
  \$R->set({i=>[87], f=>'Z'}, '153.031'),\
  \$R->set({i=>[90], f=>'X'}, '20104.133'),\
  \$R->set({i=>[90], f=>'X_Angle'}, '0.1312'),\
  \$R->set({i=>[90], f=>'Y'}, '-92036.766'),\
  \$R->set({i=>[90], f=>'Y_Angle'}, '0.0524'),\
  \$R->set({i=>[90], f=>'Z'}, '128.351'),\
  \$R->set({i=>[93], f=>'X'}, '20126.928'),\
  \$R->set({i=>[93], f=>'X_Angle'}, '0.1595'),\
  \$R->set({i=>[93], f=>'Y'}, '-91982.109'),\
  \$R->set({i=>[93], f=>'Y_Angle'}, '0.0353'),\
  \$R->set({i=>[93], f=>'Z'}, '109.477'),\
  \$R->set({i=>[96], f=>'X'}, '20146.648'),\
  \$R->set({i=>[96], f=>'X_Angle'}, '0.1586'),\
  \$R->set({i=>[96], f=>'Y_Angle'}, '0.0353'),\
  \$R->set({i=>[96], f=>'Z_Angle'}, '5.0126'),\
  \$R->set({i=>[96], f=>'Y'}, '-91927.602'),\
  \$R->set({i=>[96], f=>'Z'}, '93.724'),\
  \$R->set({i=>[99], f=>'X'}, '20167.729'),\
  \$R->set({i=>[99], f=>'X_Angle'}, '0.2173'),\
  \$R->set({i=>[99], f=>'Y_Angle'}, '0.0363'),\
  \$R->set({i=>[99], f=>'Z_Angle'}, '4.9742'),\
  \$R->set({i=>[99], f=>'Y'}, '-91876.086'),\
  \$R->set({i=>[99], f=>'Z'}, '72.000')"

# 1.4 Delete street lights aggressive(balmora) for compatibility with The Strider's Nest

cp -v "${out3_aggro}" "${out4_aggro_sn}"
 
${tes3cmd} delete "${out4_aggro_sn}" --hide-backups --type CELL\
  --exact-id "balmora (-3, -3)"\
  --instance-match "light_de_streetlight_01_223|furn_de_lightpost_01"
 
# 1.5 Remove few more cell changes for compat with Nwah's Ald-Ruhn and Vivec

cp -v "${out3_aggro}" "${out5_ultra}"

# remove changes to Ald-Ruhn and Vivec strider ports, also remove lonely fender from vivec port just in case
${tes3cmd} delete "${out5_ultra}" --hide-backups --type CELL\
  --exact-id "ald-ruhn (-3, 6)"\
  --exact-id "vivec (3, -9)"\
  --exact-id "ascadian isles region (4, -9)"

# 1.6 Delete street lights from ultra(balmora) for compatibility with The Strider's Nest

cp -v "${out5_ultra}" "${out6_ultra_sn}"
 
${tes3cmd} delete "${out6_ultra_sn}" --hide-backups --type CELL\
  --exact-id "balmora (-3, -3)"\
  --instance-match "light_de_streetlight_01_223|furn_de_lightpost_01"

# 2.1 NPC only - TOTSP

${tes3cmd} dump "${out2_compat}" --raw-with-header "${npc1_totsp}" --type MISC --id "_key_"
${tes3cmd} dump "${out2_compat}" --raw "${npc1_totsp}" --type LIGH
${tes3cmd} dump "${out2_compat}" --raw "${npc1_totsp}" --type NPC_

${tes3cmd} modify "${npc1_totsp}" --hide-backups --type NPC_ --exact-id "s'virr" --run "\
  \$R->set({i=>[14], f=>'Count'}, '220'), \
  \$R->set({t=>'AIDT', f=>'Unknown_2'}, '040000'), \
  \$R->set({t=>'DODT', f=>'X'}, '-116443.70'), \
  \$R->set({t=>'DODT', f=>'Y'}, '185960.48'), \
  \$R->set({t=>'DODT', f=>'Z'}, '400.00'), \
  \$R->set({t=>'DODT', f=>'Z_Angle'}, '6.18')"
${tes3cmd} modify "${npc1_totsp}" --hide-backups --type NPC_ --exact-id "basks_in_the_sun" --run "\
  \$R->set({i=>[17], f=>'Count'}, '500'), \
  \$R->set({t=>'AIDT', f=>'Unknown_2'}, '809d74'), \
  \$R->set({i=>[20], f=>'X'}, '-142486.91'), \
  \$R->set({i=>[20], f=>'Y'}, '204220.64'), \
  \$R->set({i=>[20], f=>'Z'}, '227.79'), \
  \$R->set({i=>[20], f=>'Z_Angle'}, '5.38')"
${tes3cmd} modify "${npc1_totsp}" --hide-backups --type NPC_ --exact-id "veresa alver" --run "\
  \$R->set({t=>'AIDT', f=>'Unknown_2'}, '809d74'), \
  \$R->set({t=>'DODT', f=>'X'}, '-116443.75'), \
  \$R->set({t=>'DODT', f=>'Y'}, '185971.22'), \
  \$R->set({t=>'DODT', f=>'Z'}, '399.99'), \
  \$R->set({t=>'DODT', f=>'Z_Angle'}, '6.16')"

# 2.2 NPC only - Both Boys in the Hood and Wear a Mask

${tes3cmd} dump "${out2_compat}" --raw-with-header "${npc2_hood_mask}" --type MISC --id "_key_"
${tes3cmd} dump "${out2_compat}" --raw "${npc2_hood_mask}" --type LIGH
${tes3cmd} dump "${out2_compat}" --raw "${npc2_hood_mask}" --type NPC_
${tes3cmd} dump "${source_hood}" --raw "${npc2_hood_mask}" --type NPC_\
  --exact-id "dralcea arethi"\
  --exact-id "fadila balvel"\
  --exact-id "rararyn radarys"

# first fix 3 NPCs according to Patch for Purists
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "dralcea arethi"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Dralcea Arethi')"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "fadila balvel"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Fadila Balvel')"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "rararyn radarys"\
  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Rarayn Radarys')"
# now append scripts from boys in the hood
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "punibi yahaz"\
  --exact-id "valveli arelas"\
  --exact-id "nireli farys"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_caravanhat_rain'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "baleni salavel"\
  --exact-id "tonas telvani"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_netchblackhat_rain'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "haema farseer"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_colth2hat_rain'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "darvame hleran"\
  --exact-id "talmeni drethan"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_mudcrabhat_rain'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "selvil sareloth"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_leatherhat_rain'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "seldus nerendus"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_strawhat_rain'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "s'virr"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_fancycolhat_rain'}))"
# now wear a mask
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "navam veran"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_ashmask_weather'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "daras aryon"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabmask_weather'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "dilami androm"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_gondagoggles_weather'}))"
${tes3cmd} modify "${npc2_hood_mask}" --hide-backups --type NPC_\
  --exact-id "rindral dralor"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabgoggles_weather'}))"

# 2.3 NPC only - Both Boys in the Hood and Wear a Mask - TOTSP

cp -v "${npc1_totsp}" "${npc3_hood_mask_totsp}"

${tes3cmd} dump "${npc2_hood_mask}" --raw "${npc3_hood_mask_totsp}" --type NPC_\
  --exact-id "dralcea arethi"\
  --exact-id "fadila balvel"\
  --exact-id "rararyn radarys"

# now append scripts from boys in the hood
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "punibi yahaz"\
  --exact-id "valveli arelas"\
  --exact-id "nireli farys"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_caravanhat_rain'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "baleni salavel"\
  --exact-id "tonas telvani"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_netchblackhat_rain'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "haema farseer"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_colth2hat_rain'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "darvame hleran"\
  --exact-id "talmeni drethan"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_mudcrabhat_rain'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "selvil sareloth"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_leatherhat_rain'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "seldus nerendus"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_strawhat_rain'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "s'virr"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_fancycolhat_rain'}))"
# now wear a mask
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "navam veran"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_ashmask_weather'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "daras aryon"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabmask_weather'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "dilami androm"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_gondagoggles_weather'}))"
${tes3cmd} modify "${npc3_hood_mask_totsp}" --hide-backups --type NPC_\
  --exact-id "rindral dralor"\
  --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabgoggles_weather'}))"

# 2.4 NPC only - LGNPC Gnaar Mok

${tes3cmd} dump "${out2_compat}" --raw-with-header "${npc4_lgnpc}" --type MISC --id "_key_"
${tes3cmd} dump "${out2_compat}" --raw "${npc4_lgnpc}" --type LIGH
${tes3cmd} dump "${out2_compat}" --raw "${npc4_lgnpc}" --type NPC_

# now append scripts from LGNPC Gnaar Mok
${tes3cmd} modify "${npc4_lgnpc}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
  --run "\$R->append(NPC_::SCRI->new({'script', 'max_LGNPC_valveli_script'}))"

# 2.5 NPC only - LGNPC Gnaar Mok - TOTSP

cp -v "${npc1_totsp}" "${npc5_lgnpc_totsp}"

# now append scripts from LGNPC Gnaar Mok
${tes3cmd} modify "${npc5_lgnpc_totsp}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
  --run "\$R->append(NPC_::SCRI->new({'script', 'max_LGNPC_valveli_script'}))"

# 2.6 NPC only - LGNPC Gnaar Mok - Both Boys in the Hood and Wear a Mask

cp -v "${npc2_hood_mask}" "${npc6_lgnpc_hood_mask}"

# now append scripts from LGNPC Gnaar Mok
${tes3cmd} modify "${npc6_lgnpc_hood_mask}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
  --run "\$R->set({t=>'SCRI', f=>'Script'}, 'max_LGNPC_valveli_script')"

# 2.7 NPC only - LGNPC Gnaar Mok - Both Boys in the Hood and Wear a Mask - TOTSP

cp -v "${npc3_hood_mask_totsp}" "${npc7_lgnpc_hood_mask_totsp}"

# now append scripts from LGNPC Gnaar Mok
${tes3cmd} modify "${npc7_lgnpc_hood_mask_totsp}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
  --run "\$R->set({t=>'SCRI', f=>'Script'}, 'max_LGNPC_valveli_script')"

# 3.1 Silt Strider Courier
# Postion of 3 couriers conflicts with Ports of Vvardenfell

${tes3cmd} dump "${source_courier}" --raw-with-header "${other1_courier}" --type CELL --exact-id "vivec (3, -9)"\
  --instance-match "Name:G93_SiltStriderCourier4"

${tes3cmd} dump "${source_courier}" --raw "${other1_courier}" --type CELL --exact-id "seyda neen (-2, -9)"\
  --instance-match "Name:G93_SiltStriderCourier2"

${tes3cmd} dump "${source_courier}" --raw "${other1_courier}" --type CELL --exact-id "balmora (-3, -3)"\
  --instance-match "Name:G93_SiltStriderCourier"

${tes3cmd} modify "${other1_courier}" --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', '${source_courier}'})), \
  \$R->append(TES3::DATA->new({'length', '49521'}))"

${tes3cmd} header "${other1_courier}" --hide-backups --synchronize

${tes3cmd} modify "${other1_courier}" --hide-backups --exact-id "balmora (-3, -3)" --sub-match "Name:G93_SiltStriderCourier" --run "\
  \$R->set({i=>[6],f=>'X'}, '-21060'), \
  \$R->set({i=>[6],f=>'Y'}, '-17780'), \
  \$R->set({t=>'FRMR', f=>'MastIdx'}, '4')"
${tes3cmd} modify "${other1_courier}" --hide-backups --exact-id "seyda neen (-2, -9)" --sub-match "Name:G93_SiltStriderCourier2" --run "\
  \$R->set({i=>[6],f=>'X'}, '-8610'), \
  \$R->set({i=>[6],f=>'Y'}, '-69905'), \
  \$R->set({i=>[6],f=>'Z'}, '823.219'), \
  \$R->set({i=>[6],f=>'Z_Angle'}, '1.23'), \
  \$R->set({t=>'FRMR', f=>'MastIdx'}, '4')"
${tes3cmd} modify "${other1_courier}" --hide-backups --exact-id "vivec (3, -9)" --sub-match "Name:G93_SiltStriderCourier" --run "\
  \$R->set({i=>[6],f=>'X'}, '31425'), \
  \$R->set({i=>[6],f=>'Y'}, '-72098'), \
  \$R->set({i=>[6],f=>'Z'}, '913.126'), \
  \$R->set({i=>[6],f=>'Z_Angle'}, '4'), \
  \$R->set({t=>'FRMR', f=>'MastIdx'}, '4')"

# create directory structure

dir00patches="pov_patches"
dir01base="${dir00patches}/01 Base"
mkdir -pv "${dir01base}"

mkdir -v "${dir01base}/01 Clean"
mv -v -t "${dir01base}/01 Clean"/ "${out1_clean}"
mkdir -v "${dir01base}/02 Compatible"
mv -v -t "${dir01base}/02 Compatible"/ "${out2_compat}"
mkdir -v "${dir01base}/03 Aggressively Compatible"
mv -v -t "${dir01base}/03 Aggressively Compatible"/ "${out3_aggro}"
mkdir -v "${dir01base}/04 Aggressively Compatible - The Strider's Nest"
mv -v -t "${dir01base}/04 Aggressively Compatible - The Strider's Nest"/ "${out4_aggro_sn}"
mkdir -v "${dir01base}/05 Ultra Aggressively Compatible"
mv -v -t "${dir01base}/05 Ultra Aggressively Compatible"/ "${out5_ultra}"
mkdir -v "${dir01base}/06 Ultra Aggressively Compatible - The Strider's Nest"
mv -v -t "${dir01base}/06 Ultra Aggressively Compatible - The Strider's Nest"/ "${out6_ultra_sn}"

dir02npc="${dir00patches}/02 NPC Only"
mkdir -pv "${dir02npc}"

mkdir -v "${dir02npc}/01 TOTSP"
mv -v -t "${dir02npc}/01 TOTSP"/ "${npc1_totsp}"
mkdir -v "${dir02npc}/02 Hood and Mask"
mv -v -t "${dir02npc}/02 Hood and Mask"/ "${npc2_hood_mask}"
mkdir -v "${dir02npc}/03 Hood and Mask - TOTSP"
mv -v -t "${dir02npc}/03 Hood and Mask - TOTSP"/ "${npc3_hood_mask_totsp}"
mkdir -v "${dir02npc}/04 LGNPC Gnaar Mok"
mv -v -t "${dir02npc}/04 LGNPC Gnaar Mok"/ "${npc4_lgnpc}"
mkdir -v "${dir02npc}/05 LGNPC Gnaar Mok - TOTSP"
mv -v -t "${dir02npc}/05 LGNPC Gnaar Mok - TOTSP"/ "${npc5_lgnpc_totsp}"
mkdir -v "${dir02npc}/06 LGNPC Gnaar Mok - Hood and Mask"
mv -v -t "${dir02npc}/06 LGNPC Gnaar Mok - Hood and Mask"/ "${npc6_lgnpc_hood_mask}"
mkdir -v "${dir02npc}/07 LGNPC Gnaar Mok - Hood and Mask - TOTSP"
mv -v -t "${dir02npc}/07 LGNPC Gnaar Mok - Hood and Mask - TOTSP"/ "${npc7_lgnpc_hood_mask_totsp}"

dir10courier="${dir00patches}/10 Silt Strider Courier"
mkdir -pv "${dir10courier}"
mv -v  -t "${dir10courier}"/ "${other1_courier}"

# Remains of TR_Travels patches. Obsolete after Tamriel Rebuilt v22.11
 
# npc1_tr="Ports Of Vvardenfell V1.6 - NPC Only - TR_Travels.esp"
# npc3_tr_totsp="Ports Of Vvardenfell V1.6 - NPC Only - TR_Travels - TOTSP.esp"
# npc5_hood_mask_tr="Ports Of Vvardenfell V1.6 - NPC Only - Hood and Mask - TR_Travels.esp"
# npc7_hood_mask_tr_totsp="Ports Of Vvardenfell V1.6 - NPC Only - Hood and Mask - TR_Travels - TOTSP.esp"
# npc9_lgnpc_tr="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - TR_Travels.esp"
# npc11_lgnpc_tr_totsp="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - TR_Travels - TOTSP.esp"
# npc13_lgnpc_hood_mask_tr="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - Hood and Mask - TR_Travels.esp"
# npc15_lgnpc_hood_mask_tr_totsp="Ports Of Vvardenfell V1.6 - NPC Only - LGNPC_GnaarMok - Hood and Mask - TR_Travels - TOTSP.esp"
 
# # 2.1 NPC only - TR_Travels

# ${tes3cmd} dump "${out2_compat}" --raw-with-header "${npc1_tr}" --type MISC --id "_key_"
# ${tes3cmd} dump "${out2_compat}" --raw "${npc1_tr}" --type LIGH
# ${tes3cmd} dump "${out2_compat}" --raw "${npc1_tr}" --type NPC_

# ${tes3cmd} delete "${npc1_tr}" --hide-backups --type NPC_ --exact-id "baleni salavel" --sub-match 'X:20380'
# ${tes3cmd} delete "${npc1_tr}" --hide-backups --type NPC_ --exact-id "baleni salavel" --sub-match 'X:113960'
# ${tes3cmd} modify "${npc1_tr}" --hide-backups --type NPC_ --exact-id "baleni salavel" --run "\
#   \$R->set({i=>[23], f=>'X'}, '-9327.44'), \
#   \$R->set({i=>[23], f=>'Y'}, '-72774.51'), \
#   \$R->set({i=>[23], f=>'Z'}, '171.48'), \
#   \$R->set({i=>[23], f=>'Z_Angle'}, '4.98')"
# ${tes3cmd} delete "${npc1_tr}" --hide-backups --type NPC_ --exact-id "nevosi hlan" --sub-match 'X:141877'
# ${tes3cmd} modify "${npc1_tr}" --hide-backups --type NPC_ --exact-id "nevosi hlan" --run "\
#   \$R->set({i=>[17], f=>'X'}, '-9330.88'), \
#   \$R->set({i=>[17], f=>'Y'}, '-72778.06'), \
#   \$R->set({i=>[17], f=>'Z'}, '171.48'), \
#   \$R->set({i=>[17], f=>'Z_Angle'}, '4.98'), \
#   \$R->set({i=>[18], f=>'X'}, '60865.54'), \
#   \$R->set({i=>[18], f=>'Y'}, '-144539.22'), \
#   \$R->set({i=>[18], f=>'Z'}, '418.29'), \
#   \$R->set({i=>[18], f=>'Z_Angle'}, '1.60')"
# ${tes3cmd} modify "${npc1_tr}" --hide-backups --type NPC_ --exact-id "daynas darys" --run "\
#   \$R->set({i=>[18], f=>'X'}, '141872.75'), \
#   \$R->set({i=>[18], f=>'Y'}, '38613.71'), \
#   \$R->set({i=>[18], f=>'Z'}, '296.26'), \
#   \$R->set({i=>[18], f=>'Z_Angle'}, '3.10')"
# ${tes3cmd} modify "${npc1_tr}" --hide-backups --type NPC_ --exact-id "haema farseer" --run "\
#   \$R->set({i=>[19], f=>'X'}, '140525.78'), \
#   \$R->set({i=>[19], f=>'Y'}, '126555.31'), \
#   \$R->set({i=>[19], f=>'Z'}, '96.64'), \
#   \$R->set({i=>[19], f=>'Z_Angle'}, '1.60'), \
#   \$R->set({i=>[21], f=>'X'}, '124191.16'), \
#   \$R->set({i=>[21], f=>'Y'}, '205391.12'), \
#   \$R->set({i=>[21], f=>'Z'}, '192.44'), \
#   \$R->set({i=>[21], f=>'Z_Angle'}, '0.90')"
# ${tes3cmd} delete "${npc1_tr}" --hide-backups --type NPC_ --exact-id "sedyni veran" --sub-match 'X:141876'
# ${tes3cmd} modify "${npc1_tr}" --hide-backups --type NPC_ --exact-id "gals arethi" --run "\
#   \$R->set({i=>[16], f=>'X'}, '207514.09'), \
#   \$R->set({i=>[16], f=>'Y'}, '6561.99'), \
#   \$R->set({i=>[16], f=>'Z'}, '207.15'), \
#   \$R->set({i=>[16], f=>'Z_Angle'}, '0.90'), \
#   \$R->set({i=>[17], f=>'X'}, '123314.12'), \
#   \$R->set({i=>[17], f=>'Y'}, '41165.79'), \
#   \$R->set({i=>[17], f=>'Z'}, '189.05'), \
#   \$R->set({i=>[17], f=>'Z_Angle'}, '0.90'), \
#   \$R->set({i=>[19], f=>'X'}, '191499.58'), \
#   \$R->set({i=>[19], f=>'Y'}, '-8647.22'), \
#   \$R->set({i=>[19], f=>'Z'}, '449.97'), \
#   \$R->set({i=>[19], f=>'Z_Angle'}, '2.20')"
# ${tes3cmd} delete "${npc1_tr}" --hide-backups --type NPC_ --exact-id "rindral dralor" --sub-match 'X:-48492'
# ${tes3cmd} modify "${npc1_tr}" --hide-backups --type NPC_ --exact-id "nireli farys" --run "\
#   \$R->set({i=>[20], f=>'X'}, '191513.56'), \
#   \$R->set({i=>[20], f=>'Y'}, '-8644.94'), \
#   \$R->set({i=>[20], f=>'Z'}, '446.75'), \
#   \$R->set({i=>[20], f=>'Z_Angle'}, '2.30'), \
#   \$R->set({i=>[21], f=>'X'}, '181072.17'), \
#   \$R->set({i=>[21], f=>'Y'}, '-140653.78'), \
#   \$R->set({i=>[21], f=>'Z'}, '206.87'), \
#   \$R->set({i=>[21], f=>'Z_Angle'}, '2.30')"
# ${tes3cmd} delete "${npc1_tr}" --hide-backups --type NPC_ --exact-id "ano andaram" --sub-match 'X:-48489'

# # 2.3 NPC only - TR_Travels and TOTSP

# cp -v "${npc1_tr}" "${npc3_tr_totsp}"

# ${tes3cmd} modify "${npc3_tr_totsp}" --hide-backups --type NPC_ --exact-id "s'virr" --run "\
#   \$R->set({i=>[14], f=>'Count'}, '220'), \
#   \$R->set({t=>'AIDT', f=>'Unknown_2'}, '040000'), \
#   \$R->set({t=>'DODT', f=>'X'}, '-116443.70'), \
#   \$R->set({t=>'DODT', f=>'Y'}, '185960.48'), \
#   \$R->set({t=>'DODT', f=>'Z'}, '400.00'), \
#   \$R->set({t=>'DODT', f=>'Z_Angle'}, '6.18')"
# ${tes3cmd} modify "${npc3_tr_totsp}" --hide-backups --type NPC_ --exact-id "basks_in_the_sun" --run "\
#   \$R->set({i=>[17], f=>'Count'}, '500'), \
#   \$R->set({t=>'AIDT', f=>'Unknown_2'}, '809d74'), \
#   \$R->set({i=>[20], f=>'X'}, '-142486.91'), \
#   \$R->set({i=>[20], f=>'Y'}, '204220.64'), \
#   \$R->set({i=>[20], f=>'Z'}, '227.79'), \
#   \$R->set({i=>[20], f=>'Z_Angle'}, '5.38')"
# ${tes3cmd} modify "${npc3_tr_totsp}" --hide-backups --type NPC_ --exact-id "veresa alver" --run "\
#   \$R->set({t=>'AIDT', f=>'Unknown_2'}, '809d74'), \
#   \$R->set({t=>'DODT', f=>'X'}, '-116443.75'), \
#   \$R->set({t=>'DODT', f=>'Y'}, '185971.22'), \
#   \$R->set({t=>'DODT', f=>'Z'}, '399.99'), \
#   \$R->set({t=>'DODT', f=>'Z_Angle'}, '6.16')"

# # 2.5 NPC only - Both Boys in the Hood and Wear a Mask - TR_Travels

# cp -v "${npc1_tr}" "${npc5_hood_mask_tr}"

# ${tes3cmd} dump "${npc4_hood_mask}" --raw "${npc5_hood_mask_tr}" --type NPC_\
#   --exact-id "dralcea arethi"\
#   --exact-id "fadila balvel"\
#   --exact-id "rararyn radarys"

# # now append scripts from boys in the hood
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "punibi yahaz"\
#   --exact-id "valveli arelas"\
#   --exact-id "nireli farys"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_caravanhat_rain'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "baleni salavel"\
#   --exact-id "tonas telvani"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_netchblackhat_rain'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "haema farseer"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_colth2hat_rain'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "darvame hleran"\
#   --exact-id "talmeni drethan"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_mudcrabhat_rain'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "selvil sareloth"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_leatherhat_rain'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "seldus nerendus"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_strawhat_rain'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "s'virr"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_fancycolhat_rain'}))"
# # now wear a mask
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "navam veran"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_ashmask_weather'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "daras aryon"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabmask_weather'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "dilami androm"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_gondagoggles_weather'}))"
# ${tes3cmd} modify "${npc5_hood_mask_tr}" --hide-backups --type NPC_\
#   --exact-id "rindral dralor"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabgoggles_weather'}))"

# # 2.7 NPC only - Both Boys in the Hood and Wear a Mask - TR_Travel - TOTSP

# cp -v "${npc3_tr_totsp}" "${npc7_hood_mask_tr_totsp}"

# ${tes3cmd} dump "${npc4_hood_mask}" --raw "${npc7_hood_mask_tr_totsp}" --type NPC_\
#   --exact-id "dralcea arethi"\
#   --exact-id "fadila balvel"\
#   --exact-id "rararyn radarys"

# # now append scripts from boys in the hood
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "punibi yahaz"\
#   --exact-id "valveli arelas"\
#   --exact-id "nireli farys"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_caravanhat_rain'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "baleni salavel"\
#   --exact-id "tonas telvani"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_netchblackhat_rain'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "haema farseer"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_colth2hat_rain'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "darvame hleran"\
#   --exact-id "talmeni drethan"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_mudcrabhat_rain'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "selvil sareloth"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_leatherhat_rain'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "seldus nerendus"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_strawhat_rain'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "s'virr"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_fancycolhat_rain'}))"
# # now wear a mask
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "navam veran"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_ashmask_weather'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "daras aryon"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabmask_weather'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "dilami androm"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_gondagoggles_weather'}))"
# ${tes3cmd} modify "${npc7_hood_mask_tr_totsp}" --hide-backups --type NPC_\
#   --exact-id "rindral dralor"\
#   --run "\$R->append(NPC_::SCRI->new({'script', '_VD_crabgoggles_weather'}))"

# # 2.9 NPC only - LGNPC Gnaar Mok - TR_Travels

# cp -v "${npc1_tr}" "${npc9_lgnpc_tr}"

# # now append scripts from LGNPC Gnaar Mok
# ${tes3cmd} modify "${npc9_lgnpc_tr}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
#   --run "\$R->append(NPC_::SCRI->new({'script', 'max_LGNPC_valveli_script'}))"

# # 2.11 NPC only - LGNPC Gnaar Mok - TR_Travels and TOTSP

# cp -v "${npc3_tr_totsp}" "${npc11_lgnpc_tr_totsp}"

# # now append scripts from LGNPC Gnaar Mok
# ${tes3cmd} modify "${npc11_lgnpc_tr_totsp}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
#   --run "\$R->append(NPC_::SCRI->new({'script', 'max_LGNPC_valveli_script'}))"

# # 2.13 NPC only - LGNPC Gnaar Mok - Both Boys in the Hood and Wear a Mask - TR_Travels

# cp -v "${npc5_hood_mask_tr}" "${npc13_lgnpc_hood_mask_tr}"

# # now append scripts from LGNPC Gnaar Mok
# ${tes3cmd} modify "${npc13_lgnpc_hood_mask_tr}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
#   --run "\$R->set({t=>'SCRI', f=>'Script'}, 'max_LGNPC_valveli_script')"

# # 2.15 NPC only - LGNPC Gnaar Mok - Both Boys in the Hood and Wear a Mask - TR_Travel - TOTSP

# cp -v "${npc7_hood_mask_tr_totsp}" "${npc15_lgnpc_hood_mask_tr_totsp}"

# # now append scripts from LGNPC Gnaar Mok
# ${tes3cmd} modify "${npc15_lgnpc_hood_mask_tr_totsp}" --hide-backups --type NPC_ --exact-id "valveli arelas"\
#   --run "\$R->set({t=>'SCRI', f=>'Script'}, 'max_LGNPC_valveli_script')"

# mkdir -v "${dir02npc}/01 TR_Travels"
# mv -v -t "${dir02npc}/01 TR_Travels"/ "${npc1_tr}"
# mkdir -v "${dir02npc}/03 TR_Travels - TOTSP"
# mv -v -t "${dir02npc}/03 TR_Travels - TOTSP"/ "${npc3_tr_totsp}"
# mkdir -v "${dir02npc}/05 Hood and Mask - TR_Travels"
# mv -v -t "${dir02npc}/05 Hood and Mask - TR_Travels"/ "${npc5_hood_mask_tr}"
# mkdir -v "${dir02npc}/07 Hood and Mask - TR_Travels - TOTSP"
# mv -v -t "${dir02npc}/07 Hood and Mask - TR_Travels - TOTSP"/ "${npc7_hood_mask_tr_totsp}"
# mkdir -v "${dir02npc}/09 LGNPC Gnaar Mok - TR_Travels"
# mv -v -t "${dir02npc}/09 LGNPC Gnaar Mok - TR_Travels"/ "${npc9_lgnpc_tr}"
# mkdir -v "${dir02npc}/11 LGNPC Gnaar Mok - TR_Travels - TOTSP"
# mv -v -t "${dir02npc}/11 LGNPC Gnaar Mok - TR_Travels - TOTSP"/ "${npc11_lgnpc_tr_totsp}"
# mkdir -v "${dir02npc}/13 LGNPC Gnaar Mok - Hood and Mask - TR_Travels"
# mv -v -t "${dir02npc}/13 LGNPC Gnaar Mok - Hood and Mask - TR_Travels"/ "${npc13_lgnpc_hood_mask_tr}"
# mkdir -v "${dir02npc}/15 LGNPC Gnaar Mok - Hood and Mask - TR_Travels - TOTSP"
# mv -v -t "${dir02npc}/15 LGNPC Gnaar Mok - Hood and Mask - TR_Travels - TOTSP"/ "${npc15_lgnpc_hood_mask_tr_totsp}"
