#!/usr/bin/env bash
# Changelog:
# 2021-01-16
#   - Decrease faction max primary skill requirements to 75 due to the way ncgd works
#     - Only 100s required, but this way is better
#     - Increase secondary skill requirements to balance these changes
#
# 2021-07-09
#   - Change values for some playable (and playable with mods) factions
#     - census and excise, camonna tong
#
# 2021-07-27
#   - Change values for quarra clan (from quarra clan mod)
#
# 2021-09-24
#   - Remove Better Sounds from here as it's not required. Moved into 03-Various
#   - As a result files and folders were renamed (remove BS part)
#
# 2022-02-07
#   - Change(slightly lower) Census and Excise, East Empire Company template to IC's/IL's instead of Guild's
# 
# 2022-07-24
#   - Add Alchemical Hustle(Toxicity)
#
# 2022-09-03
#   - Add More Exclusive Factions corresponding patches
#
# 2022-12-04
#   - Do not try to override values for factions not present in BTBGI
#
# vim: nowrap
# command used for formatting text: '<,'>EasyAlign 4/\-\-/r0 ; '<,'>EasyAlign / '/r0
source_morr="Morrowind.esm"
source_ncgd="ncgdMW.omwaddon"
source_mbsp="MBSP-BetterSounds+ncgdMW+ClassSpec.omwaddon"
prefix_fact="avp BTBGI-NE - Factions"
prefix="avp BTBGI-NE - NCGD - MBSP"
suffix_mef=" - More Exclusive Factions"
tes3cmd="./tes3cmd"

# i have tried to keep scripts KISS for easy reuse in windows command prompt etc, but here the loop make things much easier
for suffix in "" " - Vvardenfell Brotherhood" " - TR_Factions" " - Merged"; do
result="${prefix}${suffix}.esp"
source_fact="${prefix_fact}${suffix}.esp"
$tes3cmd dump "${source_morr}" --raw-with-header "${result}" --type GMST \
 --exact-id "fminorskillbonus"
$tes3cmd dump "${source_ncgd}" --raw "${result}" --type SKIL \
 --exact-id "1" \
 --exact-id "8" \
 --exact-id "9" \
 --exact-id "16" \
 --exact-id "18" \
 --exact-id "19" \
 --exact-id "20"
$tes3cmd dump "${source_mbsp}" --raw "${result}" --type MGEF \
 --exact-id "49" \
 --exact-id "50" \
 --exact-id "53" \
 --exact-id "54" \
 --exact-id "55" \
 --exact-id "56" \
 --exact-id "85" \
 --exact-id "86" \
 --exact-id "87" \
 --exact-id "88" \
 --exact-id "123" \
 --exact-id "127"
$tes3cmd dump "${source_fact}" --raw "${result}" --type FACT
$tes3cmd header "${result}" --hide-backups --update-record-count

# Modify ncgd records to bring them in line with BTBGI
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "1"                 --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '0.75')"
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "8"                 --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '0.01'), \
  \$R->set({t=>'SKDT', f=>'Use_Value_2'},  '0.05')"
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "9"                 --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '2.5'),  \
  \$R->set({t=>'SKDT', f=>'Use_Value_2'},  '1'),    \
  \$R->set({t=>'SKDT', f=>'Use_Value_4'},  '0.5')"
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "16"                --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '2.5'),  \
  \$R->set({t=>'SKDT', f=>'Use_Value_2'},  '0')"
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "18"                --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '25'),   \
  \$R->set({t=>'SKDT', f=>'Use_Value_2'},  '2.5')"
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "19"                --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '0.5'), \
  \$R->set({t=>'SKDT', f=>'Use_Value_2'},  '5')"
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "20"                --run "\
  \$R->set({t=>'SKDT', f=>'Use_Value_1'},  '0.1'), \
  \$R->set({t=>'SKDT', f=>'Use_Value_2'},  '5')"
