#!/usr/bin/env bash
# vim: nowrap
#
# Changelog:
# 2021-09-12
#   - Forward TLAD Necro Edit v3.1 time changes

tes3cmd="./tes3cmd"

source_gf_tlad_tweak="Glowing Flames - TrueLightsAndDarkness Tweaks.ESP"
source_tlad_still_nc="TLAD Full - Still - Necro Colors.esp"
source_tlad_flick_nc="TLAD Lights - Logical Flicker - Necro Colors.esp"
source_noct_moth_tld="Nocturnal Moths - TLD.ESP"

result1="Glowing Flames - TLAD Still - Necro Colors.esp"
result2="Glowing Flames - TLAD Logical Flicker - Necro Colors.esp"
result3="Nocturnal Moths - TLAD Still - Necro Colors.esp"
result4="Nocturnal Moths - TLAD Logical Flicker - Necro Colors.esp"
result5="Glowing Flames - Nocturnal Moths - TLAD Still - Necro Colors.esp"
result6="Glowing Flames - Nocturnal Moths - TLAD Logical Flicker - Necro Colors.esp"

# 01. Create replacement patch for "Glowing Flames" with changes from "TLAD Full - Still - Necro Colors.esp"

# 01.01 "Glowing Flames - TrueLightsAndDarkness Tweaks.ESP"

cp -v "${source_gf_tlad_tweak}" "${result1}"

