#!/usr/bin/env bash
# Changelog:
# 2023-11-04
#   - Add new reactions from Tamriel_Data v10
#   - Increase reputation requirements according to TR_Factions v23.10
#
# 2022-12-04
#   - Add new reactions from TR_Factions v22.11
#   - Increase reputation requirements according to TR_Factions v22.11
#   - Do not try to override values for factions not present in BTBGI
# 
# 2022-09-03
#   - Fix to 7th preferred skill removed previously from BTBGI for Imperial Legion, Morag Tong, Temple
#     - Found a bug of tes3cmd not showing 7th+ preferred skill correctly thank's to Walkihr
#   - Add More Exclusive Factions corresponding patches
#
# 2022-02-07
#   - Change(slightly lower) Census and Excise, East Empire Company template to IC's/IL's instead of Guild's
# 
# 2021-07-27
#   - Change values for quarra clan (from quarra clan mod)
# 
# 2021-07-09
#   - Change values for some playable (and playable with mods) factions
#     - census and excise, camonna tong, east empire company
# 
# 2021-07-08
#   - Fix rep_5 value for Census and Excise in case you use mod that make it playable
#   - Add some non-BTB factions to overcome changes by some mods
#     - The Lamp
#        - breaks Redoran and Camonna Tongs reactions
#     - Imperial Factions - targeted at another balance
#
# 2021-01-16
#   - Increase reputation requirements according to TR_Factions
#
# vim: nowrap
# command used for formatting text:
#   '<,'>EasyAlign 5/'/r0                            # mass attibute/skill change
#   '<,'>EasyAlign 4/\-\-/r0 ; '<,'>EasyAlign / '/r0 # the rest
result="avp BTBGI-NE - Factions.esp"
source_btb_gi_ne="BTB's Game Improvements (Necro Edit).esp"
# source_morrowind="Morrowind.esm"
# source_bloodmoon="Bloodmoon.esm"
source_bfactions="${result}"
mef=" - More Exclusive Factions.esp"
tes3cmd="./tes3cmd"

# 01 Generic Factions patch to override other mods

# Hlaalu and Redoran not strictly required for TR_Factions patch for now, but preparing for future here
$tes3cmd dump "${source_btb_gi_ne}" --raw-with-header "${result}" --type FACT \
  --exact-id "fighters guild" \
  --exact-id "hlaalu" \
  --exact-id "imperial cult" \
  --exact-id "imperial legion" \
  --exact-id "mages guild" \
  --exact-id "morag tong" \
  --exact-id "redoran" \
  --exact-id "telvanni" \
  --exact-id "temple" \
  --exact-id "thieves guild"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial cult" --run "\
  \$R->delete({i=>[35,36,41,42]})"
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "temple" --run "\
  \$R->delete({i=>[31,32]})"

# 01MEF
cp -v "${result}" "${result%.*}${mef}"
$tes3cmd modify "${result%.*}${mef}" --hide-backups --type FACT --exact-id "telvanni"   --run "\
  \$R->set({t=>'FADT', f=>'sk_1'},      '9')"

# 2023-11-04 - Add new reactions from Tamriel_Data v10
# 02 Tamriel_Data
result="avp BTBGI-NE - Factions - Tamriel_Data.esp"
source_tdfaction="${result}"
$tes3cmd dump "${source_bfactions}" --raw-with-header "${result}" --type FACT \
  --exact-id "fighters guild" \
  --exact-id "hlaalu" \
  --exact-id "imperial cult" \
  --exact-id "imperial legion" \
  --exact-id "mages guild" \
  --exact-id "morag tong" \
  --exact-id "redoran" \
  --exact-id "telvanni" \
  --exact-id "temple" \
  --exact-id "thieves guild"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "fighters guild" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_FightersGuild'})), \
  \$R->append(FACT::INTV->new({'reaction',  '3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Glb_ArchaeologicalSociety'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_ImperialNavy'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "hlaalu" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial cult" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_ImperialCult'})), \
  \$R->append(FACT::INTV->new({'reaction',  '3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial legion" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_ImperialNavy'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ImperialNavy'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "mages guild" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_ImperialNavy'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "redoran" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Glb_ArchaeologicalSociety'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "telvanni" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_ImperialNavy'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "temple" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'}))"

