################################################################################
# define
################################################################################
!define REG    `${SET}\${APP}.reg`
!define DEFREG `${DEFSET}\${APP}.reg`
!define YDS    `${SET}\scripts0.yds`
!define DEFYDS `${DEFSET}\scripts0.yds`
!define GA     `Software\GreenTree Applications\YTD`
!define SEC    `HKEY_CURRENT_USER\${GA}`
!define KEY    Language
!define YTD    `$0\YTD Video Downloader`
!define FP     `$APPDATA\Adobe\Flash Player`
!define ASS    `${FP}\AssetCache`
!define OBJ    `$APPDATA\Macromedia\Flash Player\#SharedObjects`
!define SOL1   `$APPDATA\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\settings.sol`
!define SOL2   `$APPDATA\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\#s.ytimg.com\settings.sol`
!define SOL3   `$APPDATA\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\s.ytimg.com\settings.sol`
!define AiR    `$APPDATA\Macromedia\Flash Player\#Security\FlashPlayerTrust\air.1.0.trust.cfg`
!define PFM    `$0\PortableApps.com\PortableAppsPlatform.exe`
################################################################################
# macro
################################################################################
!define Backup::AiR "!insertmacro _Backup::AiR"
!macro _Backup::AiR _DLL _KEY
	IfFileExists `${_DLL}` 0 +3
	WriteINIStr `${RUNTIME}` ${PAL} ${_KEY} 1
	WriteINIStr `${RUNTIME2}` ${PAL} ${_KEY} 1
!macroend
!define Restore::AiR "!insertmacro _Restore::AiR"
!macro _Restore::AiR _DLL _KEY _VAR
	IfFileExists `${RUNTIME}` 0 +4
		ClearErrors
		ReadINIStr ${_VAR} `${RUNTIME}` ${PAL} ${_KEY}
		Goto +3
	ClearErrors
	ReadINIStr ${_VAR} `${RUNTIME2}` ${PAL} ${_KEY}
	IfErrors 0 +2
	Delete `${_DLL}`
!macroend
################################################################################
# function
################################################################################
Function GetLCID
	!macro _GetLCID _LNG _LCID
		Push ${_LNG}
		Call GetLCID
		Pop ${_LCID}
	!macroend
	!define GetLCID `!insertmacro _GetLCID`
	Exch $0
	StrCmpS $0 1052 +30
	StrCmpS $0 1025 +29
	StrCmpS $0 1059 +28
	StrCmpS $0 1026 +27
	StrCmpS $0 1050 +26
	StrCmpS $0 1029 +25
	StrCmpS $0 1030 +24
	StrCmpS $0 1043 +23
	StrCmpS $0 1033 +22
	StrCmpS $0 1061 +21
	StrCmpS $0 1035 +20
	StrCmpS $0 1036 +19
	StrCmpS $0 1031 +18
	StrCmpS $0 1032 +17
	StrCmpS $0 1038 +16
	StrCmpS $0 1040 +15
	StrCmpS $0 9999 +14
	StrCmpS $0 1044 +13
	StrCmpS $0 1045 +12
	StrCmpS $0 2070 +11
	StrCmpS $0 1048 +10
	StrCmpS $0 1049 +9
	StrCmpS $0 3098 +8
	StrCmpS $0 2052 +7
	StrCmpS $0 1051 +6
	StrCmpS $0 1060 +5
	StrCmpS $0 1034 +4
	StrCmpS $0 1053 +3
	StrCmpS $0 1055 +2
	StrCpy $0 error
	Exch $0
FunctionEnd
################################################################################
# custom
################################################################################
!define MsgBox "!insertmacro MsgBox"
!macro MsgBox
	StrCpy $0 `Windows XP or newer`
	MessageBox MB_ICONSTOP|MB_TOPMOST `$(MINREQ)`
	Call Unload
	Quit
!macroend
!macro OS
	${If} ${IsNT}
		${IfNot} ${AtLeastWinXP}
			${MsgBox}
		${EndIf}
	${Else}
		${MsgBox}
	${EndIf}
!macroend
Function PortableApps.comLocaleID
	IfFileExists '${REG}' 0 +3
	ReadINIStr $0 '${REG}' '${SEC}' '"${KEY}"'
	Goto +2
	ReadINIStr $0 '${DEFREG}' '${SEC}' '"${KEY}"'
	Push $0
	Call GetLCID
	Pop $0
	StrCmpS $0 error 0 +2
	StrCpy $0 1033
	System::Call 'Kernel32::SetEnvironmentVariable(t "PortableApps.comLocaleID", t "$0")'
FunctionEnd
!macro Lang
	Push '$EXEDIR'
	Call Get.Parent
	Pop $0
	IfFileExists '${PFM}' 0 +6
	ReadEnvStr $0 PortableApps.comLocaleID
	Push $0
	Call GetLCID
	Pop $0
	StrCmpS $0 error 0 +2
	Call PortableApps.comLocaleID
!macroend
!macro Init
	IfFileExists `${REG}` +2
	CopyFiles /SILENT `${DEFREG}` `${REG}`
	IfFileExists `${YDS}` +2
	CopyFiles /SILENT `${DEFYDS}` `${YDS}`
!macroend
!macro DirCleanup
	FindFirst $0 $1 `${ASS}\*.*`
	StrCmpS $0 "" +8
	StrCmpS $1 "" +7
	StrCmpS $1 "." +4
	StrCmpS $1 ".." +3
	IfFileExists `${ASS}\$1\*.txt` 0 +2
	Delete `${ASS}\$1\*.*`
	FindNext $0 $1
	Goto -6
	FindClose $0
	${Restore::AiR} `${AIR}` AIR $0
	${Restore::AiR} `${SOL1}` SOL1 $0
	${Restore::AiR} `${SOL2}` SOL2 $0
	${Restore::AiR} `${SOL3}` SOL3 $0
	${RMEmptyDir} `${ASS}` ""
	${RMEmptyDir} `${OBJ}` s.ytimg.com
	${RMEmptyDir} `${OBJ}` `#s.ytimg.com`
!macroend
!macro PreDirMove
	${Backup::AiR} `${AIR}` AIR
	${Backup::AiR} `${SOL1}` SOL1
	${Backup::AiR} `${SOL2}` SOL2
	${Backup::AiR} `${SOL3}` SOL3
!macroend
${SegmentUnload}
	ReadEnvStr $0 ALLUSERSAPPDATA
	${If} ${FileExists} `${YTD}\*.exe`
		FindFirst $2 $1 `${YTD}\*.exe`
		ClearErrors
		Delete `${YTD}\$1`
		${If} ${Errors}
			${TerminateProcess} $1 $2
			Sleep 1000
			ClearErrors
			Delete `${YTD}\$1`
			IfErrors 0 +4
			Rename `${YTD}\$1` `$TEMP\$1`
			Delete /REBOOTOK `$TEMP\$1`
			RMDir `${YTD}`
		${EndIf}
	${EndIf}
!macroend