# modify records
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_candle_07_64"       --run "\$R->set({t=>'LHDT', f=>'Flags'}, '17')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_01"         --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_01_128"     --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_01_256"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_01_77"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_01_off"     --run "\$R->set({t=>'LHDT', f=>'Flags'}, '51'), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02"         --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02_128"     --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02_128_off" --run "\$R->set({t=>'LHDT', f=>'Flags'}, '51'), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02_177"     --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02_256"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02_64"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_lantern_02_off"     --run "\$R->set({t=>'LHDT', f=>'Flags'}, '51'), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_01"           --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_01_128"       --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_01_200"       --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_01_256"       --run "\$R->set({t=>'LHDT', f=>'Flags'}, '27'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_01_77"        --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_01_off"       --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_torch_02"           --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch"                        --run "\$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_128"                    --run "\$R->set({t=>'LHDT', f=>'Time'}, '10800')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_128_off"                --run "\$R->set({t=>'LHDT', f=>'Time'}, '10800')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_157"                    --run "\$R->set({t=>'LHDT', f=>'Time'}, '10800')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_256"                    --run "\$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_64"                     --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_77"                     --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_infinite_time"          --run "\$R->set({t=>'LHDT', f=>'Flags'}, '59'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "torch_infinite_time_unique"   --run "\$R->set({t=>'LHDT', f=>'Flags'}, '59'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"

# delete records to use them from TLAD Necro
${tes3cmd} delete "${result1}" --hide-backups\
  --exact-id "light_com_lantern_01_s"\
  --exact-id "light_com_lantern_02_177_boat01"\
  --exact-id "light_com_lantern_02_200_boat"\
  --exact-id "light_com_lantern_02_inf"\
  --exact-id "light_com_lantern_bm_unique"\
  --exact-id "light_com_lantern_sweetie1"\
  --exact-id "light_com_lantern_sweetie2"\
  --exact-id "light_com_lantern_sweetie3"\
  --exact-id "light_de_candle_10"\
  --exact-id "light_de_lamp_05"\
  --exact-id "light_de_lamp_05_128"\
  --exact-id "light_de_lantern_07"\
  --exact-id "light_de_lantern_07_128"

# 01.02 "Glowing Flames - NoMoreLightlessFlames v1.1.ESP"
${tes3cmd} dump "${source_tlad_still_nc}" --raw "${result1}" --type LIGH\
  --exact-id "light_com_candle_01_off"\
  --exact-id "light_com_candle_05_off"\
  --exact-id "light_com_candle_09_off"\
  --exact-id "light_com_candle_10_off"\
  --exact-id "light_com_candle_12_off"\
  --exact-id "light_de_candle_06_off"\
  --exact-id "light_de_candle_11_off"\
  --exact-id "light_de_candle_128_off"\
  --exact-id "light_de_candle_13_off"\
  --exact-id "light_de_candle_blue_01_off"\
  --exact-id "light_de_candle_green_01_off"\
  --exact-id "light_de_candle_ivory_01_off"\
  --exact-id "light_de_candle_ivory_dead"\
  --exact-id "light_de_lantern_02_256_off"

${tes3cmd} header "${result1}" --hide-backups --update-record-count

${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_candle_01_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_01_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_candle_05_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_05_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_candle_09_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_09_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_candle_10_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_10_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_com_candle_12_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_12_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_06_off"       --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_06_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_11_off"       --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_11_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_128_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_12_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_13_off"       --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_13_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_blue_01_off"  --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_blue_1off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_green_01_off" --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_grn_1_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_ivory_01_off" --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_ivory_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_candle_ivory_dead"   --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_ivory_off.nif')"
${tes3cmd} modify "${result1}" --hide-backups --exact-id "light_de_lantern_02_256_off"  --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_lantern_02_off.nif')"

# 02. Create replacement patch for "Glowing Flames" with changes from "TLAD Lights - Logical Flicker - Necro Colors.esp"

# 02.01 "Glowing Flames - TrueLightsAndDarkness Tweaks.ESP"

cp -v "${source_gf_tlad_tweak}" "${result2}"

# modify records

${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_01"         --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_01_128"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_01_256"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_01_77"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_01_off"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02"         --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02_128"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02_128_off" --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02_177"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02_256"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02_64"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_lantern_02_off"     --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_01"           --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_01_128"       --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_01_200"       --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_01_77"        --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_01_off"       --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_02"           --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch"                        --run "\$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_128"                    --run "\$R->set({t=>'LHDT', f=>'Time'}, '10800')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_128_off"                --run "\$R->set({t=>'LHDT', f=>'Time'}, '10800')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_157"                    --run "\$R->set({t=>'LHDT', f=>'Time'}, '10800')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_256"                    --run "\$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_64"                     --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_77"                     --run "\$R->set({t=>'LHDT', f=>'Time'}, '3600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_infinite_time"          --run "\$R->set({t=>'LHDT', f=>'Flags'}, '115'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "torch_infinite_time_unique"   --run "\$R->set({t=>'LHDT', f=>'Flags'}, '115'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_torch_01_256"       --run "\$R->set({t=>'LHDT', f=>'Flags'}, '83'), \$R->set({t=>'LHDT', f=>'Time'}, '21600')"

# delete records to use them from TLAD Necro
${tes3cmd} delete "${result2}" --hide-backups\
  --exact-id "light_com_lantern_01_s"\
  --exact-id "light_com_lantern_02_177_boat01"\
  --exact-id "light_com_lantern_02_200_boat"\
  --exact-id "light_com_lantern_02_inf"\
  --exact-id "light_com_lantern_bm_unique"\
  --exact-id "light_com_lantern_sweetie1"\
  --exact-id "light_com_lantern_sweetie2"\
  --exact-id "light_com_lantern_sweetie3"\
  --exact-id "light_de_candle_10"\
  --exact-id "light_de_lamp_05"\
  --exact-id "light_de_lamp_05_128"\
  --exact-id "light_de_lantern_07"\
  --exact-id "light_de_lantern_07_128"

# 02.02 "Glowing Flames - NoMoreLightlessFlames v1.1.ESP"
${tes3cmd} dump "${source_tlad_flick_nc}" --raw "${result2}" --type LIGH\
  --exact-id "light_com_candle_01_off"\
  --exact-id "light_com_candle_05_off"\
  --exact-id "light_com_candle_09_off"\
  --exact-id "light_com_candle_10_off"\
  --exact-id "light_com_candle_12_off"\
  --exact-id "light_de_candle_06_off"\
  --exact-id "light_de_candle_11_off"\
  --exact-id "light_de_candle_128_off"\
  --exact-id "light_de_candle_13_off"\
  --exact-id "light_de_candle_blue_01_off"\
  --exact-id "light_de_candle_green_01_off"\
  --exact-id "light_de_candle_ivory_01_off"\
  --exact-id "light_de_candle_ivory_dead"\
  --exact-id "light_de_lantern_02_256_off"

${tes3cmd} header "${result2}" --hide-backups --update-record-count

${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_candle_01_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_01_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_candle_05_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_05_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_candle_09_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_09_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_candle_10_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_10_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_com_candle_12_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_com_candle_12_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_06_off"       --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_06_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_11_off"       --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_11_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_128_off"      --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_12_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_13_off"       --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_13_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_blue_01_off"  --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_blue_1off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_green_01_off" --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_grn_1_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_ivory_01_off" --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_ivory_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_candle_ivory_dead"   --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_candle_ivory_off.nif')"
${tes3cmd} modify "${result2}" --hide-backups --exact-id "light_de_lantern_02_256_off"  --run "\$R->set({t=>'MODL', f=>'Model'}, 'l\\light_de_lantern_02_off.nif')"

# 03. Create additional patch for "Nocturnal Moths - TLD.ESP" and "TLAD Full - Still - Necro Colors.esp"

${tes3cmd} dump "${source_tlad_still_nc}" --raw-with-header "${result3}" --type LIGH\
  --exact-id "light_com_lantern_01"\
  --exact-id "light_com_lantern_01_128"\
  --exact-id "light_com_lantern_02"\
  --exact-id "light_de_lantern_03_200_r"\
  --exact-id "light_de_lantern_13_128"\
  --exact-id "light_de_paper_lantern_01"\
  --exact-id "light_de_paper_lantern_01_200"\
  --exact-id "light_de_paper_lantern_02"\
  --exact-id "light_de_paper_lantern_03"

${tes3cmd} dump "${source_noct_moth_tld}" --raw "${result3}" --type LIGH\
  --exact-id "light_com_lantern_02_128"

${tes3cmd} header "${result3}" --hide-backups --update-record-count

${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_com_lantern_01"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'})), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_com_lantern_01_128"      --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'})), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_com_lantern_02"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'})), \$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19'), \$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_de_lantern_03_200_r"     --run "\$R->set({t=>'SCRI', f=>'Script'}, 'R0_mthplcr_de01r_scr')"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_de_lantern_13_128"       --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de01_scr'}))"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_de_paper_lantern_01"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_de_paper_lantern_01_200" --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_de_paper_lantern_02"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result3}" --hide-backups --exact-id "light_de_paper_lantern_03"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"

