#!/usr/bin/env bash
# Changelog:
# 2023-05-23
#   - Add "No masters" private version for easier merging with Habasi
#
# 2022-09-18
#   - Remove doubled entries for SPEL records "Trollkin", "Fay", "Blood of the North"
#   - Disable "synchronize" to prevent setting updated filesizes for slightly modified Sensible masters
#
# 2022-08-10
#   - Add masters to allow easier editing
#
# 2021-11-17
#   - Change source to Races Module after splitting it from main in BTBGI - Necro Edit v2.0.12
#   - Copy BTB unchanged BSGN and SPEL into the patch to decouple completely (mark: after split)
#
# 2021-07-07
#   - Reorder records and subrecords accoring to readme
#
# 2021-06-06
#   - Complete remake
#
# vim: nowrap
# command used for formatting text:
#  skills:  '<,'>EasyAlign 4/\-\-/r0 ; '<,'>EasyAlign 1/ \\\$R/r0
#  further: '<,'>EasyAlign 4/\-\-/r0 ; '<,'>EasyAlign / '/r0 ; '<,'>EasyAlign 2/ \\/r0
result="avp BTBGI-NE - Sensible Races and Birthsigns - Custom edit - No masters.esp"
result_with_masters="avp BTBGI-NE - Sensible Races and Birthsigns - Custom edit.esp"
source_sens_race="SensibleRaces.omwaddon"
source_sens_bsgn="SensibleBirthsigns.omwaddon"
source_morrowind="Morrowind.esm"
# source_btb_gi_ne="BTB's Game Improvements (Necro Edit).esp"
# after split
source_btb_gi_ne="BTBGI Races and Birthsigns.esp"
tes3cmd="./tes3cmd"

# after split
# this one is specific to my setup, just get required esp into build dir
source_btb_gi_ne_to_copy="../../mods/Gameplay/BTBGameImprovementsNecroEdit/0/00 Core/BTBGI Races and Birthsigns.esp.0.not.used.BAK"
cp -v "${source_btb_gi_ne_to_copy}" "${source_btb_gi_ne}"

# First RACE records
$tes3cmd dump "${source_sens_race}" --raw-with-header "${result}" --type RACE \
 --exact-id "high elf" \
 --exact-id "argonian" \
 --exact-id "wood elf" \
 --exact-id "dark elf" \
 --exact-id "imperial" \
 --exact-id "khajiit" \
 --exact-id "nord" \
 --exact-id "orc"
$tes3cmd dump "${source_morrowind}" --raw "${result}" --type RACE \
 --exact-id "breton" \
 --exact-id "redguard"
# Birthsigns
$tes3cmd dump "${source_morrowind}" --raw "${result}" --type BSGN \
 --exact-id "fay" \
 --exact-id "mooncalf" \
 --exact-id "trollkin"
$tes3cmd dump "${source_sens_bsgn}" --raw "${result}" --type BSGN \
 --exact-id "star-cursed"
# after split
$tes3cmd dump "${source_btb_gi_ne}" --raw "${result}" --type BSGN \
 --exact-id "elfborn" \
 --exact-id "lady's favor"
# Finally SPEL records
$tes3cmd dump "${source_sens_race}" --raw "${result}" --type SPEL \
 --exact-id "_naturalArmor"
$tes3cmd dump "${source_morrowind}" --raw "${result}" --type SPEL \
 --exact-id "charioteer ability" \
 --exact-id "elfborn ability" \
 --exact-id "warywrd ability"
$tes3cmd dump "${source_btb_gi_ne}" --raw "${result}" --type SPEL \
 --exact-id "blood of the north" \
 --exact-id "fay ability" \
 --exact-id "lady's grace" \
 --exact-id "lady's touch"
