#!/usr/bin/env bash
# Changelog:
# 2021-09-25
#   - Make Blighted Animals Retextured BTBGI Patch after blighed rat got changed in BTBGI - NE 2.0.8
#   - Make patch for diseased shalk after it got changed in BTBGI - NE 2.0.8
#
# 2021-10-16
#   - Bloodmoon Rebalance now contain PFP fixes for Plague Bear and Wolf
#   - TriangleTooth's Ecology Mod now contains PFP fixes as well for Plague Bear and Wolf (same way as BTB's diseased creatures - via leveled lists)
#
# 2022-05-10
#   - Switch back to update Bloodmoon Rebalance from "Bloodmoon Rebalance - PFP" Patch as the latter is no longer required
#
# 2022-07-22
#   - Add Dwemer Rebirth Project(dreugh)
#
# 2022-12-03
#   - Remove TR_Mainland patch - it's not needed and should've not been created
#
# Here i use "tes3cmd modify --replace" with 0.40. To be able to do this i've commented lines 8665-8667 of tes3cmd.
# Regexp validation seems to be broken already, commenting those lines just disables exit on validation error. Now --replace works, just make sure to pass correct regexps ;-)
tes3cmd="./tes3cmd"
# tempfile will be used for modifying lots of LEVC to avoid listing them all 1 by 1
tempfile_levc="tempfile_levc.esp"

# 01 Base game and Patch for Purists
result="avp BTBGI-NE - No Creature Buffs - NDL - Base.esp"
$tes3cmd dump "YANDL - Base.esp" --raw-with-header "${result}" --type CREA
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "rat_blighted"   --run "\$R->set({t=>'NPDT',f=>'Str'}, '55')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "shalk_diseased" --run "\$R->set({t=>'NPDT',f=>'Spd'}, '30')"

# 02 Blighted Animals Retextured
result="avp BTBGI-NE - No Creature Buffs - NDL - BlightedAnimalsRetextured.esp"
$tes3cmd dump "YANDL - BlightedAnimalsRetextured.esp" --raw-with-header "${result}" --type CREA
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "rat_blighted" --run "\$R->set({t=>'NPDT',f=>'Str'}, '55')"

# 03 Tribunal Rebalance
result="avp BTBGI-NE - No Creature Buffs - NDL - tribunal rebalance.esp"
$tes3cmd dump "YANDL - tribunal rebalance.esp" --raw-with-header "${result}" --type CREA

# 04 Bloodmoon Rebalance
result="avp BTBGI-NE - No Creature Buffs - NDL - Bloodmoon Rebalance.esp"
$tes3cmd dump "YANDL - Bloodmoon Rebalance.esp" --raw-with-header "${result}" --type CREA

# 05 Tamriel Data
result="avp BTBGI-NE - No Creature Buffs - NDL - Tamriel_Data.esp"
$tes3cmd dump "YANDL - Tamriel_Data.esp" --raw-with-header "${result}" --type CREA

# 2022-12-03 - Remove TR_Mainland patch - it's not needed and should've not been created
# 06 Tamriel Rebuilt
# result="avp BTBGI-NE - No Creature Buffs - NDL - TR_Mainland.esp"
# $tes3cmd dump "YANDL - TR_Mainland.esp" --raw-with-header "${result}" --type CREA

# 07 DRPV1.9
result="avp BTBGI-NE - No Creature Buffs - NDL - DRPV1.9.esp"
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw-with-header "${result}" --type CREA --exact-id "dreugh"
$tes3cmd header "${result}" --hide-backups --update-record-count

# 08 TriangleTooth's Ecology Mod 
result="avp BTBGI-NE - No Creature Buffs - NDL - Ttooth Ecology.esp"
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw-with-header "${result}" --type CREA\
  --exact-id "guar_feral" \
  --exact-id "netch_betty" \
  --exact-id "netch_bull" \
  --exact-id "nix-hound" \
  --exact-id "dreugh"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "guar_feral"         --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_btb_guar_feral')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "avp_btb_guar_feral" --run "\$R->set({t=>'FNAM', f=>'Name'},  'Feral Guar')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "netch_betty"        --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_btb_netch_betty')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "netch_bull"         --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_btb_netch_bull')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "nix-hound"          --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_btb_nix-hound')"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd dump "Patch for Purists.esm" --raw "${result}" --type CREA\
  --exact-id "bm_bear_brown"\
  --exact-id "bm_wolf_red"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_bear_brown"      --run "\$R->set({t=>'FNAM', f=>'Name'},  'Bear')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_wolf_red"        --run "\$R->set({t=>'FNAM', f=>'Name'},  'Wolf')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_bear_brown"      --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_pfp_bm_bear_brown')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_wolf_red"        --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_pfp_bm_wolf_red')"
