!define NAME "PortableNVU"
!define VER "0.0.3"

!packhdr "C:\tmp.dat" "c:\Program Files\UPX\upx.exe --best --crp-ms=999999 C:\tmp.dat"

;=== Out program information
Name "${NAME}"
OutFile "${NAME}.exe"
Caption "${NAME} - Making NVU Portable"

CRCCheck On
WindowIcon Off
SilentInstall Silent
AutoCloseWindow True

;=== Set program icon
Icon "nvu.ico"

;=== Path variables
Var NVUPATH
Var PROFILEDIRECTORY

Section "Main"
	
	;=== Get the path information from the INI file
	IfFileExists "$EXEDIR\${NAME}.ini" ReadINI NoINI
	ReadINI:
		ReadINIStr $0 "$EXEDIR\${NAME}.ini" "${NAME}" "NVUPATH"
		StrCpy "$NVUPATH" $0
		ReadINIStr $0 "$EXEDIR\${NAME}.ini" "${NAME}" "PROFILEDIRECTORY"
		StrCpy "$PROFILEDIRECTORY" $0
		IfErrors NoINI EndINI
	NoINI:
		MessageBox MB_OK "Unable to find configuration file $\"$EXEDIR\${NAME}.ini$\". Click OK to exit." IDOK
		Goto TheEnd
	EndINI:

	IfFileExists "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf" ChromeFound RunNVU
	ChromeFound:
	FileOpen $0 "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf" r
	FileOpen $R0 "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf.new" w
	ClearErrors ; if there's an error, we're done with the file
	NextLine:
		FileWrite $R0 $4
		FileRead $0 $4
		IfErrors NoMoreLines
		StrCpy $5 $4 39
		;MessageBox MB_OK "$5"
		StrCmp $5 `                   c:baseURL="jar:file:` "" NextLine
		StrCpy $R4 40
	NotYet:
		IntOp $R4 $R4 + 1
		StrCpy $7 $4 10 $R4 ; looking for the point to strip the extension path
		;MessageBox MB_OK "$7"
		StrCmp $7 "extensions" PathFound NotYet
	PathFound:
		StrCpy $5 $4 "" $R4
		;MessageBox MB_OK "The following extension has an absolute path.$\n$\n$5$\n$\nWe'll make it relative, because that's our purpose :)"
		StrCpy $4 `                   c:baseURL="jar:resource:/$PROFILEDIRECTORY/$5` ; the path made relative
		GoTo NextLine
	NoMoreLines:
	FileClose $0
	FileClose $R0
	CopyFiles "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf" "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf.old" ; rename it just in case
	CopyFiles "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf.new" "$NVUPATH\$PROFILEDIRECTORY\chrome\chrome.rdf" ; that was our purpose

	RunNVU:
	Exec "$NVUPATH\NVU.exe -profile $NVUPATH\$PROFILEDIRECTORY\"

	TheEnd:
SectionEnd