# MBSP now
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "49"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '0.64')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "50"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '0.48')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "53"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '0.36')"
# this is to fix "Demoralize Human" spell, which trained Illusion, but depent on Mysticism. Now it's purely Illusion spell

# No longer required as I base on already fixed esp
# $tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "53"                --run "\$R->set({t=>'MEDT',f=>'School'},        '3')"

$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "54"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '0.24')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "55"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '0.04')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "56"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '0.02')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "85"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '4')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "86"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '12')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "87"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '12')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "88"                --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '6')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "123"               --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '15')"
$tes3cmd modify "${result}" --hide-backups --type MGEF --exact-id "127"               --run "\$R->set({t=>'MEDT',f=>'Base_Cost'},     '10')"

# Change: 2021-01-16 - Decrease faction max primary skill requirements to 75 due to the way ncgd works. Only 100s required, but this way is better. Increase secondary skill requirements to balance these changes
# FACT: redoran (Great House Redoran)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "redoran"           --run "\
  \$R->set({t=>'FADT', f=>'sk1_8'},        '75'), \
  \$R->set({t=>'FADT', f=>'sk2_8'},        '50'), \
  \$R->set({t=>'FADT', f=>'sk1_9'},        '75'), \
  \$R->set({t=>'FADT', f=>'sk2_9'},        '60'), \
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '70')"
# FACT: thieves guild (Thieves Guild)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "thieves guild"     --run "\
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '65')"
# FACT: temple (Temple)
# FACT: imperial cult (Imperial Cult)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial cult"     --run "\
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '55')"
# FACT: hlaalu (Great House Hlaalu)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "hlaalu"            --run "\
  \$R->set({t=>'FADT', f=>'sk1_8'},        '75'), \
  \$R->set({t=>'FADT', f=>'sk2_8'},        '50'), \
  \$R->set({t=>'FADT', f=>'sk1_9'},        '75'), \
  \$R->set({t=>'FADT', f=>'sk2_9'},        '60'), \
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '70')"
# FACT: telvanni (Great House Telvanni)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "telvanni"          --run "\
  \$R->set({t=>'FADT', f=>'sk1_8'},        '75'), \
  \$R->set({t=>'FADT', f=>'sk2_8'},        '50'), \
  \$R->set({t=>'FADT', f=>'sk1_9'},        '75'), \
  \$R->set({t=>'FADT', f=>'sk2_9'},        '60'), \
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '70')"
# FACT: fighters guild (Fighters Guild)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "fighters guild"    --run "\
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '65')"
# FACT: morag tong (Morag Tong)
# FACT: imperial legion (Imperial Legion)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial legion"   --run "\
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '55')"
# FACT: mages guild (Mages Guild)
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "mages guild"       --run "\
  \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
  \$R->set({t=>'FADT', f=>'sk2_10'},       '65')"
# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# # Change: 2021-07-09 - Change values for some playable (and playable with mods) factions: census and excise, camonna tong
# # Change: 2022-02-07 - Change(slightly lower) Census and Excise, East Empire Company template to IC's/IL's instead of Guild's
# # FACT: census and excise
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "census and excise" --run "\
#   \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
#   \$R->set({t=>'FADT', f=>'sk2_10'},       '55')"
# # FACT: camonna tong
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "camonna tong"      --run "\
#   \$R->set({t=>'FADT', f=>'sk1_10'},       '75'), \
#   \$R->set({t=>'FADT', f=>'sk2_10'},       '65')"
# # 2021-07-27 - Change values for quarra clan (from quarra clan mod)
# # FACT: quarra clan
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "clan quarra"       --run "\
#   \$R->set({t=>'FADT', f=>'sk1_8'},        '75'), \
#   \$R->set({t=>'FADT', f=>'sk2_8'},        '65')"
done