# 04. Create additional patch for "Nocturnal Moths - TLD.ESP" and "TLAD Lights - Logical Flicker - Necro Colors.esp"

${tes3cmd} dump "${source_tlad_flick_nc}" --raw-with-header "${result4}" --type LIGH\
  --exact-id "light_de_lantern_03_200_r"\
  --exact-id "light_de_lantern_13_128"\
  --exact-id "light_de_paper_lantern_01"\
  --exact-id "light_de_paper_lantern_01_200"\
  --exact-id "light_de_paper_lantern_02"\
  --exact-id "light_de_paper_lantern_03"

${tes3cmd} dump "${source_noct_moth_tld}" --raw "${result4}" --type LIGH\
  --exact-id "light_com_lantern_01"\
  --exact-id "light_com_lantern_01_128"\
  --exact-id "light_com_lantern_02"\
  --exact-id "light_com_lantern_02_128"

${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_com_lantern_01"          --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400'), \$R->set({t=>'LHDT', f=>'Radius'}, '600')"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_com_lantern_01_128"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_com_lantern_02"          --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400'), \$R->set({t=>'LHDT', f=>'Radius'}, '600')"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_de_lantern_03_200_r"     --run "\$R->set({t=>'SCRI', f=>'Script'}, 'R0_mthplcr_de01r_scr')"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_de_lantern_13_128"       --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de01_scr'}))"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_de_paper_lantern_01"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_de_paper_lantern_01_200" --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_de_paper_lantern_02"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result4}" --hide-backups --exact-id "light_de_paper_lantern_03"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"

# 05. Create additional patch for "Glowing Flames" , "Nocturnal Moths - TLD.ESP" and "TLAD Full - Still - Necro Colors.esp"

# first the ones with script

cp -v "${result1}" "${result5}"

${tes3cmd} delete "${result5}" --hide-backups\
  --exact-id "light_com_lantern_02_128"

