/*
https://vivaldi.com/download/download.php?f=Vivaldi.1.0.196.2.exe
https://vivaldi.com/download/download.php?f=Vivaldi.1.0.196.2.x64.exe
https://vivaldi.com/download/snapshot/Vivaldi.1.0.196.2.x64.exe

*/
!define RELEASURL	"https://vivaldi.com/download/snapshot"
!define APPSIZE	"130000" # kB
!define DLVER	"snapshot"
!define APPVER 	"0.0.0.0"
!define APPNAME "Vivaldi"
!define APP 	"Vivaldi"
!define DLNAME	"Vivaldi"
!define APPLANG	"32-64-bit_Multilingual_Online"
!define FOLDER	"VivaldiPortable"
!define FINISHRUN ; Delete if not Finish pages
!define OPTIONS ; Delete if no Components
!define SOURCES ; Delete if no Sources
; !define DESCRIPTION	"A new browser for our friends" ; Delete if no AppInfo
!define INPUTBOX ; Delete if no InputBox

SetCompressor /SOLID lzma
SetCompressorDictSize 32

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

!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "Arabic"
!insertmacro MUI_LANGUAGE "Bulgarian"
!insertmacro MUI_LANGUAGE "Catalan"
!insertmacro MUI_LANGUAGE "Czech"
!insertmacro MUI_LANGUAGE "Danish"
!insertmacro MUI_LANGUAGE "German"
!insertmacro MUI_LANGUAGE "Greek"
!insertmacro MUI_LANGUAGE "SpanishInternational"
!insertmacro MUI_LANGUAGE "Spanish"
!insertmacro MUI_LANGUAGE "Estonian"
!insertmacro MUI_LANGUAGE "Farsi"
!insertmacro MUI_LANGUAGE "Finnish"
!insertmacro MUI_LANGUAGE "French"
!insertmacro MUI_LANGUAGE "Hebrew"
!insertmacro MUI_LANGUAGE "Croatian"
!insertmacro MUI_LANGUAGE "Hungarian"
!insertmacro MUI_LANGUAGE "Indonesian"
!insertmacro MUI_LANGUAGE "Italian"
!insertmacro MUI_LANGUAGE "Japanese"
!insertmacro MUI_LANGUAGE "Korean"
!insertmacro MUI_LANGUAGE "Lithuanian"
!insertmacro MUI_LANGUAGE "Latvian"
!insertmacro MUI_LANGUAGE "Malay"
!insertmacro MUI_LANGUAGE "Dutch"
!insertmacro MUI_LANGUAGE "Polish"
!insertmacro MUI_LANGUAGE "PortugueseBR"
!insertmacro MUI_LANGUAGE "Portuguese"
!insertmacro MUI_LANGUAGE "Romanian"
!insertmacro MUI_LANGUAGE "Russian"
!insertmacro MUI_LANGUAGE "Slovak"
!insertmacro MUI_LANGUAGE "Slovenian"
!insertmacro MUI_LANGUAGE "Serbian"
!insertmacro MUI_LANGUAGE "Swedish"
!insertmacro MUI_LANGUAGE "Thai"
!insertmacro MUI_LANGUAGE "Turkish"
!insertmacro MUI_LANGUAGE "Ukrainian"
!insertmacro MUI_LANGUAGE "SimpChinese"
!insertmacro MUI_LANGUAGE "TradChinese"

Var InputVer
Var VER
Function nsDialogsPage
	nsDialogs::Create 1018
	Pop $0
	${NSD_CreateLabel} 0 0 100% 12u "Enter Version Number:"
	Pop $0
	${NSD_CreateText} 0 13u 100% 12u ""
	Pop $InputVer
	nsDialogs::Show
FunctionEnd
Function nsDialogsPageLeave
	${NSD_GetText} $InputVer $R0
StrCmp $R0 "" 0 +3
	MessageBox MB_ICONEXCLAMATION `You must enter a version number!`
Abort
	StrCpy $VER "$R0"
FunctionEnd

Section "${APPNAME} Portable 32 bit" main

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