# 2022-07-24 - Add Alchemical Hustle(Toxicity)
$tes3cmd dump "BTB's Game Improvements (Necro Edit).esp" --raw-with-header "${prefix} - Alchemical Hustle(Toxicity).esp" --type SKIL --exact-id "16"
# New loop goes here just for AH and Merged to change Alchemy
for suffix in " - Alchemical Hustle(Toxicity)" " - Merged"; do
result="${prefix}${suffix}.esp"
$tes3cmd header "${result}" --hide-backups --update-record-count
$tes3cmd modify "${result}" --hide-backups --type SKIL --exact-id "16" --run "\
 \$R->set({t=>'DESC',f=>'Description'}, 'Alchemy identifies magical properties in mundane substances. Substances are consumed directly, or prepared as potions to provide long-lasting benefits like healing and curing disease, water-walking, magical shielding, and fortifying bodily attributes. Experts in this skill are also less susceptible to toxicity caused by heavy potion use.

Influences Intelligence, Personality, and Endurance.  All skills influence Luck a small amount.')"
done

# 2022-09-03 - Add More Exclusive Factions corresponding patches
# MEF
for suffix in "" " - Vvardenfell Brotherhood" " - TR_Factions" " - Merged"; do
  if [ "${suffix}" = " - Merged" ]; then
    result="${prefix}${suffix_mef}${suffix}.esp"
  else
    result="${prefix}${suffix}${suffix_mef}.esp"
  fi
  cp -v "${prefix}${suffix}.esp" "${result}"
  $tes3cmd header "${result}" --hide-backups --update-record-count
  $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "telvanni" --run "\$R->set({t=>'FADT', f=>'sk_1'}, '9')"
done
# and don't forget that MEF still needs Alchemical Hustle patch
cp -v "${prefix} - Alchemical Hustle(Toxicity).esp" "${prefix} - Alchemical Hustle(Toxicity)${suffix_mef}.esp"

# Create dirs and move files there
mkdir -v "06 NCGD - MBSP - Merged"
mv -v\
  "${prefix} - Merged.esp"\
  "06 NCGD - MBSP - Merged"/
mkdir -v "06 NCGD - MBSP${suffix_mef} - Merged"
mv -v\
  "${prefix}${suffix_mef} - Merged.esp"\
  "06 NCGD - MBSP${suffix_mef} - Merged"/
mkdir -v "06 NCGD - MBSP - Modular"
mv -v\
  "${prefix}.esp"\
  "${prefix} - Vvardenfell Brotherhood.esp"\
  "${prefix} - TR_Factions.esp"\
  "${prefix} - Alchemical Hustle(Toxicity).esp"\
  "06 NCGD - MBSP - Modular"/
mkdir -v "06 NCGD - MBSP${suffix_mef} - Modular"
mv -v\
  "${prefix}${suffix_mef}.esp"\
  "${prefix} - Vvardenfell Brotherhood${suffix_mef}.esp"\
  "${prefix} - TR_Factions${suffix_mef}.esp"\
  "${prefix} - Alchemical Hustle(Toxicity)${suffix_mef}.esp"\
  "06 NCGD - MBSP${suffix_mef} - Modular"/
mkdir -v "06 Factions - Merged"
mv -v\
  "${prefix_fact} - Merged.esp"\
  "06 Factions - Merged"/
mkdir -v "06 Factions${suffix_mef} - Merged"
mv -v\
  "${prefix_fact}${suffix_mef} - Merged.esp"\
  "06 Factions${suffix_mef} - Merged"/
mkdir -v "06 Factions - Modular"
mv -v\
  "${prefix_fact}.esp"\
  "${prefix_fact} - TR_Factions.esp"\
  "${prefix_fact} - Vvardenfell Brotherhood.esp"\
  "06 Factions - Modular"/
mkdir -v "06 Factions${suffix_mef} - Modular"
mv -v\
  "${prefix_fact}${suffix_mef}.esp"\
  "${prefix_fact} - TR_Factions${suffix_mef}.esp"\
  "${prefix_fact} - Vvardenfell Brotherhood${suffix_mef}.esp"\
  "06 Factions${suffix_mef} - Modular"/
