#!/usr/bin/env bash
# Changelog:
# 2021-09-18
#   - Backport Enchanted Weapon Resistance changes of 2.0.5-2.0.6
#
# 2022-02-18
#   - Add missing shockbite warhammer
#   - Get weapons with changed price from BTBGI from v2.0.14
#
# 2022-02-19
#   - Modify a bit more steel spears and axes
#
# 2022-04-12
#   - Synchronize dozen of weapons prices with BTBGI v2.0.15
#   - Add missing steel stormsword
#
# vim: nowrap
# command used for formatting text: '<,'>EasyAlign 4/\-\-/r0 ; '<,'>EasyAlign / '/r0

source_btb="BTB's Game Improvements (Necro Edit).esp"
source_pfp="Patch for Purists.esm"
source_bld="Bloodmoon.esm"
source_trb="Tribunal.esm"
source_mor="Morrowind.esm"
tes3cmd="./tes3cmd"

# 01 Fortify Magicka Enchantments
result="avp BTBGI-NE - No MWSE - FortifyMagickaEnchantments.esp"
$tes3cmd dump "${source_btb}" --raw-with-header "${result}" --type ENCH \
  --exact-id "keening_en" \
  --exact-id "mantle_woe_en"

$tes3cmd modify "${result}" --hide-backups --type ENCH --exact-id "keening_en"          --run " \
  \$R->set({i=>[2], t=>'ENAM', f=>'Spell_Effect'},  '84'), \
  \$R->set({i=>[2], t=>'ENAM', f=>'Range'},         '0'),  \
  \$R->set({i=>[2], t=>'ENAM', f=>'Duration'},      '1'),  \
  \$R->set({i=>[2], t=>'ENAM', f=>'Mag_Min'},       '10'), \
  \$R->set({i=>[2], t=>'ENAM', f=>'Mag_Max'},       '10')"
$tes3cmd modify "${result}" --hide-backups --type ENCH --exact-id "mantle_woe_en"       --run " \
  \$R->set({i=>[2], t=>'ENAM', f=>'Spell_Effect'},  '84'), \
  \$R->set({i=>[2], t=>'ENAM', f=>'Range'},         '0'),  \
  \$R->set({i=>[2], t=>'ENAM', f=>'Duration'},      '1'),  \
  \$R->set({i=>[2], t=>'ENAM', f=>'Mag_Min'},       '20'), \
  \$R->set({i=>[2], t=>'ENAM', f=>'Mag_Max'},       '20')"

# 02 Fortify Fatigue Potions
result="avp BTBGI-NE - No MWSE - FortifyFatiguePotions.esp"
$tes3cmd dump "${source_btb}" --raw-with-header "${result}" --type ALCH \
  --exact-id "p_fortify_fatigue_b" \
  --exact-id "p_fortify_fatigue_c" \
  --exact-id "p_fortify_fatigue_s" \
  --exact-id "p_fortify_fatigue_q" \
  --exact-id "p_fortify_fatigue_e"


$tes3cmd modify "${result}" --hide-backups --type ALCH --exact-id "p_fortify_fatigue_b" --run " \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Weapons, Bargain'), \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '98'), \
  \$R->set({t=>'ENAM', f=>'Duration'},              '10'), \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '10'), \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '10')"
$tes3cmd modify "${result}" --hide-backups --type ALCH --exact-id "p_fortify_fatigue_c" --run " \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Weapons, Cheap'), \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '98'), \
  \$R->set({t=>'ENAM', f=>'Duration'},              '15'), \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '15'), \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '15')"
$tes3cmd modify "${result}" --hide-backups --type ALCH --exact-id "p_fortify_fatigue_s" --run " \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Weapons, Normal'), \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '98'), \
  \$R->set({t=>'ENAM', f=>'Duration'},              '20'), \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '20'), \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '20')"
$tes3cmd modify "${result}" --hide-backups --type ALCH --exact-id "p_fortify_fatigue_q" --run " \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Weapons, Quality'), \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '98'), \
  \$R->set({t=>'ENAM', f=>'Duration'},              '25'), \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '25'), \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '25')"