$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "thieves guild" --run "\
  \$R->append(FACT::ANAM->new({'faction',   'T_Cyr_AbeceanTradingCompany'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Glb_ArchaeologicalSociety'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  \$R->append(FACT::ANAM->new({'faction',   'T_Sky_ClanKhulari'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-1'}))"

# 02MEF
cp -v "${result}" "${result%.*}${mef}"
$tes3cmd modify "${result%.*}${mef}" --hide-backups --type FACT --exact-id "telvanni"   --run "\
  \$R->set({t=>'FADT', f=>'sk_1'},      '9')"

# 2023-11-04 - Increase reputation requirements according to TR_Factions v23.10
# 03 TR_Factions
result="avp BTBGI-NE - Factions - TR_Factions.esp"
source_tr_factio="${result}"
$tes3cmd dump "${source_tdfaction}" --raw-with-header "${result}" --type FACT

# 2021-01-16 - Increase reputation requirements according to TR_Factions
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "fighters guild"      --run "\
  \$R->set({t=>'FADT', f=>'Rep_2'},     '5'),  \
  \$R->set({t=>'FADT', f=>'Rep_3'},     '20'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '40'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '70'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '100'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '130'), \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '160'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '190'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '235')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Glb_ArchaeologicalSociety'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '1'}))"
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "hlaalu"              --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '20'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '35'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '50'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '70'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '95'),  \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '120'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '145'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '175')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'}))"
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial cult"       --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '15'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '30'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '45'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '65'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '90'),  \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '120'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '150'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '185')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'}))"
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial legion"     --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '15'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '30'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '45'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '70'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '95'), \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '110'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '125'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '155')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'}))"
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "mages guild"         --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '20'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '40'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '70'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '100'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '130'), \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '160'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '190'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '235')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'}))"
# 2022-12-04 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "telvanni"            --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '15'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '25'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '35'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '55'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '75'),  \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '100'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '125'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '150')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'}))"
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "temple"              --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '15'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '35'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '55'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '80'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '105'),  \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '135'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '165'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '200')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '2'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-3'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-3'}))"
# 2022-12-04 - Increase reputation requirements according to TR_Factions v22.11 - Add new reactions from TR_Factions v22.11
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "thieves guild"       --run "\
  \$R->set({t=>'FADT', f=>'Rep_3'},     '20'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '40'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '70'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '100'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '130'),  \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '160'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '190'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '235')"
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
  # \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
  # \$R->append(FACT::INTV->new({'reaction',  '-1'}))"
# # 2022-12-04 - Add new reactions from TR_Factions v22.11
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "redoran"             --run "\
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '1'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '1'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '-2'}))"
# 2023-11-04 - Increase reputation requirements according to TR_Factions v23.10
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "morag tong"          --run "\
  \$R->set({t=>'FADT', f=>'Rep_2'},     '15'),  \
  \$R->set({t=>'FADT', f=>'Rep_3'},     '30'),  \
  \$R->set({t=>'FADT', f=>'Rep_4'},     '45'),  \
  \$R->set({t=>'FADT', f=>'Rep_5'},     '60'),  \
  \$R->set({t=>'FADT', f=>'Rep_6'},     '75'),  \
  \$R->set({t=>'FADT', f=>'Rep_7'},     '90'),  \
  \$R->set({t=>'FADT', f=>'Rep_8'},     '110'), \
  \$R->set({t=>'FADT', f=>'Rep_9'},     '130'), \
  \$R->set({t=>'FADT', f=>'Rep_10'},    '155')"

# 03MEF
cp -v "${result}" "${result%.*}${mef}"
$tes3cmd modify "${result%.*}${mef}" --hide-backups --type FACT --exact-id "telvanni"   --run "\
  \$R->set({t=>'FADT', f=>'sk_1'},      '9')"

# 04 Vvardenfell Brotherhood
result="avp BTBGI-NE - Factions - Vvardenfell Brotherhood.esp"
$tes3cmd dump "${source_bfactions}" --raw-with-header "${result}" --type FACT

# Vvardenfell Brotherhood new reactions
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "morag tong"          --run "\
  \$R->append(FACT::ANAM->new({'faction',   'Vvardenfell Brotherhood'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'}))"

