OUTER_SET tobex_redist_version = 22

ACTION_IF NOT (FILE_EXISTS "TobEx_ini/TobExVer.txt") BEGIN

  PRINT @-10000 //TobEx not installed
  
  OUTER_SET tobex_installed_version = 0

  MKDIR "TobEx_ini"
  COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobExLoader.exe" "TobExLoader.exe"
  COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx.dll" "TobEx.dll"
  COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/default_core.ini" "TobEx_ini/TobExCore.ini"
  COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/default_tweak.ini" "TobEx_ini/TobExTweak.ini"
  COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx_ini.txt" "TobEx_ini/TobEx_ini.txt"
  COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobExVer.txt" "TobEx_ini/TobExVer.txt"

  //ASCII 'TobEx.dll'
  OUTER_PATCH_SAVE orig_bytes_1 ~~ BEGIN
    INSERT_BYTES 0x000 0xA
    WRITE_LONG   0x000 0xCCCCCCCC
    WRITE_LONG   0x004 0xCCCCCCCC
    WRITE_SHORT  0x008 0xCCCC
  END

  OUTER_PATCH_SAVE patch_bytes_1 ~~ BEGIN
    INSERT_BYTES 0x000 0xA
    WRITE_LONG   0x000 0x45626F54
    WRITE_LONG   0x004 0x6C642E78
    WRITE_SHORT  0x008 0x006C
  END

  //jmp 00A6F0F5
  OUTER_PATCH_SAVE orig_bytes_2 ~~ BEGIN
    INSERT_BYTES 0x000 0x5
    WRITE_LONG   0x000 0x629C2EE8
    WRITE_BYTE   0x004 0x00
  END

  OUTER_PATCH_SAVE patch_bytes_2 ~~ BEGIN
    INSERT_BYTES 0x000 0x5
    WRITE_LONG   0x000 0x63D437E9
    WRITE_BYTE   0x004 0x00
  END

  //push 0A39573, jmp short
  OUTER_PATCH_SAVE orig_bytes_3 ~~ BEGIN
    INSERT_BYTES 0x000 0x7
    WRITE_LONG   0x000 0xCCCCCCCC
    WRITE_SHORT  0x004 0xCCCC
    WRITE_BYTE   0x006 0xCC
  END

  OUTER_PATCH_SAVE patch_bytes_3 ~~ BEGIN
    INSERT_BYTES 0x000 0x7
    WRITE_LONG   0x000 0xA3957368
    WRITE_SHORT  0x004 0xEB00
    WRITE_BYTE   0x006 0x19
  END

  //call KERNEL32.LoadLibraryA, jmp short
  OUTER_PATCH_SAVE orig_bytes_4 ~~ BEGIN
    INSERT_BYTES 0x000 0x8
    WRITE_LONG   0x000 0xCCCCCCCC
    WRITE_LONG   0x004 0xCCCCCCCC
  END

  OUTER_PATCH_SAVE patch_bytes_4 ~~ BEGIN
    INSERT_BYTES 0x000 0x8
    WRITE_LONG   0x000 0x537015FF
    WRITE_LONG   0x004 0x18EB00AA
  END

  //call 0A5B8EC, jmp 431CBE
  OUTER_PATCH_SAVE orig_bytes_5 ~~ BEGIN
    INSERT_BYTES 0x000 0xA
    WRITE_LONG   0x000 0xCCCCCCCC
    WRITE_LONG   0x004 0xCCCCCCCC
    WRITE_SHORT  0x008 0xCCCC
  END

  OUTER_PATCH_SAVE patch_bytes_5 ~~ BEGIN
    INSERT_BYTES 0x000 0xA
    WRITE_LONG   0x000 0xFEC7B2E8
    WRITE_LONG   0x004 0x2B7FE9FF
    WRITE_SHORT  0x008 0xFF9C
  END

  COPY bgmain.exe bgmain.exe
    READ_ASCII 0x00639573 target_bytes_1 ELSE 0 (0xA)
    READ_ASCII 0x00031CB9 target_bytes_2 ELSE 0 (0x5)
    READ_ASCII 0x0066F0F5 target_bytes_3 ELSE 0 (0x7)
    READ_ASCII 0x0066F115 target_bytes_4 ELSE 0 (0x8)
    READ_ASCII 0x0066F135 target_bytes_5 ELSE 0 (0xA)
    PATCH_IF (~%target_bytes_1%~ STRING_EQUAL ~%orig_bytes_1%~)
      AND (~%target_bytes_2%~ STRING_EQUAL ~%orig_bytes_2%~)
      AND (~%target_bytes_3%~ STRING_EQUAL ~%orig_bytes_3%~)
      AND (~%target_bytes_4%~ STRING_EQUAL ~%orig_bytes_4%~)
      AND (~%target_bytes_5%~ STRING_EQUAL ~%orig_bytes_5%~)
    THEN BEGIN
      WRITE_ASCIIE 0x00639573 ~%patch_bytes_1%~ (0xA)
      WRITE_ASCIIE 0x00031CB9 ~%patch_bytes_2%~ (0x5)
      WRITE_ASCIIE 0x0066F0F5 ~%patch_bytes_3%~ (0x7)
      WRITE_ASCIIE 0x0066F115 ~%patch_bytes_4%~ (0x8)
      WRITE_ASCIIE 0x0066F135 ~%patch_bytes_5%~ (0xA)
    END ELSE
    PATCH_IF (~%target_bytes_1%~ STRING_EQUAL ~%patch_bytes_1%~)
      AND (~%target_bytes_2%~ STRING_EQUAL ~%patch_bytes_2%~)
      AND (~%target_bytes_3%~ STRING_EQUAL ~%patch_bytes_3%~)
      AND (~%target_bytes_4%~ STRING_EQUAL ~%patch_bytes_4%~)
      AND (~%target_bytes_5%~ STRING_EQUAL ~%patch_bytes_5%~)
    THEN BEGIN
      PATCH_PRINT @-10001
    END ELSE INNER_ACTION BEGIN FAIL @-10002 END
  BUT_ONLY

  //ugly compatibility stuff
  ACTION_IF (MOD_IS_INSTALLED ~DQ/SETUP-DQ.TP2~ 0) BEGIN
    COPY "TobEx_ini/TobExCore.ini" "TobEx_ini/TobExCore.ini"
      REPLACE_TEXTUALLY ~Externalise Race Selection StrRef=1~ ~Externalise Race Selection StrRef=0~
    BUT_ONLY
  END