$tes3cmd dump "Ttooth Ecology.esp" --raw "${result}" --type CREA\
  --exact-id "guar_feral" \
  --exact-id "netch_betty" \
  --exact-id "netch_bull" \
  --exact-id "nix-hound"
#  --exact-id "dreugh" \
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA \
  --exact-id "dreugh" --run "\
  \$R->set({t=>'NPDT',f=>'Lev'}, '8'), \
  \$R->set({t=>'NPDT',f=>'Health'}, '120'), \
  \$R->set({t=>'NPDT',f=>'Soul'}, '75'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_1'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_2'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_3'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_1'}, '20'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_2'}, '20'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_3'}, '20')"
# LEVC
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC --exact-id "in_dae_atronach_lev+2"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_btb_guar_feral_levc'), \
  \$R->set({t=>'INDX',f=>'Item_Count'}, '3'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'guar_feral'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'guar_feral'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_guar_feral'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC --exact-id "in_dae_atronach_lev+2"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_btb_netch_betty_levc'), \
  \$R->set({t=>'INDX',f=>'Item_Count'}, '3'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'netch_betty'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'netch_betty'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_netch_betty'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC --exact-id "in_dae_atronach_lev+2"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_btb_netch_bull_levc'), \
  \$R->set({t=>'INDX',f=>'Item_Count'}, '3'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'netch_bull'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'netch_bull'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_netch_bull'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC --exact-id "in_dae_atronach_lev+2"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_btb_nix-hound_levc'), \
  \$R->set({t=>'INDX',f=>'Item_Count'}, '3'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'nix-hound'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'nix-hound'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_nix-hound'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC --exact-id "in_dae_atronach_lev+2"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_pfp_bm_bear_brown_levc'), \
  \$R->set({t=>'INDX',f=>'Item_Count'}, '3'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'bm_bear_brown'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'bm_bear_brown'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_pfp_bm_bear_brown'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC --exact-id "in_dae_atronach_lev+2"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "in_dae_atronach_lev+2" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_pfp_bm_wolf_red_levc'), \
  \$R->set({t=>'INDX',f=>'Item_Count'}, '3'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'bm_wolf_red'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'bm_wolf_red'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_pfp_bm_wolf_red'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Ttooth Ecology.esp" --raw-with-header "${tempfile_levc}" --type LEVC --match "netch_bull|netch_betty|guar_feral|nix-hound"
$tes3cmd dump "Morrowind.esm" --raw "${tempfile_levc}" --type LEVC \
  --exact-id "ex_ascadianisles_sleep" \
  --exact-id "ex_azurascoast_sleep" \
  --exact-id "ex_bittercoast_sleep" \
  --exact-id "ex_grazelands_sleep" \
  --exact-id "ex_molagmar_sleep" \
  --exact-id "ex_westgash_sleep" \
  --exact-id "ex_wild_all_sleep" \
  --exact-id "in_cave_nix_lev+0" \
  --exact-id "in_cave_nix_lev+2" \
  --exact-id "in_cave_nix_lev-1"
$tes3cmd modify "${tempfile_levc}" --hide-backups --type LEVC --match "_ID:guar_feral"           --replace '/^guar_feral$/avp_btb_guar_feral_levc/'
$tes3cmd modify "${tempfile_levc}" --hide-backups --type LEVC --match "_ID:netch_betty"          --replace '/^netch_betty$/avp_btb_netch_betty_levc/'
$tes3cmd modify "${tempfile_levc}" --hide-backups --type LEVC --match "_ID:netch_bull"           --replace '/^netch_bull$/avp_btb_netch_bull_levc/'
$tes3cmd modify "${tempfile_levc}" --hide-backups --type LEVC --match "_ID:nix-hound"            --replace '/^nix-hound$/avp_btb_nix-hound_levc/'
$tes3cmd dump "Bloodmoon.esm" --raw "${tempfile_levc}" --type LEVC --match "bm_bear_brown|bm_wolf_red"
$tes3cmd modify "${tempfile_levc}" --hide-backups --type LEVC --match "_ID:BM_bear_brown"         --replace '/^BM_bear_brown$/avp_pfp_bm_bear_brown_levc/'
$tes3cmd modify "${tempfile_levc}" --hide-backups --type LEVC --match "_ID:BM_wolf_red"           --replace '/^BM_wolf_red$/avp_pfp_bm_wolf_red_levc/'
$tes3cmd dump "${tempfile_levc}" --raw "${result}" --type LEVC
rm "${tempfile_levc}"
$tes3cmd header "${result}" --hide-backups --update-record-count
# Rename
$tes3cmd dump "Ttooth Ecology.esp" --raw "${result}" --type CREA\
  --exact-id "alit_diseased" \
  --exact-id "cliff racer_diseased" \
  --exact-id "kagouti_diseased" \
  --exact-id "mudcrab-diseased" \
  --exact-id "rat_diseased" \
  --exact-id "shalk_diseased" \
  --exact-id "ttooth_wolf_red_pup"\
  --exact-id "ttooth_bear_brown_cub"\
  --exact-id "bm_bear_brown"\
  --exact-id "bm_wolf_red"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "shalk_diseased"        --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Shalk')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "shalk_diseased"        --run "\$R->set({t=>'NPDT',f=>'Spd'}, '30')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "cliff racer_diseased"  --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Cliff Racer')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "alit_diseased"         --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Alit')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "kagouti_diseased"      --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Kagouti')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "rat_diseased"          --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Rat')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "mudcrab-diseased"      --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Mudcrab')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "ttooth_wolf_red_pup"   --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Wolf Pup')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "ttooth_bear_brown_cub" --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Bear Cub')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_bear_brown"         --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Bear')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_wolf_red"           --run "\$R->set({t=>'FNAM', f=>'Name'}, 'Wolf')"