# after split
$tes3cmd dump "${source_btb_gi_ne}" --raw "${result}" --type SPEL \
 --exact-id "adrenaline rush"\
 --exact-id "ancestor guardian"\
 --exact-id "arcane ancestry"\
 --exact-id "black death"\
 --exact-id "dragon skin"\
 --exact-id "eye of fear"\
 --exact-id "immune to paralysis"\
 --exact-id "lover's kiss"\
 --exact-id "magicka mult bonus_20"\
 --exact-id "orc weapon resistance"\
 --exact-id "orc_beserk"\
 --exact-id "thunder fist"\
 --exact-id "voice of the emperor"\
 --exact-id "wizard's brand"\
 --exact-id "woad"\
 --exact-id "wombburn"
# 2022-09-18 - Remove doubled entries for SPEL records "Trollkin", "Fay", "Blood of the North"
# --exact-id "star of the west"\
# --exact-id "fay ability"\
# --exact-id "blood of the north"\
$tes3cmd dump "${source_sens_bsgn}" --raw "${result}" --type SPEL \
 --exact-id "_elusive" \
 --exact-id "_maraProtect"
$tes3cmd header "${result}" --hide-backups --update-record-count

# 1. Skills

# 1.1. Breton # conj 10 -> enchant 5, unarmored 5
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "breton"   --run "\$R->set({t=>'RADT', f=>'skill_3'}, '9'),   \$R->set({t=>'RADT', f=>'bonus_3'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "breton"   --run "\$R->set({t=>'RADT', f=>'skill_7'}, '25'),  \$R->set({t=>'RADT', f=>'bonus_7'}, '5')"
# 1.2. Dunmer # long blade -> conjuration, requires rearrange of skills
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "dark elf" --run "\$R->set({t=>'RADT', f=>'skill_2'}, '13'),  \$R->set({t=>'RADT', f=>'bonus_2'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "dark elf" --run "\$R->set({t=>'RADT', f=>'skill_3'}, '10'),  \$R->set({t=>'RADT', f=>'bonus_3'}, '10')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "dark elf" --run "\$R->set({t=>'RADT', f=>'skill_4'}, '21'),  \$R->set({t=>'RADT', f=>'bonus_4'}, '5')"
# 1.3. Imperial # blunt weapon, hand to hand?  -> block 10?
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "imperial" --run "\$R->set({t=>'RADT', f=>'skill_1'}, '0'),   \$R->set({t=>'RADT', f=>'bonus_1'}, '5')"
# 1.4. Nord # rearrange, armorer, axe, block, heavy, unarmored, mercantile
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"     --run "\$R->set({t=>'RADT', f=>'skill_1'}, '1'),   \$R->set({t=>'RADT', f=>'bonus_1'}, '10')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"     --run "\$R->set({t=>'RADT', f=>'skill_2'}, '6'),   \$R->set({t=>'RADT', f=>'bonus_2'}, '10')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"     --run "\$R->set({t=>'RADT', f=>'skill_3'}, '0'),   \$R->set({t=>'RADT', f=>'bonus_3'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"     --run "\$R->set({t=>'RADT', f=>'skill_4'}, '3'),   \$R->set({t=>'RADT', f=>'bonus_4'}, '10')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"     --run "\$R->set({t=>'RADT', f=>'skill_5'}, '17'),  \$R->set({t=>'RADT', f=>'bonus_5'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"     --run "\$R->set({t=>'RADT', f=>'skill_6'}, '24'),  \$R->set({t=>'RADT', f=>'bonus_6'}, '5')"
# 1.5. Orc # rearrange, axe -> blunt, heavy -> athletics
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "orc"      --run "\$R->set({t=>'RADT', f=>'skill_1'}, '1'),   \$R->set({t=>'RADT', f=>'bonus_1'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "orc"      --run "\$R->set({t=>'RADT', f=>'skill_2'}, '8'),   \$R->set({t=>'RADT', f=>'bonus_2'}, '10')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "orc"      --run "\$R->set({t=>'RADT', f=>'skill_4'}, '4'),   \$R->set({t=>'RADT', f=>'bonus_4'}, '10')"
# 1.6. Redguard # acrobatice, athletics, armorer, block, long blade, medium, hand to hand?
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard" --run "\$R->set({t=>'RADT', f=>'skill_1'}, '20'),  \$R->set({t=>'RADT', f=>'bonus_1'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard" --run "\$R->set({t=>'RADT', f=>'skill_2'}, '8'),   \$R->set({t=>'RADT', f=>'bonus_2'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard" --run "\$R->set({t=>'RADT', f=>'skill_3'}, '0'),   \$R->set({t=>'RADT', f=>'bonus_3'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard" --run "\$R->set({t=>'RADT', f=>'skill_4'}, '26'),  \$R->set({t=>'RADT', f=>'bonus_4'}, '5')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard" --run "\$R->set({t=>'RADT', f=>'skill_7'}, '17'),  \$R->set({t=>'RADT', f=>'bonus_7'}, '5')"

# 2. Abilities and powers

# 2.1. Altmer # lower weakness to magicka to 25%, add reflect, arcane mastery, increase magicka pool
# here and further i use "weakness magicka_50" as a template for different spells
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_weakness_magicka_25'),    \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_reflect_25'),             \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Arcane Acumen'),               \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '68'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"
# when i change something i'd have to readd descriptions and sometimes change order of subrecords to keep things nice
# $tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "high elf"                 --sub-match "Spell:weakness magicka_50"
# $tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "high elf"                 --sub-match "Spell:magicka mult bonus_15"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "high elf"                 --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "high elf"                 --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "high elf"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             'magicka mult bonus_20'})),     \
  \$R->append(RACE::NPCS->new({'spell',             'resist disease_75'})),         \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_reflect_25'})),           \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_weakness_magicka_25'})),  \
  \$R->append(RACE::NPCS->new({'spell',             '_fireWeakness25'})),           \
  \$R->append(RACE::NPCS->new({'spell',             'weakness frost_25'})),         \
  \$R->append(RACE::NPCS->new({'spell',             'weakness shock_25'})),         \
  \$R->append(RACE::NPCS->new({'spell',             'arcane ancestry'})),           \
  \$R->append(RACE::DESC->new({'description', 'The High Elves consider themselves the most civilized culture of Tamriel; the common tongue of the Empire, Tamrielic, is based on Altmer speech and writing, and most of the Empire\'s arts, crafts, and sciences derive from High Elven traditions. Deft, intelligent, and strong-willed, High Elves are often gifted in the arcane arts, and High Elves boast that their sublime physical natures make them far more resistant to disease than the \"lesser races.\"'}))"

# 2.2. Argonian # Increase resist normal weapons to 25%, add black death from BTB
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "_naturalArmor"            --run "
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "argonian"                 --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "argonian"                 --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "argonian"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             '_racialArgonian_00'})),        \
  \$R->append(RACE::NPCS->new({'spell',             'immune to poison'})),          \
  \$R->append(RACE::NPCS->new({'spell',             'resist disease_75'})),         \
  \$R->append(RACE::NPCS->new({'spell',             '_naturalArmor'})),             \
  \$R->append(RACE::NPCS->new({'spell',             'black death'})),               \
  \$R->append(RACE::DESC->new({'description', 'At home in water and on land, the Argonians of Black Marsh are well-suited to the treacherous swamps of their homeland, with natural immunities protecting them from disease and poison. The female life-phase is highly intelligent, and gifted in the magical arts. The more aggressive male phase has the traits of the hunter: stealth, speed, and agility. Argonians are reserved with strangers, yet fiercely loyal to those they accept as friends.'}))"

# 2.3. Bosmer # add resist paralysis 100% from BTB, add resist poison 50
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "wood elf"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             'immune to paralysis'}))"
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_resist_poison_50'),       \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Poison'),               \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '97')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "wood elf"                 --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "wood elf"                 --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "wood elf"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             '_racialBosmer_00'})),          \
  \$R->append(RACE::NPCS->new({'spell',             'immune to paralysis'})),       \
  \$R->append(RACE::NPCS->new({'spell',             'resist disease_75'})),         \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_resist_poison_50'})),     \
  \$R->append(RACE::NPCS->new({'spell',             '_beastTongue'})),              \
  \$R->append(RACE::DESC->new({'description', 'The Wood Elves are the various barbarian Elven clanfolk of the Western Valenwood forests. These country cousins of the High Elves and Dark Elves are nimble and quick in body and wit, and because of their curious natures and natural agility, Wood Elves are especially suitable as scouts, agents, and thieves. But most of all, the Wood Elves are known for their skills with bows; there are no finer archers in all of Tamriel.'}))"

