/*
http://get.daum.net/PotPlayer/v3/PotPlayerSetup.exe
http://get.daum.net/PotPlayer64/v3/PotPlayerSetup64.exe

http://get.daum.net/PotPlayer/Codec/OpenCodecSetup.exe
http://get.daum.net/PotPlayer/Codec/OpenCodecSetup64.exe
*/
!define RELEASURL	"http://get.daum.net/PotPlayer/v3/PotPlayerSetup.exe"
!define RELEASURL64	"http://get.daum.net/PotPlayer64/v3/PotPlayerSetup64.exe"
!define CODECURL	"http://get.daum.net/PotPlayer/Codec/OpenCodecSetup.exe"
!define CODECURL64	"http://get.daum.net/PotPlayer/Codec/OpenCodecSetup64.exe"
!define APPSIZE	"34600" # kB
!define DLVER	"1.6.x"
!define APPVER 	"0.0.0.0"
!define APPNAME "PotPlayer"
!define APP 	"PotPlayer"
!define DLNAME	"PotPlayer"
!define APPLANG	"32-64-bit_Multilingual_Online"
!define FOLDER	"PotPlayerPortable"
!define FINISHRUN ; Delete if not Finish pages
!define OPTIONS ; Delete if no Components
!define SOURCES ; Delete if no Sources
; !define DESCRIPTION	"Multimedia Player (KMPlayer Reloaded)" ; Delete if no AppInfo

SetCompressor /SOLID lzma
SetCompressorDictSize 32

!include "..\_Include\Installer.nsh"
!include "LogicLib.nsh"
!include "x64.nsh"

!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "Bulgarian"
!insertmacro MUI_LANGUAGE "SimpChinese"
!insertmacro MUI_LANGUAGE "TradChinese"
!insertmacro MUI_LANGUAGE "French"
!insertmacro MUI_LANGUAGE "German"
!insertmacro MUI_LANGUAGE "Hebrew"
!insertmacro MUI_LANGUAGE "Hungarian"
!insertmacro MUI_LANGUAGE "Japanese"
!insertmacro MUI_LANGUAGE "Korean"
!insertmacro MUI_LANGUAGE "Polish"
!insertmacro MUI_LANGUAGE "Portuguese"
!insertmacro MUI_LANGUAGE "Russian"
!insertmacro MUI_LANGUAGE "Serbian"
!insertmacro MUI_LANGUAGE "Spanish"
!insertmacro MUI_LANGUAGE "Swedish"
!insertmacro MUI_LANGUAGE "Thai"
!insertmacro MUI_LANGUAGE "Turkish"
!insertmacro MUI_LANGUAGE "Ukrainian"

Section "${APPNAME} Portable 32 bit" main
SectionIn RO

	File "/oname=$TEMP\${APP}PortableTemp\7z.dll" "..\_Include\7-Zip\7z.dll"
	File "/oname=$TEMP\${APP}PortableTemp\7z.exe" "..\_Include\7-Zip\7z.exe"

${If} ${FileExists} "$EXEDIR\PotPlayerSetup.exe"
DetailPrint "Installing ${APPNAME} Portable"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$EXEDIR\PotPlayerSetup.exe" -aoa -o"$TEMP\${APP}PortableTemp\PotPlayerSetup"`
${Else}
Call CheckConnected
	inetc::get "${RELEASURL}" "$TEMP\${APP}PortableTemp\PotPlayerSetup.exe" /END
	Pop $0
StrCmp $0 "OK" +3
	MessageBox MB_USERICON "Download of PotPlayerSetup.exe: $0"
	Abort
DetailPrint "Installing ${APPNAME} Portable 32 bit"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\PotPlayerSetup.exe" -aoa -o"$TEMP\${APP}PortableTemp\PotPlayerSetup"`
${EndIf}

	SetOutPath "$INSTDIR"
		File "..\..\..\${FOLDER}\${APP}Portable.exe"
	SetOutPath "$INSTDIR\App\DefaultData\PotPlayer"
		File "PotPlayer.ini"


	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\$$EXEDIR"
	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\$$PLUGINSDIR"
	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\$$SYSDIR"
	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\$$TEMP"
	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\Html"
	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\Logos"
	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetup\Module\FFmpeg\$$PLUGINSDIR"
	Delete "$TEMP\${APP}PortableTemp\PotPlayerSetup\Daum_Movie.resource"
	Delete "$TEMP\${APP}PortableTemp\PotPlayerSetup\DaumDownloader.exe"
	Delete "$TEMP\${APP}PortableTemp\PotPlayerSetup\PotPlayer.exe"
	Rename "$TEMP\${APP}PortableTemp\PotPlayerSetup\PotPlayerMini.exe" "$TEMP\${APP}PortableTemp\PotPlayerSetup\PotPlayer.exe"
	Delete "$TEMP\${APP}PortableTemp\PotPlayerSetup\PotPlayerXP.exe"
	Rename "$TEMP\${APP}PortableTemp\PotPlayerSetup\PotPlayerMiniXP.exe" "$TEMP\${APP}PortableTemp\PotPlayerSetup\PotPlayerXP.exe"

	SetOutPath "$INSTDIR\App\${APP}"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetup\*.*" "$INSTDIR\App\${APP}\"