# 08 TriangleTooth's Ecology Mod - Bloodmoon Rebalance
result="avp BTBGI-NE - No Creature Buffs - NDL - Ttooth Ecology - BMR Patch.esp"
$tes3cmd dump "YANDL - Bloodmoon Rebalance.esp" --raw-with-header "${result}" --type CREA\
  --exact-id "bm_bear_brown"\
  --exact-id "bm_wolf_red"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_bear_brown"      --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_pfp_bm_bear_brown')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "bm_wolf_red"        --run "\$R->set({t=>'NAME', f=>'ID'},    'avp_pfp_bm_wolf_red')"
$tes3cmd dump "YANDL - Ttooth Ecology - BMR Patch.esp" --raw "${result}" --type CREA
$tes3cmd header "${result}" --hide-backups --update-record-count

# 10 TriangleTooth's Ecology Mod - Tamriel Rebuilt
result="avp BTBGI-NE - No Creature Buffs - NDL - Ttooth Ecology - TR Patch.esp"
$tes3cmd dump "Tamriel_Data.esm" --raw-with-header "${result}" --type LEVC --match "netch_bull|netch_betty|guar_feral|nix-hound"
$tes3cmd modify "${result}" --hide-backups --type LEVC --match "_ID:guar_feral"           --replace '/guar_feral$/avp_btb_guar_feral_levc/'
$tes3cmd modify "${result}" --hide-backups --type LEVC --match "_ID:netch_betty"          --replace '/netch_betty$/avp_btb_netch_betty_levc/'
$tes3cmd modify "${result}" --hide-backups --type LEVC --match "_ID:netch_bull"           --replace '/netch_bull$/avp_btb_netch_bull_levc/'
$tes3cmd modify "${result}" --hide-backups --type LEVC --match "_ID:nix-hound"            --replace '/nix-hound$/avp_btb_nix-hound_levc/'
# Rename
$tes3cmd dump "YANDL - Ttooth Ecology - TR Patch.esp" --raw "${result}" --type CREA
$tes3cmd header "${result}" --hide-backups --update-record-count

# 11 abot Waterlife
result="avp BTBGI-NE - No Creature Buffs - NDL - abotWaterLife.esp"
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw-with-header "${result}" --type CREA \
  --exact-id "dreugh"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "dreugh" --run "\$R->set({t=>'NAME', f=>'ID'}, 'avp_btb_dreugh')"
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw "${result}" --type CREA \
  --exact-id "slaughterfish_small"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "slaughterfish_small"   --run "\$R->set({t=>'NAME', f=>'ID'}, 'avp_btb_slfish_small')"
$tes3cmd dump "abotWaterLife.esm" --raw "${result}" --type CREA \
  --exact-id "dreugh" \
  --exact-id "slaughterfish_small"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC \
  --exact-id "h2o_all_lev+0"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_abot_btb_dreugh_levc'), \
  \$R->set({t=>'NNAM',f=>'Chance_None'}, '0'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'dreugh'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'dreugh'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_dreugh'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC \
  --exact-id "h2o_all_lev+0"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_abot_btb_slfish_small_levc'), \
  \$R->set({t=>'NNAM',f=>'Chance_None'}, '0'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'Slaughterfish_Small'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'Slaughterfish_Small'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_slfish_small'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC \
  --exact-id "h2o_all_lev+2" \
  --exact-id "h2o_all_lev+0" \
  --exact-id "h2o_all_lev-2" \
  --exact-id "h2o_slaughterfish"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type LEVC \
  --exact-id "h2o_all_lev+0" \
  --exact-id "h2o_all_lev-2" --run "\
  \$R->set({i=>[6],f=>'Creature_ID'}, 'avp_abot_btb_slfish_small_levc'), \
  \$R->set({i=>[8],f=>'Creature_ID'}, 'avp_abot_btb_dreugh_levc')"