# 2.4. Breton # add resist paralysis 50%, add magicka from BTB
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_resist_paralysis_50'),    \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Paralysis'),            \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '99'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '50'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '50')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "breton"                   --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "breton"                   --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "breton"                   --run "
  \$R->append(RACE::NPCS->new({'spell',             'magicka mult bonus_10'})),     \
  \$R->append(RACE::NPCS->new({'spell',             'resist magicka_50'})),         \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_resist_paralysis_50'})),  \
  \$R->append(RACE::NPCS->new({'spell',             'dragon skin'})),               \
  \$R->append(RACE::DESC->new({'description', 'Passionate and eccentric, poetic and flamboyant, intelligent and willful, the Bretons feel an inborn, instinctive bond with the mercurial forces of magic and the supernatural. Many great sorcerers have come out of their home province of High Rock, and in addition to their quick and perceptive grasp of spellcraft, enchantment, and alchemy, even the humblest of Bretons can boast a high resistance to destructive and dominating magical energies.'}))"

# 2.5. Dunmer # just to reorder ability and power
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "dark elf"                 --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "dark elf"                 --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "dark elf"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             'resist fire_75'})),            \
  \$R->append(RACE::NPCS->new({'spell',             'ancestor guardian'})),         \
  \$R->append(RACE::DESC->new({'description', 'In the Empire, \"Dark Elves\" is the common usage, but in their Morrowind homeland, they call themselves the \"Dunmer\". The dark-skinned, red-eyed Dark Elves combine powerful intellect with strong and agile physiques, producing superior warriors and sorcerers. On the battlefield, Dark Elves are noted for their skilled and balanced integration of swordsmen, marksmen, and war wizards. In character, they are grim, distrusting, and disdainful of other races.'}))"