DetailPrint "Installing ${APPNAME} Portable 32 bit"

${If} ${FileExists} "$EXEDIR\${APP}.$VER.exe"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$EXEDIR\${APP}.$VER.exe" -aoa -o"$TEMP\${APP}PortableTemp"`
${Else}
Call CheckConnected
	inetc::get "${RELEASURL}/${APP}.$VER.exe" "$TEMP\${APP}PortableTemp\${APP}.$VER.exe" /END
	Pop $0
StrCmp $0 "OK" +3
	MessageBox MB_ICONEXCLAMATION "${APP}.$VER.exe not found in $EXEDIR and download: $0"
	Abort
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\${APP}.$VER.exe" -aoa -o"$TEMP\${APP}PortableTemp"`
${EndIf}
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\vivaldi.7z" -o"$TEMP\${APP}PortableTemp"`

DetailPrint "Installing ${APPNAME} Portable 32 bit"

	SetOutPath "$INSTDIR"
		File "..\..\..\${FOLDER}\${APP}Portable.exe"

	SetOutPath "$INSTDIR\App\DefaultData\${APP}\Default"
		File  /r "Default\*.*"
	SetOutPath "$INSTDIR\App\${APP}"
	FileOpen $0 "$INSTDIR\App\${APP}\First Run" w
	FileClose $0
	CopyFiles /SILENT /FILESONLY "$TEMP\${APP}PortableTemp\Vivaldi-bin\*.*" "$INSTDIR\App\${APP}"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\Vivaldi-bin\$VER\*.*" "$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\Default"
	File /r "Default\*.*"
!endif
!ifdef SOURCES & DESCRIPTION
Call SourceInfo
!endif

SectionEnd

Section "${APPNAME} Portable 64 bit" x64

	RMDir "/r" "$TEMP\${APP}PortableTemp"
	CreateDirectory "$TEMP\${APP}PortableTemp"

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

DetailPrint "Installing ${APPNAME} Portable 64 bit"

${If} ${FileExists} "$EXEDIR\${APP}.$VER.x64.exe"
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$EXEDIR\${APP}.$VER.x64.exe" -aoa -o"$TEMP\${APP}PortableTemp"`
${Else}
Call CheckConnected
	inetc::get "${RELEASURL}/${APP}.$VER.x64.exe" "$TEMP\${APP}PortableTemp\${APP}.$VER.x64.exe" /END
	Pop $0
StrCmp $0 "OK" +3
	MessageBox MB_ICONEXCLAMATION "${APP}.$VER.x64.exe not found in $EXEDIR and download: $0"
	Abort
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\${APP}.$VER.x64.exe" -aoa -o"$TEMP\${APP}PortableTemp"`
${EndIf}
	nsExec::Exec `"$TEMP\${APP}PortableTemp\7z.exe" x "$TEMP\${APP}PortableTemp\vivaldi.7z" -o"$TEMP\${APP}PortableTemp"`

DetailPrint "Installing ${APPNAME} Portable 64 bit"

	SetOutPath "$INSTDIR"
		File "..\..\..\${FOLDER}\${APP}Portable.exe"

	SetOutPath "$INSTDIR\App\DefaultData\${APP}\Default"
		File  /r "Default\*.*"
	SetOutPath "$INSTDIR\App\${APP}64"
	FileOpen $0 "$INSTDIR\App\${APP}64\First Run" w
	FileClose $0
	CopyFiles /SILENT /FILESONLY "$TEMP\${APP}PortableTemp\Vivaldi-bin\*.*" "$INSTDIR\App\${APP}64"
	CopyFiles /SILENT "$TEMP\${APP}PortableTemp\Vivaldi-bin\$VER\*.*" "$INSTDIR\App\${APP}64"

!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\Default"
	File /r "Default\*.*"
!endif
!ifdef SOURCES & DESCRIPTION
Call SourceInfo
!endif

SectionEnd

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

Function Init
SectionSetSize ${x64} 157000 # kB
${If} ${RunningX64}
SectionSetFlags ${x64} 1
${Else}
${EndIf}
FunctionEnd