!ifdef DESCRIPTION
Call AppInfo
!endif
!ifdef SOURCES
Call Sources
	SetOutPath "$INSTDIR\Other\_Include\7-Zip"
	File "..\_Include\7-Zip\7z.exe"
	File "..\_Include\7-Zip\7z.dll"
	SetOutPath "$INSTDIR\Other\Source"
	File "${APP}.ini"
!endif
!ifdef SOURCES & DESCRIPTION
Call SourceInfo
!endif

SectionEnd

Section /o "${APPNAME} Portable 64 bit" x64
${If} ${FileExists} "$EXEDIR\PotPlayerSetup64.exe"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$EXEDIR\PotPlayerSetup64.exe" -aoa -o"$TEMP\${APP}PortableTemp\PotPlayerSetupx64"`
${Else}
	inetc::get "${RELEASURL64}" "$TEMP\${APP}PortableTemp\PotPlayerSetup64.exe" /END
	Pop $0
StrCmp $0 "OK" +3
	MessageBox MB_USERICON "Download of PotPlayerSetup64.exe: $0"
	Abort
DetailPrint "Installing ${APPNAME} Portable 64 bit"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\PotPlayerSetup64.exe" -aoa -o"$TEMP\${APP}PortableTemp\PotPlayerSetupx64"`
${EndIf}

	Delete "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\PotPlayer64.exe"
	Rename "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\PotPlayerMini64.exe" "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\PotPlayer64.exe"
	Rename "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\D3DCompiler_43.dll" "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\D3DCompiler_43_64.dll"
	Rename "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\d3dx9_43.dll" "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\d3dx9_43_64.dll"

	RMDir "/r" "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\Module\FFmpeg\$$PLUGINSDIR"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\Module\*.*" "$INSTDIR\App\${APP}\Module\"

	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\PotPlayer64.exe" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\PotPlayer64.dll" "$INSTDIR\App\${APP}\"

	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\D3DCompiler_43_64.dll" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\d3dx9_43_64.dll" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\ATextOut64.dll" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\D_Exec64.exe" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\DaumCrashHandler64.dll" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\DChat64.dll" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\DTDrop64.exe" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\ffcodec64.dll" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\KillPot64.exe" "$INSTDIR\App\${APP}\"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\PotPlayerSetupx64\PotIcons64.dll" "$INSTDIR\App\${APP}\"
SectionEnd

SectionGroup "Open Codec"

Section /o "32 bit" codec32
Call CheckConnected
	inetc::get "${CODECURL}" "$TEMP\${APP}PortableTemp\OpenCodecSetup.exe" /END
	Pop $0
StrCmp $0 "OK" +3
	MessageBox MB_USERICON "Download of OpenCodecSetup.exe: $0"
	Abort
DetailPrint "Installing Open Codec 32 bit"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\OpenCodecSetup.exe" -aoa -o"$INSTDIR\App\${APP}"`
SectionEnd

Section /o "64 bit" codec64
Call CheckConnected
	inetc::get "${CODECURL64}" "$TEMP\${APP}PortableTemp\OpenCodecSetup64.exe" /END
	Pop $0
StrCmp $0 "OK" +3
	MessageBox MB_USERICON "Download of OpenCodecSetup64.exe: $0"
	Abort
DetailPrint "Installing Open Codec 64 bit"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\OpenCodecSetup64.exe" -aoa -o"$INSTDIR\App\${APP}"`
	RMDir "/r" "$INSTDIR\App\${APP}\Module\Libav\$$PLUGINSDIR"
SectionEnd

SectionGroupEnd

Function .onGUIEnd
	RMDir "/r" "$TEMP\${APP}PortableTemp"
FunctionEnd

Function Init
SectionSetSize ${x64} 32800 # kB
SectionSetSize ${codec32} 19000 # kB
SectionSetSize ${codec64} 20170 # kB
${If} ${RunningX64}
SectionSetFlags ${x64} 1
${Else}
${EndIf}
FunctionEnd