# 2.6. Imperial # add resist shock 75% from BTB
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "imperial"                 --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "imperial"                 --sub-match "Description:"
# description is from "Balanced Passive Races and Birthsigns" by Necro
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "imperial"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             '_racialImperial_00'})),        \
  \$R->append(RACE::NPCS->new({'spell',             'resist shock_75'})),           \
  \$R->append(RACE::NPCS->new({'spell',             'star of the west'})),          \
  \$R->append(RACE::NPCS->new({'spell',             'voice of the emperor'})),      \
  \$R->append(RACE::DESC->new({'description', 'The well-educated and well-spoken natives of Cyrodiil are known for the discipline and training of their citizen armies. Though physically less imposing than the other races, Imperials are shrewd diplomats and traders, and these traits, along with their remarkable skill and training as infantry forces, have enabled them to subdue all the other nations and races, and to have erected the monument to peace and prosperity that comprises the Glorious Empire.'}))"

# 2.7. Khajiit # add resist frost 50% from BTB, though of adding hand-to-hand boost "Claws", but stopped when thought of gauntlets - wearing them should not grant boost to claws...
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_warm_fur'),               \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Warm Fur'),                    \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '91'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '50'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '50')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "khajiit"                  --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "khajiit"                  --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "khajiit"                  --run "
  \$R->append(RACE::NPCS->new({'spell',             '_racialKhajiit_00'})),         \
  \$R->append(RACE::NPCS->new({'spell',             '_felineGrace'})),              \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_warm_fur'})),             \
  \$R->append(RACE::NPCS->new({'spell',             'eye of fear'})),               \
  \$R->append(RACE::DESC->new({'description', 'The Khajiit of Elsweyr can vary in appearance from nearly Elven to the cathay-raht \"jaguar men\" to the great Senche-Tiger. The most common breed found in Morrowind, the suthay-raht, is intelligent, quick, and agile. Khajiit of all breeds have a weakness for sweets, especially the drug known as skooma. Many Khajiit disdain weapons in favor of their natural claws. They make excellent thieves due to their natural agility and unmatched acrobatics ability.'}))"