${tes3cmd} dump "${source_noct_moth_tld}" --raw "${result5}" --type LIGH\
  --exact-id "light_com_lantern_01_128_r0c"\
  --exact-id "light_com_lantern_01_r0c"\
  --exact-id "light_com_lantern_02_128"\
  --exact-id "light_com_lantern_02_128_r0c"\
  --exact-id "light_com_lantern_02_r0c"

${tes3cmd} dump "${source_tlad_still_nc}" --raw "${result5}" --type LIGH\
  --exact-id "light_de_lantern_03_200_r"\
  --exact-id "light_de_lantern_13_128"\
  --exact-id "light_de_paper_lantern_01"\
  --exact-id "light_de_paper_lantern_01_200"\
  --exact-id "light_de_paper_lantern_02"\
  --exact-id "light_de_paper_lantern_03"

${tes3cmd} header "${result5}" --hide-backups --update-record-count

# Script:R0_mthplcr_de02_scr
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_de_paper_lantern_01"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_de_paper_lantern_01_200" --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_de_paper_lantern_02"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_de_paper_lantern_03"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
# Script:R0_mthplcr_de01_scr
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_de_lantern_03_200_r"     --run "\$R->set({t=>'SCRI', f=>'Script'}, 'R0_mthplcr_de01r_scr')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_de_lantern_13_128"       --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de01_scr'}))"
# Script:R0_mthplcr_com01_scr
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'}))"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_128"      --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'}))"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'}))"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Radius'}, '437')"
# remove flicker
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_r0c"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Flags'}, '19')"
# radius
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Radius'}, '437')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_r0c"      --run "\$R->set({t=>'LHDT', f=>'Radius'}, '625')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Radius'}, '437')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Radius'}, '625')"
# time
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_01_r0c"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
# color, BGR hex to decimal
${tes3cmd} modify "${result5}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Color'}, '3311600')"

# 06. Create additional patch for "Glowing Flames" , "Nocturnal Moths - TLD.ESP" and "TLAD Lights - Logical Flicker - Necro Colors.esp"

# first the ones with script

cp -v "${result2}" "${result6}"

${tes3cmd} delete "${result6}" --hide-backups\
  --exact-id "light_com_lantern_02_128"

${tes3cmd} dump "${source_noct_moth_tld}" --raw "${result6}" --type LIGH\
  --exact-id "light_com_lantern_01_128_r0c"\
  --exact-id "light_com_lantern_01_r0c"\
  --exact-id "light_com_lantern_02_128"\
  --exact-id "light_com_lantern_02_128_r0c"\
  --exact-id "light_com_lantern_02_r0c"

${tes3cmd} dump "${source_tlad_flick_nc}" --raw "${result6}" --type LIGH\
  --exact-id "light_de_lantern_03_200_r"\
  --exact-id "light_de_lantern_13_128"\
  --exact-id "light_de_paper_lantern_01"\
  --exact-id "light_de_paper_lantern_01_200"\
  --exact-id "light_de_paper_lantern_02"\
  --exact-id "light_de_paper_lantern_03"

${tes3cmd} header "${result6}" --hide-backups --update-record-count

# Script:R0_mthplcr_de02_scr
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_de_paper_lantern_01"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_de_paper_lantern_02"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_de_paper_lantern_03"     --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_de_paper_lantern_01_200" --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de02_scr'}))"
# Script:R0_mthplcr_de01_scr
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_de_lantern_13_128"       --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_de01_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_de_lantern_03_200_r"     --run "\$R->set({t=>'SCRI', f=>'Script'}, 'R0_mthplcr_de01r_scr')"
# Script:R0_mthplcr_com01_scr
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_01"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02"          --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_01_128"      --run "\$R->append(LIGH::SCRI->new({script=>'R0_mthplcr_com01_scr'}))"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Radius'}, '437')"
# radius
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_01_r0c"      --run "\$R->set({t=>'LHDT', f=>'Radius'}, '625')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Radius'}, '625')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_01_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Radius'}, '437')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Radius'}, '437')"
# time
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_01_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_01_r0c"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_128"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_128_r0c"  --run "\$R->set({t=>'LHDT', f=>'Time'}, '43200')"
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Time'}, '86400')"
# color, BGR hex to decimal
${tes3cmd} modify "${result6}" --hide-backups --exact-id "light_com_lantern_02_r0c"      --run "\$R->set({t=>'LHDT', f=>'Color'}, '3311600')"