$tes3cmd modify "${result}" --hide-backups --type ALCH --exact-id "p_fortify_fatigue_e" --run " \
  \$R->set({t=>'FNAM', f=>'Name'},                  'Resist Weapons, Special'), \
  \$R->set({t=>'ENAM', f=>'Spell_Effect'},          '98'), \
  \$R->set({t=>'ENAM', f=>'Duration'},              '30'), \
  \$R->set({t=>'ENAM', f=>'Mag_Min'},               '50'), \
  \$R->set({t=>'ENAM', f=>'Mag_Max'},               '50')"

# 03 Enchanted Weapon Resistance
result="avp BTBGI-NE - No MWSE - EnchantedWeaponResistance.esp"
$tes3cmd dump "${source_btb}" --raw-with-header "${result}" --type WEAP \
  --exact-id "bm_saber_seasplitter" \
  --exact-id "cruel flameblade"\
  --exact-id "cruel flamesword"\
  --exact-id "cruel shardblade"\
  --exact-id "cruel shardsword"\
  --exact-id "cruel sparkblade"\
  --exact-id "cruel sparksword"\
  --exact-id "cruel viperblade"\
  --exact-id "cruel vipersword"\
  --exact-id "dagoth dagger" \
  --exact-id "demon katana" \
  --exact-id "demon longbow" \
  --exact-id "demon tanto" \
  --exact-id "devil katana" \
  --exact-id "devil longbow" \
  --exact-id "devil spear" \
  --exact-id "devil tanto" \
  --exact-id "devil_tanto_tgamg" \
  --exact-id "dire flameblade"\
  --exact-id "dire flamesword"\
  --exact-id "dire frostarrow" \
  --exact-id "dire shardblade"\
  --exact-id "dire shardsword"\
  --exact-id "dire sparkblade"\
  --exact-id "dire sparksword"\
  --exact-id "dire viperblade"\
  --exact-id "dire vipersword"\
  --exact-id "fiend battle axe" \
  --exact-id "fiend katana" \
  --exact-id "fiend longbow" \
  --exact-id "fiend spear" \
  --exact-id "fiend tanto" \
  --exact-id "firebite war axe" \
  --exact-id "fireblade"\
  --exact-id "flamestar" \
  --exact-id "icebreaker" \
  --exact-id "imperial netch blade" \
  --exact-id "imperial shortsword severio" \
  --exact-id "iron flameblade"\
  --exact-id "iron flamecleaver" \
  --exact-id "iron flamemace"\
  --exact-id "iron flamemauler" \
  --exact-id "iron flameskewer"\
  --exact-id "iron flameslayer" \
  --exact-id "iron flamesword" \
  --exact-id "iron shardaxe" \
  --exact-id "iron shardblade"\
  --exact-id "iron shardcleaver" \
  --exact-id "iron shardmace"\
  --exact-id "iron shardmauler" \
  --exact-id "iron shardskewer"\
  --exact-id "iron shardslayer" \
  --exact-id "iron shardsword" \
  --exact-id "iron sparkaxe"\
  --exact-id "iron sparkblade"\
  --exact-id "iron sparkcleaver" \
  --exact-id "iron sparkmace"\
  --exact-id "iron sparkmauler" \
  --exact-id "iron sparkskewer"\
  --exact-id "iron sparkslayer" \
  --exact-id "iron sparksword" \
  --exact-id "iron spider dagger" \
  --exact-id "iron viperaxe"\
  --exact-id "iron viperblade"\
  --exact-id "iron vipercleaver" \
  --exact-id "iron vipermauler" \
  --exact-id "iron viperskewer"\
  --exact-id "iron viperslayer" \
  --exact-id "iron vipersword" \
  --exact-id "nordic claymore_stormfang" \
  --exact-id "shardstar" \
  --exact-id "shockbite battle axe"\
  --exact-id "shockbite halberd"\
  --exact-id "shockbite mace"\
  --exact-id "shockbite warhammer"\
  --exact-id "shortbow of sanguine sureflight"\
  --exact-id "spear_of_light"\
  --exact-id "spiderbite" \
  --exact-id "steel claymore of hewing"\
  --exact-id "steel firesword"\
  --exact-id "steel flameaxe"\
  --exact-id "steel flameblade"\
  --exact-id "steel flamecleaver"\
  --exact-id "steel flamemace"\
  --exact-id "steel flamemauler"\
  --exact-id "steel flamescythe"\
  --exact-id "steel flameskewer"\
  --exact-id "steel flameslayer"\
  --exact-id "steel flamesword"\
  --exact-id "steel frostsword"\
  --exact-id "steel jinkblade of the aegis" \
  --exact-id "steel jinkblade"\
  --exact-id "steel jinksword"\
  --exact-id "steel poisonsword"\
  --exact-id "steel shardaxe"\
  --exact-id "steel shardblade"\
  --exact-id "steel shardcleaver"\
  --exact-id "steel shardmace"\
  --exact-id "steel shardmauler"\
  --exact-id "steel shardscythe"\
  --exact-id "steel shardskewer"\
  --exact-id "steel shardslayer"\
  --exact-id "steel shardsword"\
  --exact-id "steel sparkaxe"\
  --exact-id "steel sparkblade"\
  --exact-id "steel sparkcleaver"\
  --exact-id "steel sparkmace"\
  --exact-id "steel sparkmauler"\
  --exact-id "steel sparkscythe"\
  --exact-id "steel sparkskewer"\
  --exact-id "steel sparkslayer"\
  --exact-id "steel sparksword"\
  --exact-id "steel spear of impaling thrust"\
  --exact-id "steel spider blade"\
  --exact-id "steel staff of chastening"\
  --exact-id "steel staff of divine judgement"\
  --exact-id "steel staff of peace"\
  --exact-id "steel staff of shaming"\
  --exact-id "steel staff of the ancestors" \
  --exact-id "steel staff of war"\
  --exact-id "steel stormsword" \
  --exact-id "steel viperaxe"\
  --exact-id "steel viperblade"\
  --exact-id "steel vipercleaver"\
  --exact-id "steel vipermace"\
  --exact-id "steel vipermauler"\
  --exact-id "steel viperscythe"\
  --exact-id "steel viperskewer"\
  --exact-id "steel viperslayer"\
  --exact-id "steel vipersword"\
  --exact-id "steel war axe of deep biting"\
  --exact-id "steel warhammer of smiting"\
  --exact-id "steelstaffancestors_ttsa" \
  --exact-id "stormblade" \
  --exact-id "stormkiss" \
  --exact-id "warhammer_rammekald_unique" \
  --exact-id "water spear" \
  --exact-id "we_temreki"