# got rid of this idea - it completely kills acrobatics for khajiits IMO
# this is also from "Balanced Passive Races and Birthsigns" by Necro
# $tes3cmd modify "${result}" --hide-backups --exact-id "_felineGrace" --run "\$R->set({t=>'ENAM', f=>'Spell_Effect'}, '9'), \$R->set({t=>'ENAM', f=>'Mag_Min'}, '8'), \$R->set({t=>'ENAM', f=>'Mag_Max'}, '8')"
# $tes3cmd modify "${result}" --hide-backups --exact-id "_felineGrace" --run "\$R->append(SPEL::ENAM->new({'spell_effect', '11', 'range', '0', 'area', '0', 'duration', '0', 'mag_min', '10', 'mag_max', '10'}))"

# 2.8. Nord # add resist shock 50% from BTB and thunder fist, fortify attack 10
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_fierceness'),             \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Fierceness'),                  \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '117'),                         \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '10'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '10')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "nord"                     --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "nord"                     --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "nord"                     --run "
  \$R->append(RACE::NPCS->new({'spell',             '_avp_fierceness'})),           \
  \$R->append(RACE::NPCS->new({'spell',             'immune to frost'})),           \
  \$R->append(RACE::NPCS->new({'spell',             'resist shock_50'})),           \
  \$R->append(RACE::NPCS->new({'spell',             'thunder fist'})),              \
  \$R->append(RACE::NPCS->new({'spell',             'woad'})),                      \
  \$R->append(RACE::DESC->new({'description', 'The citizens of Skyrim are aggressive and fearless in war, industrious and enterprising in trade and exploration. Strong, stubborn, and hardy, Nords are famous for their resistance to cold, even magical frost. Violence is an accepted and comfortable aspect of Nord culture; Nords of all classes are skilled with a variety of weapon and armor styles, and they cheerfully face battle with an ecstatic ferocity that shocks and appalls their enemies.'}))"

# 2.9. Orc # add resist weapons from BTB, add resist fire and shock as in Necro's Balanced passive
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_resist_fire_50'),         \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Fire'),                 \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '90'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '50'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '50')"
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_resist_shock_25'),        \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Shock'),                \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '92'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "orc"                      --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "orc"                      --sub-match "Description:"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "orc"                      --run "
  \$R->append(RACE::NPCS->new({'spell',             '_avp_resist_fire_50'})),       \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_resist_shock_25'})),      \
  \$R->append(RACE::NPCS->new({'spell',             'resist magicka_25'})),         \
  \$R->append(RACE::NPCS->new({'spell',             'orc weapon resistance'})),     \
  \$R->append(RACE::NPCS->new({'spell',             'orc_beserk'})),                \
  \$R->append(RACE::DESC->new({'description', 'These sophisticated barbarian beast peoples of the Wrothgarian and Dragontail Mountains are noted for their unshakeable courage in war and their unflinching endurance of hardships. Orc warriors in heavy armor are among the finest front-line troops in the Empire. Most Imperial citizens regard Orc society as rough and cruel, but there is much to admire in their fierce tribal loyalties and generous equality of rank and respect among the sexes.'}))"

# 2.9. Redguard # add resist fire 25% from Necro, add fortify sanctuary 10
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_resist_fire_25'),         \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Fire'),                 \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '90'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             '_avp_resist_fire_25'}))"
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_nimbleness'),             \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Nimbleness'),                  \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '42'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '10'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '10')"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "redguard"                 --sub-match "Spell:"
$tes3cmd delete "${result}" --hide-backups --type RACE --exact-id "redguard"                 --sub-match "Description:"
# description is from "Balanced Passive Races and Birthsigns" by Necro
$tes3cmd modify "${result}" --hide-backups --type RACE --exact-id "redguard"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             '_avp_nimbleness'})),           \
  \$R->append(RACE::NPCS->new({'spell',             'resist poison_75'})),          \
  \$R->append(RACE::NPCS->new({'spell',             'resist disease_75'})),         \
  \$R->append(RACE::NPCS->new({'spell',             '_avp_resist_fire_25'})),       \
  \$R->append(RACE::NPCS->new({'spell',             'adrenaline rush'})),           \
  \$R->append(RACE::DESC->new({'description', 'The most naturally talented warriors in Tamriel, the dark-skinned, wiry-haired Redguards of Hammerfell seem born to battle, though their pride and fierce independence of spirit makes them more suitable as scouts or skirmishers, or as free-ranging heroes and adventurers, than as rank-and-file soldiers. In addition to their cultural affinity for the combat arts, Redguards are also physically blessed with hardy constitutions and quickness of foot.'}))"