# 04MEF
cp -v "${result}" "${result%.*}${mef}"
$tes3cmd modify "${result%.*}${mef}" --hide-backups --type FACT --exact-id "telvanni"   --run "\
  \$R->set({t=>'FADT', f=>'sk_1'},      '9')"

# 2023-11-04 - Increase reputation requirements according to TR_Factions v23.10
# 05 Vvardenfell Brotherhood - TR_Factions
result="avp BTBGI-NE - Factions - Vvardenfell Brotherhood - Tamriel_Data.esp"
$tes3cmd dump "${source_tdfaction}" --raw-with-header "${result}" --type FACT

# Vvardenfell Brotherhood new reactions
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "morag tong"          --run "\
  \$R->append(FACT::ANAM->new({'faction',   'Vvardenfell Brotherhood'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'}))"

# 05MEF
cp -v "${result}" "${result%.*}${mef}"
$tes3cmd modify "${result%.*}${mef}" --hide-backups --type FACT --exact-id "telvanni"   --run "\
  \$R->set({t=>'FADT', f=>'sk_1'},      '9')"

# 2023-11-04 - Increase reputation requirements according to TR_Factions v23.10
# 06 Vvardenfell Brotherhood - TR_Factions
result="avp BTBGI-NE - Factions - Vvardenfell Brotherhood - TR_Factions.esp"
$tes3cmd dump "${source_tr_factio}" --raw-with-header "${result}" --type FACT

# Vvardenfell Brotherhood new reactions
$tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "morag tong"          --run "\
  \$R->append(FACT::ANAM->new({'faction',   'Vvardenfell Brotherhood'})), \
  \$R->append(FACT::INTV->new({'reaction',  '-3'}))"

# 06MEF
cp -v "${result}" "${result%.*}${mef}"
$tes3cmd modify "${result%.*}${mef}" --hide-backups --type FACT --exact-id "telvanni"   --run "\
  \$R->set({t=>'FADT', f=>'sk_1'},      '9')"

# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# Change: 2021-07-08 - Add some non-BTB factions to overcome changes by some mods
# $tes3cmd dump "${source_morrowind}" --raw "${result}" --type FACT \
#   --exact-id "census and excise" \
#   --exact-id "imperial knights" \
#   --exact-id "camonna tong" \
#   --exact-id "clan aundae" \
#   --exact-id "clan berne" \
#   --exact-id "clan quarra"

# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# # Change: 2021-07-08 - Add some non-BTB factions to overcome changes by some mods
# $tes3cmd dump "${source_bloodmoon}" --raw "${result}" --type FACT \
#   --exact-id "east empire company"