$tes3cmd dump "${source_pfp}" --raw "${result}" --type WEAP \
  --exact-id "bonebiter_bow_unique"\
  --exact-id "erur_dan_spear_unique"\
  --exact-id "shockbite war axe"

$tes3cmd dump "${source_bld}" --raw "${result}" --type WEAP \
  --exact-id "steel arrow_carnius"\
  --exact-id "steel spear snow prince"

$tes3cmd dump "${source_trb}" --raw "${result}" --type WEAP \
  --exact-id "dire viperarrow"

$tes3cmd dump "${source_mor}" --raw "${result}" --type WEAP \
  --exact-id "arrow of wasting flame"\
  --exact-id "arrow of wasting shard"\
  --exact-id "arrow of wasting spark"\
  --exact-id "arrow of wasting viper"\
  --exact-id "cloudcleaver_unique"\
  --exact-id "dagger of judgement"\
  --exact-id "dire flame bolt"\
  --exact-id "dire flamearrow"\
  --exact-id "dire shard bolt"\
  --exact-id "dire shardarrow"\
  --exact-id "dire spark bolt"\
  --exact-id "dire sparkarrow"\
  --exact-id "dire viper bolt"\
  --exact-id "firebite club"\
  --exact-id "firebite dagger"\
  --exact-id "firebite star"\
  --exact-id "firebite sword"\
  --exact-id "flame arrow"\
  --exact-id "flame_bolt"\
  --exact-id "flying viper"\
  --exact-id "grey shaft of holding"\
  --exact-id "grey shaft of nonsense"\
  --exact-id "grey shaft of unraveling"\
  --exact-id "iron_arrow_uniq_judgement"\
  --exact-id "shard arrow"\
  --exact-id "shard_bolt"\
  --exact-id "spark arrow"\
  --exact-id "spark_bolt"\
  --exact-id "sparkstar"\
  --exact-id "staff_of_llevule"\
  --exact-id "steel broadsword of hewing"\
  --exact-id "steel dagger of swiftblade"\
  --exact-id "viper arrow"\
  --exact-id "viper_bolt"\
  --exact-id "viperstar"\
  --exact-id "widowmaker_unique"\
  --exact-id "wooden staff of chastening"\
  --exact-id "wooden staff of divine"\
  --exact-id "wooden staff of peace"\
  --exact-id "wooden staff of shaming"\
  --exact-id "wooden staff of war"