# 3. Birthsigns

# 3.1. Warrior # unchanged from Sensible Birthsigns, same as in BTB but 25 instead of 20.

# 3.2. Mage # change willpower to intelligence as in btb, set magicka bonus to 0.5 
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "fay ability"              --run "
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Min'},       '5'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Max'},       '5'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Min'},       '25'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Max'},       '25')"

# 3.3. Thief # unchanged from Sensible Birthsigns

# 3.4. Serpent # now a buffed version of Sensible Birthsigns, but not that much to be overpowered IMO, though i had a desire to make it 25.
$tes3cmd dump   "${source_morrowind}" --raw "${result}" --type SPEL --exact-id "weakness magicka_50"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "weakness magicka_50"      --run "
  \$R->set({t=>'NAME', f=>'ID'},                    '_avp_serpent_dance'),          \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Serpent Dance'),               \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '42'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '20'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '20')"
$tes3cmd delete "${result}" --hide-backups --type BSGN --exact-id "star-cursed"              --sub-match "Spell:_cobraDance"
$tes3cmd modify "${result}" --hide-backups --type BSGN --exact-id "star-cursed"              --run "
  \$R->append(RACE::NPCS->new({'spell',             '_avp_serpent_dance'}))"

# 3.5. Lady # increase bonuses to 25, change lady's touch from calm 100 to charm 10
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "lady's grace"             --run "
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Min'},       '25'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Max'},       '25'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Duration'},      '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Min'},       '25'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Max'},       '25')"
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "lady's touch"             --run "
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '44'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '10'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '10')"

# 3.6. Steed # unchanged from Sensible Birthsigns, same as in BTB but 25 instead of 20. Steed is almost as powerful as Ritual IMO

# 3.7. Lord # increase stat increase to 25, increase health regen to 5, add trollkin ability from Sensible Races
# due to struggle with append SPEL::ENAM i'd make it from "template" star of the west
$tes3cmd dump   "${source_btb_gi_ne}" --raw "${result}" --exact-id "star of the west"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "star of the west"         --run "
  \$R->set({i=>[3], t=>'ENAM', f=>'Spell_Effect'},  '79'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Attribute'},     '5'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Duration'},      '0'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Min'},       '25'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Max'},       '25'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Spell_Effect'},  '75'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Duration'},      '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Min'},       '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Max'},       '0'),                           \
  \$R->delete({i=>[5]}),                                                            \
  \$R->set({t=>'SPDT', f=>'Type'},                  '1'),                           \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Trollkin'),                    \
  \$R->set({t=>'NAME', f=>'ID'},                    '_trollkin')"
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "blood of the north"       --run "
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Min'},       '5'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Max'},       '5')"
$tes3cmd delete "${result}" --hide-backups --type BSGN --exact-id "trollkin"                 --sub-match "Spell:trollkin ability"
$tes3cmd modify "${result}" --hide-backups --type BSGN --exact-id "trollkin"                 --run "
  \$R->append(RACE::NPCS->new({'spell',             '_trollkin'}))"

# 3.8. Apprentice # change weakness to 25%
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "elfborn ability"          --run "
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Min'},       '25'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Max'},       '25')"

# 3.9. Atronach # BTBGI's version of wombburn ability

# 3.10. Ritual # actually it's now the most powerful sign IMO, better than atronach
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "_maraProtect"             --run "
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '93'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"

