; ENVIRONMENT
;------------------------------------------------
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Enable warnings to assist with detecting common errors.
;DetectHiddenWindows, On
#SingleInstance, Force
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;SetKeyDelay, 500
CoordMode, ToolTip, Screen
CoordMode, Mouse, Screen
;#NoTrayIcon
Menu, Tray, Icon, Update.ico
DetectHiddenWindows, ON



ScriptName = Script Updater
ScriptVersion = 1.0
FullScriptName := ScriptName . " "  ScriptVersion


;---Changelog------------------------------------------------------
; Fixed Locals not saving successful upload status to correct file
; Added Locals Upload status to Upload Results screen
;---/Changelog------------------------------------------------------





; Notes/Extra Info/#Includes
;------------------------------------------------
#Include, %A_scriptdir%\SharedFunctions.ahk


; VARIABLES
;------------------------------------------------
SettingsIniFileLocation := A_ScriptDir . "\Settings.ini"
ScriptToUpdate = %1%
ScriptToUpdate = VideoUploader



IniRead, DiscordErrorLoggingWebhookBotURL, %SettingsIniFileLocation%, SocialMediaPoster, DiscordErrorLoggingWebhookBotURL, %A_space%
SplitPath, A_scriptdir, OutFileName, ParentDirectory, OutExtension, OutNameNoExt, OutDrive




; MAIN SCRIPT
;------------------------------------------------
; Check what the current update

if(ScriptToUpdate = "VideoUploader"){
	; Current Location of the calling script
	IniRead, CallingScriptFilepath, %SettingsIniFileLocation%, General, VideoUploaderFilepath
	Iniread, CallingScriptUpdateURL, %SettingsIniFilepath%, General, VideoUploaderURL
	Iniread, CallingScriptChangelogURL, %SettingsIniFilepath%, General, VideoUploaderChangelogURL
	Iniread, CallingScriptCurrentVersion, %SettingsIniFilepath%, General, ScriptVersion
	; Iniread, CallingScriptVersion, %SettingsIniFilepath%, General, ScriptVersion
}

if(ScriptToUpdate = "SocialMediaPoster"){
	; Current Location of the calling script
	IniRead, CallingScriptFilepath, %SettingsIniFileLocation%, SocialMediaPoster, VideoUploaderFilepath
	Iniread, CallingScriptUpdateURL, %SettingsIniFilepath%, SocialMediaPoster, VideoUploaderURL
	Iniread, CallingScriptCurrentVersion, %SettingsIniFilepath%, SocialMediaPoster, ScriptVersion
}



UpdateChangelog := URLDownloadToVar(CallingScriptChangelogURL)
Msgbox % "UpdateChangelog: " UpdateChangelog


Msgbox % "ScriptToUpdate: " ScriptToUpdate
Msgbox % "CallingScriptFilepath: " CallingScriptFilepath
Msgbox % "CallingScriptUpdateURL: " CallingScriptUpdateURL
Msgbox % "CallingScriptChangelogURL: " CallingScriptChangelogURL
Msgbox % "CallingScriptVersion: " CallingScriptVersion





; Kill the Original Script in order to delete it
WinClose, %ScriptFilepath% ahk_class AutoHotkey 
sleep, 500
WinClose, %ScriptFilepath% ahk_class AutoHotkey 

; Delete the original file
FileDelete, %ScriptFilepath%





; Functions
;------------------------------------------------
URLDownloadToVar(url){
	hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
	hObject.Open("GET",url)
	hObject.Send()
	return hObject.ResponseText
}