# 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, east empire company
# # Change: 2022-02-07 - Change(slightly lower) Census and Excise, East Empire Company template to IC's/IL's instead of Guild's
# for faction in "census and excise" "east empire company"; do
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "${faction}" --run "\
#   \$R->set({t=>'FADT', f=>'At1_1'},  '30'), \
#   \$R->set({t=>'FADT', f=>'At1_2'},  '35'), \
#   \$R->set({t=>'FADT', f=>'At1_3'},  '40'), \
#   \$R->set({t=>'FADT', f=>'At1_4'},  '45'), \
#   \$R->set({t=>'FADT', f=>'At1_5'},  '50'), \
#   \$R->set({t=>'FADT', f=>'At1_6'},  '55'), \
#   \$R->set({t=>'FADT', f=>'At1_7'},  '60'), \
#   \$R->set({t=>'FADT', f=>'At1_8'},  '70'), \
#   \$R->set({t=>'FADT', f=>'At1_9'},  '80'), \
#   \$R->set({t=>'FADT', f=>'At1_10'}, '90'), \
#   \$R->set({t=>'FADT', f=>'At2_1'},  '20'), \
#   \$R->set({t=>'FADT', f=>'At2_2'},  '20'), \
#   \$R->set({t=>'FADT', f=>'At2_3'},  '25'), \
#   \$R->set({t=>'FADT', f=>'At2_4'},  '30'), \
#   \$R->set({t=>'FADT', f=>'At2_5'},  '35'), \
#   \$R->set({t=>'FADT', f=>'At2_6'},  '40'), \
#   \$R->set({t=>'FADT', f=>'At2_7'},  '45'), \
#   \$R->set({t=>'FADT', f=>'At2_8'},  '50'), \
#   \$R->set({t=>'FADT', f=>'At2_9'},  '55'), \
#   \$R->set({t=>'FADT', f=>'At2_10'}, '60'), \
#   \$R->set({t=>'FADT', f=>'Sk1_1'},  '15'), \
#   \$R->set({t=>'FADT', f=>'Sk1_2'},  '20'), \
#   \$R->set({t=>'FADT', f=>'Sk1_3'},  '25'), \
#   \$R->set({t=>'FADT', f=>'Sk1_4'},  '30'), \
#   \$R->set({t=>'FADT', f=>'Sk1_5'},  '35'), \
#   \$R->set({t=>'FADT', f=>'Sk1_6'},  '40'), \
#   \$R->set({t=>'FADT', f=>'Sk1_7'},  '50'), \
#   \$R->set({t=>'FADT', f=>'Sk1_8'},  '60'), \
#   \$R->set({t=>'FADT', f=>'Sk1_9'},  '70'), \
#   \$R->set({t=>'FADT', f=>'Sk1_10'}, '80'), \
#   \$R->set({t=>'FADT', f=>'Sk2_1'},  '05'), \
#   \$R->set({t=>'FADT', f=>'Sk2_2'},  '10'), \
#   \$R->set({t=>'FADT', f=>'Sk2_3'},  '15'), \
#   \$R->set({t=>'FADT', f=>'Sk2_4'},  '20'), \
#   \$R->set({t=>'FADT', f=>'Sk2_5'},  '25'), \
#   \$R->set({t=>'FADT', f=>'Sk2_6'},  '30'), \
#   \$R->set({t=>'FADT', f=>'Sk2_7'},  '35'), \
#   \$R->set({t=>'FADT', f=>'Sk2_8'},  '40'), \
#   \$R->set({t=>'FADT', f=>'Sk2_9'},  '45'), \
#   \$R->set({t=>'FADT', f=>'Sk2_10'}, '50')"
# done

# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "camonna tong" --run "\
#   \$R->set({t=>'FADT', f=>'At1_1'},  '30'), \
#   \$R->set({t=>'FADT', f=>'At1_2'},  '35'), \
#   \$R->set({t=>'FADT', f=>'At1_3'},  '40'), \
#   \$R->set({t=>'FADT', f=>'At1_4'},  '45'), \
#   \$R->set({t=>'FADT', f=>'At1_5'},  '50'), \
#   \$R->set({t=>'FADT', f=>'At1_6'},  '55'), \
#   \$R->set({t=>'FADT', f=>'At1_7'},  '60'), \
#   \$R->set({t=>'FADT', f=>'At1_8'},  '70'), \
#   \$R->set({t=>'FADT', f=>'At1_9'},  '80'), \
#   \$R->set({t=>'FADT', f=>'At1_10'}, '90'), \
#   \$R->set({t=>'FADT', f=>'At2_1'},  '25'), \
#   \$R->set({t=>'FADT', f=>'At2_2'},  '30'), \
#   \$R->set({t=>'FADT', f=>'At2_3'},  '35'), \
#   \$R->set({t=>'FADT', f=>'At2_4'},  '40'), \
#   \$R->set({t=>'FADT', f=>'At2_5'},  '45'), \
#   \$R->set({t=>'FADT', f=>'At2_6'},  '50'), \
#   \$R->set({t=>'FADT', f=>'At2_7'},  '55'), \
#   \$R->set({t=>'FADT', f=>'At2_8'},  '60'), \
#   \$R->set({t=>'FADT', f=>'At2_9'},  '65'), \
#   \$R->set({t=>'FADT', f=>'At2_10'}, '70'), \
#   \$R->set({t=>'FADT', f=>'Sk1_1'},  '15'), \
#   \$R->set({t=>'FADT', f=>'Sk1_2'},  '20'), \
#   \$R->set({t=>'FADT', f=>'Sk1_3'},  '25'), \
#   \$R->set({t=>'FADT', f=>'Sk1_4'},  '30'), \
#   \$R->set({t=>'FADT', f=>'Sk1_5'},  '35'), \
#   \$R->set({t=>'FADT', f=>'Sk1_6'},  '40'), \
#   \$R->set({t=>'FADT', f=>'Sk1_7'},  '50'), \
#   \$R->set({t=>'FADT', f=>'Sk1_8'},  '60'), \
#   \$R->set({t=>'FADT', f=>'Sk1_9'},  '70'), \
#   \$R->set({t=>'FADT', f=>'Sk1_10'}, '80'), \
#   \$R->set({t=>'FADT', f=>'Sk2_1'},  '10'), \
#   \$R->set({t=>'FADT', f=>'Sk2_2'},  '15'), \
#   \$R->set({t=>'FADT', f=>'Sk2_3'},  '20'), \
#   \$R->set({t=>'FADT', f=>'Sk2_4'},  '25'), \
#   \$R->set({t=>'FADT', f=>'Sk2_5'},  '30'), \
#   \$R->set({t=>'FADT', f=>'Sk2_6'},  '35'), \
#   \$R->set({t=>'FADT', f=>'Sk2_7'},  '40'), \
#   \$R->set({t=>'FADT', f=>'Sk2_8'},  '45'), \
#   \$R->set({t=>'FADT', f=>'Sk2_9'},  '50'), \
#   \$R->set({t=>'FADT', f=>'Sk2_10'}, '60')"

# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# # Change: 2021-07-08 - Fix rep_5 value for Census and Excise in case you use mod that make it playable
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "census and excise" --run "\
#   \$R->set({t=>'FADT', f=>'Rep_5'},         '30')"

# 2022-09-03 - Fix to 7th preferred skill removed previously from BTBGI for Imperial Legion, Morag Tong, Temple. Found a bug of tes3cmd not showing 7th+ preferred skill correctly thank's to Walkihr
# It appeared that those "-1" records were actually 7th skills not shown correctly. Commenting this out
# # change default faction reaction for imperial legion, temple and morag tong
# # no mention anywhere why they were changed; there are no such changes in WGI, Service Requirements as well
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "imperial legion"     --run "\$R->set({t=>'FADT', f=>'Unknown'},       '-1')"
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "morag tong"          --run "\$R->set({t=>'FADT', f=>'Unknown'},       '-1')"
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "temple"              --run "\$R->set({t=>'FADT', f=>'Unknown'},       '-1')"

# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# # 2021-07-27 - Change values for quarra clan (from quarra clan mod)
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "clan quarra" --run "\
#   \$R->set({t=>'FADT', f=>'At1_9'},  '80'),  \
#   \$R->set({t=>'FADT', f=>'At1_10'}, '90'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_2'},  '20'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_3'},  '30'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_4'},  '40'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_5'},  '50'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_6'},  '60'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_7'},  '70'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_8'},  '80'),  \
#   \$R->set({t=>'FADT', f=>'Sk1_9'},  '100'), \
#   \$R->set({t=>'FADT', f=>'Sk1_10'}, '100'), \
#   \$R->set({t=>'FADT', f=>'Sk2_2'},  '15'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_3'},  '25'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_4'},  '30'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_5'},  '35'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_6'},  '40'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_7'},  '50'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_8'},  '60'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_9'},  '80'),  \
#   \$R->set({t=>'FADT', f=>'Sk2_10'}, '100'), \
#   \$R->set({t=>'FADT', f=>'Rep_9'},  '110'), \
#   \$R->set({t=>'FADT', f=>'Rep_10'}, '150'), \
#   \$R->set({t=>'FADT', f=>'sk_4'},  '8'),    \
#   \$R->set({t=>'FADT', f=>'sk_5'},  '5'),    \
#   \$R->set({t=>'FADT', f=>'sk_6'},  '3')"

# 2022-12-04 - Do not try to override values for factions not present in BTBGI
# 2022-12-04 - Add new reactions from TR_Factions v22.11
# $tes3cmd modify "${result}" --hide-backups --type FACT --exact-id "east empire company" --run "\
#   \$R->append(FACT::ANAM->new({'faction',   'T_Glb_ArchaeologicalSociety'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '1'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseIndoril'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '-1'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_HouseDres'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '1'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Orlukh'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '-2'})), \
#   \$R->append(FACT::ANAM->new({'faction',   'T_Mw_Clan_Baluath'})), \
#   \$R->append(FACT::INTV->new({'reaction',  '-2'}))"

