#!/usr/bin/env bash
# vim: nowrap
# command used for formatting text: '<,'>EasyAlign 3/\-\-/r0 ; '<,'>EasyAlign / '/r0
result="FM - Unique Items v1.0 - PfP.ESP"
source1="FM - Unique Items v1.0.ESP"
source2="Patch for Purists.esm"
tes3cmd="./tes3cmd"

# copy source to create replacement patch
cp -v "${source1}" "${result}"

# now modify records
$tes3cmd modify "${result}" --hide-backups --exact-id "key_forge of rolamus"      --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ancient Daedric Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "robe of st roris"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Robe of St. Roris')"
$tes3cmd modify "${result}" --hide-backups --exact-id "belt of feet of notorgo"   --run "\$R->set({t=>'FNAM',f=>'Name'},   'Belt of Feet of Notorgo')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nedhelas"              --run "\$R->set({t=>'FNAM',f=>'Name'},   'Nedhelas\' Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ienasa"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ienasa Radas\' Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_galmis"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dagoth Galmis\' Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_fals"                  --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dagoth Fals\' Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_tureynul"              --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dagoth Tureynul\'s Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_odros"                 --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dagoth Odros\' Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ahnassi"               --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ahnassi\'s Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "peakstar_belt_unique"      --run "\$R->set({t=>'FNAM',f=>'Name'},   'Embroidered Belt')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_marvani_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'An Old, Ashy Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_shushishislaves"       --run "\$R->set({t=>'FNAM',f=>'Name'},   'Slave Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_shaadnius"             --run "\$R->set({t=>'FNAM',f=>'Name'},   'Sha-Adnius Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ashurninibi"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ancient Daedric Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ashurninibi_lost"      --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ancient, Rusted Daedric Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ashalmawia_prisoncell" --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ancient Daedric Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ibardad"               --run "\$R->set({t=>'FNAM',f=>'Name'},   'Plain Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ibardad_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Ancient, Rusted Daedric Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_arenim"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Arenim Burial Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_senim_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'An Old Tomb Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_gimothran"             --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Gimothran Master Burial')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_elmussadamori"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Elmussa Damori\'s Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_indaren"               --run "\$R->set({t=>'FNAM',f=>'Name'},   'Rusty Key into Indaren Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_aralen"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Aralen Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sarys_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Sarys Chest Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_tharys_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Tharys Chest Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_thelas_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Thelas Chest Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_othrelas_door"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Othrelas Tomb Door')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_arano_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Arano Tomb Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_andrethi_chest"        --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Andrethi Tomb Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_heran"                 --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Heran Tomb Door')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_lleran_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Lleran Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_brinne_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Lord Brinne\'s Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_aran_tomb"             --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Aran Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sandas_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Sandas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sarano_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Sarano Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sarano_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Sarano Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_saren_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Saren Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_saren_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Saren Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_vandus_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Vandus Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_maren_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Maren Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_raviro_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Raviro Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_andalen_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Andalen Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_andalen_chest"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Andalen Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_arenim_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Arenim Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ravel_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Ravel Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ravel_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Ravel Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_savel_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Savel Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_indalen_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Indalen Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_norvayn_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Norvayn Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_norvayn_chest"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Norvayn Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_aryon_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Aryon Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_fadathram_tomb"        --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Fadathram Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_helas_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Helas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_thalas_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Thalas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_andas_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Andas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_andules_chest"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Andules Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_gimothran_chest"       --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Gimothran Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_gimothran_tomb"        --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Gimothran Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ienith_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Ienith Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ienith_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Ienith Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_thiralas_tomb"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Thiralas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_baram_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Baram Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_dreloth_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Dreloth Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_omaren_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Omaren Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sadryon_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Sadryon Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_verelnim_tomb"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Verelnim Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_falas_tomb"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Falas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_falas_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Falas Tomb Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_llervu"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Llervu Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_rethandus_tomb"        --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Rethandus Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_rethandus_chest"       --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Rethandus Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_rothan_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Rothan Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_dareleth_tomb"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Dareleth Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_salvel_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Salvel Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_salvel_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Salvel Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nerano_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Nerano Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_andavel_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Andavel Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_dralas_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Dralas Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_dralas_chest"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Dralas Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nelas_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Nelas Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_omalen_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Omalen Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_orethi_tomb"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Orethi Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sarethi_tomb"          --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Sarethi Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_favel_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Favel Tomb Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_senim_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Senim Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ashmelech"             --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Ashmelech')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_ashmelech_chest"       --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Asmelech Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nchuleftingth_chest"   --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Nchuleftingth Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_mzahnch_chest"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Mzahnch Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_aleft_chest"           --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Aleft Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_mzanchend_chest"       --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Mzanchend Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_arkngthunch_chest"     --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Arkngthunch Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_bthanchend_chest"      --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Bthanchend Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nchardahrk_chest"      --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Nchardahrk Chest')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_table_mudan00"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dwemer Key to Table in Mudan')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_j'zhirr"               --run "\$R->set({t=>'FNAM',f=>'Name'},   'J\'Zhirr\'s Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_armigers_stronghold"   --run "\$R->set({t=>'FNAM',f=>'Name'},   'Armigers Stronghold Dungeon Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_wormlord_tomb"         --run "\$R->set({t=>'FNAM',f=>'Name'},   'Key to Worm Lord\'s Tomb')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_mebastien"             --run "\$R->set({t=>'FNAM',f=>'Name'},   'Mebestien\'s Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_bolayn"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Bolayn\'s Chest Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_velas"                 --run "\$R->set({t=>'FNAM',f=>'Name'},   'A Worn Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_thendas"               --run "\$R->set({t=>'FNAM',f=>'Name'},   'Thendas Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_trib_dwe00"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dwemer Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_trib_dwe01"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dwemer Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_trib_dwe02"            --run "\$R->set({t=>'FNAM',f=>'Name'},   'Dwemer Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_durgok"                --run "\$R->set({t=>'FNAM',f=>'Name'},   'Durgok\'s Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nuncius"               --run "\$R->set({t=>'FNAM',f=>'Name'},   'Nuncius\' Key')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_nuncius2"              --run "\$R->set({t=>'FNAM',f=>'Name'},   'Nuncius\' Key')"

# import items with strange flags from PfP
$tes3cmd delete "${result}" --hide-backups \
  --exact-id "amulet of 6th house" \
  --exact-id "key_private quarters" \
  --exact-id "key_sandas"

$tes3cmd dump "${source2}" --raw "${result}" \
  --exact-id "amulet of 6th house" \
  --exact-id "key_private quarters" \
  --exact-id "key_sandas"

$tes3cmd modify "${result}" --hide-backups --exact-id "amulet of 6th house"       --run "\$R->set({t=>'MODL',f=>'Model'},  'Alais\\c\\amulet_6th.nif')"
$tes3cmd modify "${result}" --hide-backups --exact-id "amulet of 6th house"       --run "\$R->set({t=>'ITEX',f=>'Icon'},   'Alais\\c\\c_amulet_6th.dds')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_private quarters"      --run "\$R->set({t=>'MODL',f=>'Model'},  'Keys\\key_12.nif')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_private quarters"      --run "\$R->set({t=>'ITEX',f=>'Icon'},   'Keys\\Key_12.dds')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sandas"                --run "\$R->set({t=>'MODL',f=>'Model'},  'Keys\\key_25.nif')"
$tes3cmd modify "${result}" --hide-backups --exact-id "key_sandas"                --run "\$R->set({t=>'ITEX',f=>'Icon'},   'Keys\\Key_25.dds')"

# This hack is not needed with updated script. Just keeping it in case i need it again.
# xxd | hexedit comes to the rescue! :-\
# diff <(xxd <old>) <(xxd <new>) | cmp -l <old> <new>
# < 00010330: 54a2 0000 0000 0000 0000 0000 004e 414d  T............NAM
# > 00010330: 54a2 0000 0000 0000 0000 2000 004e 414d  T......... ..NAM
# < 00010c60: 2000 004e 414d 450b 0000 006b 6579 5f53   ..NAME....key_S
# > 00010c60: 0000 004e 414d 450b 0000 006b 6579 5f53  ...NAME....key_S
# < 00015b30: 0000 0000 0000 0000 4e41 4d45 1500 0000  ........NAME....
# > 00015b30: 0000 0000 0004 0000 4e41 4d45 1500 0000  ........NAME....