# 3.11. Lover # add resist paralysis as in Necro, rest is from BTB
$tes3cmd dump   "${source_btb_gi_ne}" --raw "${result}" --exact-id "star of the west"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "star of the west"         --run "
  \$R->set({i=>[3], t=>'ENAM', f=>'Spell_Effect'},  '79'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Attribute'},     '3'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Duration'},      '0'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Min'},       '25'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Max'},       '25'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Spell_Effect'},  '99'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Duration'},      '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Min'},       '50'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Max'},       '50'),                          \
  \$R->delete({i=>[5]}),                                                            \
  \$R->set({t=>'SPDT', f=>'Type'},                  '1'),                           \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Mooncalf'),                    \
  \$R->set({t=>'NAME', f=>'ID'},                    'mooncalf ability')"

# 3.12. Shadow # almost unchanged from Sensible Birthsigns, though increased chameleon power a bit from 15 to 20
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "_elusive"                 --run "
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '20'),                          \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '20')"

# 3.13. Tower # mix of btb and Sensible, beggar's nose is now an ability
$tes3cmd dump   "${source_btb_gi_ne}" --raw "${result}" --exact-id "star of the west"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SPEL --exact-id "star of the west"         --run "
  \$R->set({i=>[3], t=>'ENAM', f=>'Spell_Effect'},  '64'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[3], t=>'ENAM', f=>'Duration'},      '60'),                          \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Min'},       '200'),                         \
  \$R->set({i=>[3], t=>'ENAM', f=>'Mag_Max'},       '200'),                         \
  \$R->set({i=>[4], t=>'ENAM', f=>'Spell_Effect'},  '65'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[4], t=>'ENAM', f=>'Duration'},      '60'),                          \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Min'},       '200'),                         \
  \$R->set({i=>[4], t=>'ENAM', f=>'Mag_Max'},       '200'),                         \
  \$R->set({i=>[5], t=>'ENAM', f=>'Spell_Effect'},  '66'),                          \
  \$R->set({i=>[5], t=>'ENAM', f=>'Range'},         '0'),                           \
  \$R->set({i=>[5], t=>'ENAM', f=>'Duration'},      '60'),                          \
  \$R->set({i=>[5], t=>'ENAM', f=>'Mag_Min'},       '200'),                         \
  \$R->set({i=>[5], t=>'ENAM', f=>'Mag_Max'},       '200'),                         \
  \$R->set({t=>'SPDT', f=>'Type'},                  '1'),                           \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Beggar\'s Nose'),              \
  \$R->set({t=>'NAME', f=>'ID'},                    'beggar\'s nose spell')"

# after split
$tes3cmd dump "${source_btb_gi_ne}" --raw "${result}" --type SPEL \
 --exact-id "star of the west"

# 2022-08-10 - Add masters to allow easier editing
# 2023-05-23 - Add "No masters" private version for easier merging with Habasi
$tes3cmd header "${result}" --hide-backups --update-record-count
cp -v "${result}" "${result_with_masters}"
$tes3cmd modify "${result_with_masters}" --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', 'BTB\'s Game Improvements (Necro Edit).esp'})), \
  \$R->append(TES3::DATA->new({'length', '712451'})), \
  \$R->append(TES3::MAST->new({'master', 'SensibleBirthsigns.esp'})), \
  \$R->append(TES3::DATA->new({'length', '7711'})), \
  \$R->append(TES3::MAST->new({'master', 'SensibleRaces.esp'})), \
  \$R->append(TES3::DATA->new({'length', '10880'}))"
# 2022-09-18 - Disable "synchronize" to prevent setting updated filesizes for slightly modified Sensible masters
# $tes3cmd header "${result}" --hide-backups --synchronize
# $tes3cmd header "${result}" --hide-backups --update-record-count

# Create dir and move files there
mkdir -v "05 Sensible Races and Birthsigns - Custom edit"
mv -v\
  "${result_with_masters}"\
  "05 Sensible Races and Birthsigns - Custom edit"/
# 2023-05-23 - Add "No masters" private version for easier merging with Habasi
mkdir -v "95 Sensible Races and Birthsigns - Custom edit"
mv -v\
  "${result}"\
  "95 Sensible Races and Birthsigns - Custom edit"/