END
ELSE BEGIN

  COPY "TobEx_ini/TobExVer.txt" "TobEx_ini/TobExVer.txt"
    READ_2DA_ENTRY 0 0 1 tobex_installed_version
  BUT_ONLY
  
  ACTION_IF (%tobex_redist_version% > %tobex_installed_version%) BEGIN

    PRINT @-10003 //installed version older than packaged version -> update

    COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx.dll" "TobEx.dll"
    COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/default_core.ini" "TobEx_ini/TobExCore.ini"
    COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx_ini.txt" "TobEx_ini/TobEx_ini.txt"
    COPY "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobExVer.txt" "TobEx_ini/TobExVer.txt"

  END
  ELSE BEGIN

    PRINT @-10004 //installed version equal or newer to packaged version -> do nothing

  END

END

//incremental updates
ACTION_IF (%tobex_installed_version% < 18) BEGIN
  INCLUDE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0018.tpa"

  COPY "TobEx_ini/TobExTweak.ini" "TobEx_ini/TobExTweak.ini"
    APPEND_FILE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0018t.txt"
END

ACTION_IF (%tobex_installed_version% < 19) BEGIN
  INCLUDE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0019.tpa"

  COPY "TobEx_ini/TobExTweak.ini" "TobEx_ini/TobExTweak.ini"
    REPLACE_TEXTUALLY "Tweak:Enable PickpocketFailed Trigger" "Trigger:Enable PickpocketFailed Trigger"
  BUT_ONLY_IF_IT_CHANGES
END

ACTION_IF (%tobex_installed_version% < 20) BEGIN
  INCLUDE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0020.tpa"

  COPY "TobEx_ini/TobExTweak.ini" "TobEx_ini/TobExTweak.ini"
    APPEND_FILE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0020t.txt"
END

ACTION_IF (%tobex_installed_version% < 21) BEGIN
  INCLUDE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0021.tpa"
END

ACTION_IF (%tobex_installed_version% < 22) BEGIN
  INCLUDE "%TOBEX_MOD_DIRECTORY%/TobEx_redist/TobEx0022.tpa"
END
