;Copyright 2004-2024 John T. Haller of PortableApps.com

;Website: https://portableapps.com/apps/utilities/hwinfo-portable

;This software is OSI Certified Open Source Software.
;OSI Certified is a certification mark of the Open Source Initiative.

;This program is free software; you can redistribute it and/or
;modify it under the terms of the GNU General Public License
;as published by the Free Software Foundation; either version 2
;of the License, or (at your option) any later version.

;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.

;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

!define NAME "KchmViewerPathAdjuster"
!define FULLNAME "KchmViewer Path Adjuster"
!define APP "KchmViewer"
!define VER "1.0.0.0"
!define WEBSITE "https://portableapps.com/apps/office/kchmviewer-portable"

;=== Program Details
Name "${FULLNAME}"
OutFile "..\..\App\Bin\${NAME}.exe"
Caption "${FULLNAME} | PortableApps.com"
VIProductVersion "${VER}"
VIAddVersionKey ProductName "${FULLNAME}"
VIAddVersionKey Comments "Allows ${APP} to be run from a removable drive.  For additional details, visit ${WEBSITE}"
VIAddVersionKey CompanyName "PortableApps.com"
VIAddVersionKey LegalCopyright "John T. Haller"
VIAddVersionKey FileDescription "${FULLNAME}"
VIAddVersionKey FileVersion "${VER}"
VIAddVersionKey ProductVersion "${VER}"
VIAddVersionKey InternalName "${FULLNAME}"
VIAddVersionKey LegalTrademarks "PortableApps.com is a trademark of Rare Ideas, LLC."
VIAddVersionKey OriginalFilename "${NAME}.exe"
;VIAddVersionKey PrivateBuild ""
;VIAddVersionKey SpecialBuild ""

;=== Runtime Switches
CRCCheck On
WindowIcon Off
SilentInstall Silent
AutoCloseWindow True
RequestExecutionLevel user
XPStyle on
Unicode true
ManifestDPIAware true
SetCompress Auto
SetCompressor /SOLID lzma
SetCompressorDictSize 32
SetDatablockOptimize On

;=== Include
;(Standard NSIS)
!include LogicLib.nsh
!include Registry.nsh
!include WordFunc.nsh

!define HKEY_CLASSES_ROOT        0x80000000
!define HKEY_CURRENT_USER        0x80000001
!define HKEY_LOCAL_MACHINE       0x80000002
!define HKEY_USERS               0x80000003
!define HKEY_PERFORMANCE_DATA    0x80000004
!define HKEY_PERFORMANCE_TEXT    0x80000050
!define HKEY_PERFORMANCE_NLSTEXT 0x80000060
!define HKEY_CURRENT_CONFIG      0x80000005
!define HKEY_DYN_DATA            0x80000006
!define KEY_QUERY_VALUE          0x0001
!define KEY_ENUMERATE_SUB_KEYS   0x0008
!define REG_NONE                 0
!define REG_SZ                   1
!define REG_EXPAND_SZ            2
!define REG_BINARY               3
!define REG_DWORD                4
!define REG_DWORD_LITTLE_ENDIAN  4
!define REG_DWORD_BIG_ENDIAN     5
!define REG_LINK                 6
!define REG_MULTI_SZ             7

!define RegOpenKeyEx     "Advapi32::RegOpenKeyEx(p, t, i, i, *p) i"
!define RegQueryValueEx  "Advapi32::RegQueryValueEx(p, t, p, *i, p, *i) i"
!define RegCloseKey      "Advapi32::RegCloseKeyA(p) i"

;=== Program Icon
Icon "..\..\App\AppInfo\appicon.ico"

Section "Main"
	;Code with help from Bart.S, OpenPlatform, and 3D1T0R
	
	StrCpy $1 ""
	StrCpy $2 ""
	StrCpy $3 ""
	StrCpy $9 ""
	System::Call "${RegOpenKeyEx}(${HKEY_CURRENT_USER}, 'Software\Ulduzsoft\kchmviewer', 0, ${KEY_QUERY_VALUE}|${KEY_ENUMERATE_SUB_KEYS}, 0 r0) .r3"
 
	StrCmp $3 0 CustomContinue
		Goto CustomLoopDone
CustomContinue:

  System::Call "${RegQueryValueEx}(r0, 'recentFileList', 0, .r1, 0, .r2) .r3"
 
  StrCmp $3 0 CustomRead
    Goto CustomLoopDone
 
CustomRead:
 
  StrCmp $1 ${REG_MULTI_SZ} CustomMultiSZ
    Goto CustomLoopDone
 
CustomMultiSZ:
 
  StrCmp $2 0 0 CustomMultiSZalloc
    Goto CustomLoopDone
 
CustomMultiSZalloc:

  System::Alloc $2
  Pop $1
 
  StrCmp $1 0 0 CustomMultiSZget
    Goto CustomLoopDone
 
CustomMultiSZget:
 
  System::Call "${RegQueryValueEx}(r0, 'recentFileList', 0, n, r1, r2) .r3"
 
  StrCmp $3 0 CustomMultiSZprocess
    Goto CustomLoopDone
 
CustomMultiSZprocess:
 
  StrCpy $4 $1
 
  CustomLoop:
 
    System::Call "*$4(&t${NSIS_MAX_STRLEN} .r3)"
    StrCmp $3 "" CustomLoopDone
    ;DetailPrint $3
	${If} $9 == ""
		StrCpy $9 "$3"
	${Else}
		StrCpy $9 "$9$\n$3"
	${EndIf}
    StrLen $5 $3
    !if "${NSIS_CHAR_SIZE}" > 1
    IntOp $5 $5 * ${NSIS_CHAR_SIZE}
    !endif
    IntPtrOp $4 $4 + $5
    IntPtrOp $4 $4 + ${NSIS_CHAR_SIZE}
    Goto CustomLoop
 
CustomLoopDone:
 
  System::Free $1
 
  StrCmp $0 0 CustomNoClose
    System::Call "${RegCloseKey}(r0)"
 
CustomNoClose:
	
	
	
	
	StrCpy $R0 $9
	;MessageBox MB_OK $R0
	StrLen $R2 $R0
	${If} $R2 > 3 ;Ensure a populated REG_MULTI_SZ
		;Replace newline between characters while preserving actual new lines
		;${WordReplace} $R0 "$\n$\n$\n" "/NEWLINE\" "+" $R2
		;${WordReplace} $R2 "$\n" "" "+" $R2
		;${WordReplace} $R2 "/NEWLINE\" "$\n" "+" $R2

		ExpandEnvStrings $4 "%PAL:LastDrive%"
		ExpandEnvStrings $5 "%PAL:Drive%"
		ExpandEnvStrings $2 "%PAL:LastPortableAppsBaseDir:ForwardSlash%"
		ExpandEnvStrings $3 "%PAL:PortableAppsBaseDir:ForwardSlash%"
		ExpandEnvStrings $6 "%PAL:LastDrive%%PAL:LastPackagePartialDir:ForwardSlash%"
		ExpandEnvStrings $7 "%PAL:Drive%%PAL:PackagePartialDir:ForwardSlash%"
		
		${If} $6 != $7
		
			${WordReplace} $R0 "$6/" "$7/" "+" $R0
			${WordReplace} $R0 "$2/" "$3/" "+" $R0
			${WordReplace} $R0 "$4/" "$5/" "+" $R0
			
			;MessageBox MB_OK $R0
			
			${registry::Write} "HKCU\Software\Ulduzsoft\kchmviewer" "recentFileList" $R0 "REG_MULTI_SZ" $R9
		${EndIf}
	${EndIf}
SectionEnd