$tes3cmd modify "${result}" --hide-backups --type WEAP --run "\$R->set({t=>'WPDT',f=>'Flags'}, '0')"
$tes3cmd modify "${result}" --hide-backups --type WEAP\
  --exact-id "arrow of wasting flame"\
  --exact-id "arrow of wasting shard"\
  --exact-id "arrow of wasting spark"\
  --exact-id "arrow of wasting viper"\
  --exact-id "dire flamearrow"\
  --exact-id "dire shardarrow"\
  --exact-id "dire sparkarrow"\
  --exact-id "flame arrow"\
  --exact-id "grey shaft of holding"\
  --exact-id "grey shaft of nonsense"\
  --exact-id "grey shaft of unraveling"\
  --exact-id "iron_arrow_uniq_judgement"\
  --exact-id "shortbow of sanguine sureflight"\
  --exact-id "spark arrow"\
  --exact-id "viper arrow"\
  --run "\
  \$R->set({t=>'WPDT',f=>'Slash_Max'}, '1'),\
  \$R->set({t=>'WPDT',f=>'Thrust_Max'}, '1')"

# fixed in 2.0.15
# change price of several items not yet in BTBGI
# $tes3cmd modify "${result}" --hide-backups --type WEAP\
#   --exact-id "steel flameskewer"\
#   --exact-id "steel shardskewer"\
#   --exact-id "steel sparkskewer"\
#   --exact-id "steel viperskewer"\
#   --exact-id "spear_of_light"\
#   --exact-id "steel spear of impaling thrust"\
#   --run "\
#   \$R->set({t=>'WPDT',f=>'Value'}, '255')"

# fixed in 2.0.15
# $tes3cmd modify "${result}" --hide-backups --type WEAP\
#   --exact-id "steel flameaxe"\
#   --exact-id "steel shardaxe"\
#   --exact-id "steel sparkaxe"\
#   --exact-id "steel viperaxe"\
#   --exact-id "steel war axe of deep biting"\
#   --run "\
#   \$R->set({t=>'WPDT',f=>'Value'}, '265')"

# fixed in 2.0.15
# $tes3cmd modify "${result}" --hide-backups --type WEAP\
#   --exact-id "steel broadsword of hewing"\
#   --run "\
#   \$R->set({t=>'WPDT',f=>'Value'}, '150')"

# this would require to dump this item first
#  --exact-id "steel spear"\
# probably not, prices are "steel dagger" 20, 33, 40 for now
#  --exact-id "steel dagger of swiftblade"\
#  --exact-id "steel jinkblade"\

# it's not needed
# $tes3cmd modify "${result}" --hide-backups --exact-id "iron flameslayer"             --run "\$R->set({t=>'ITEX',f=>'Icon'},          'w\\tx_claymore_iron.dds')"
# $tes3cmd modify "${result}" --hide-backups --exact-id "iron shardslayer"             --run "\$R->set({t=>'ITEX',f=>'Icon'},          'w\\tx_claymore_iron.dds')"
# $tes3cmd modify "${result}" --hide-backups --exact-id "iron sparkslayer"             --run "\$R->set({t=>'ITEX',f=>'Icon'},          'w\\tx_claymore_iron.dds')"
# $tes3cmd modify "${result}" --hide-backups --exact-id "iron viperslayer"             --run "\$R->set({t=>'ITEX',f=>'Icon'},          'w\\tx_claymore_iron.dds')"