$tes3cmd modify "${result}" --hide-backups --type LEVC \
  --exact-id "h2o_all_lev+2" --run "\
  \$R->set({i=>[4],f=>'Creature_ID'}, 'avp_abot_btb_slfish_small_levc'), \
  \$R->set({i=>[8],f=>'Creature_ID'}, 'avp_abot_btb_dreugh_levc')"
$tes3cmd modify "${result}" --hide-backups --type LEVC \
  --exact-id "h2o_slaughterfish" --run "\
  \$R->set({i=>[4],f=>'Creature_ID'}, 'avp_abot_btb_slfish_small_levc')"

# 12 abot Waterlife with dreugh changes from Ttooth for use together with Ttooth
result="avp BTBGI-NE - No Creature Buffs - NDL - abotWaterLife - Ttooth Ecology Dreugh.esp"
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw-with-header "${result}" --type CREA \
  --exact-id "dreugh"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA \
  --exact-id "dreugh" --run "\
  \$R->set({t=>'NPDT',f=>'Lev'}, '8'), \
  \$R->set({t=>'NPDT',f=>'Health'}, '120'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_1'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_2'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_3'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_1'}, '20'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_2'}, '20'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_3'}, '20')"
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "dreugh" --run "\$R->set({t=>'NAME', f=>'ID'}, 'avp_btb_dreugh')"
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw "${result}" --type CREA \
  --exact-id "slaughterfish_small"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA --exact-id "slaughterfish_small"   --run "\$R->set({t=>'NAME', f=>'ID'}, 'avp_btb_slfish_small')"
$tes3cmd dump "abotWaterLife.esm" --raw "${result}" --type CREA \
  --exact-id "dreugh" \
  --exact-id "slaughterfish_small"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type CREA \
  --exact-id "dreugh" --run "\
  \$R->set({t=>'NPDT',f=>'Lev'}, '8'), \
  \$R->set({t=>'NPDT',f=>'Health'}, '120'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_1'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_2'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Min_3'}, '10'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_1'}, '20'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_2'}, '20'), \
  \$R->set({t=>'NPDT',f=>'Attack_Max_3'}, '20')"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC \
  --exact-id "h2o_all_lev+0"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_abot_btb_dreugh_levc'), \
  \$R->set({t=>'NNAM',f=>'Chance_None'}, '0'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'dreugh'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'dreugh'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_dreugh'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC \
  --exact-id "h2o_all_lev+0"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Creature_ID:"
$tes3cmd delete "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --sub-match "Level:"
$tes3cmd modify "${result}" --hide-backups --type LEVC --exact-id "h2o_all_lev+0" --run "\
  \$R->set({t=>'NAME',f=>'ID'}, 'avp_abot_btb_slfish_small_levc'), \
  \$R->set({t=>'NNAM',f=>'Chance_None'}, '0'), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'Slaughterfish_Small'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'Slaughterfish_Small'})), \
  \$R->append(LEVC::INTV->new({'level', '1'})), \
  \$R->append(LEVC::CNAM->new({'creature_id', 'avp_btb_slfish_small'})), \
  \$R->append(LEVC::INTV->new({'level', '1'}))"
$tes3cmd dump "Morrowind.esm" --raw "${result}" --type LEVC \
  --exact-id "h2o_all_lev+2" \
  --exact-id "h2o_all_lev+0" \
  --exact-id "h2o_all_lev-2" \
  --exact-id "h2o_slaughterfish"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type LEVC \
  --exact-id "h2o_all_lev+0" \
  --exact-id "h2o_all_lev-2" --run "\
  \$R->set({i=>[6],f=>'Creature_ID'}, 'avp_abot_btb_slfish_small_levc'), \
  \$R->set({i=>[8],f=>'Creature_ID'}, 'avp_abot_btb_dreugh_levc')"
$tes3cmd modify "${result}" --hide-backups --type LEVC \
  --exact-id "h2o_all_lev+2" --run "\
  \$R->set({i=>[4],f=>'Creature_ID'}, 'avp_abot_btb_slfish_small_levc'), \
  \$R->set({i=>[8],f=>'Creature_ID'}, 'avp_abot_btb_dreugh_levc')"
$tes3cmd modify "${result}" --hide-backups --type LEVC \
  --exact-id "h2o_slaughterfish" --run "\
  \$R->set({i=>[4],f=>'Creature_ID'}, 'avp_abot_btb_slfish_small_levc')"
