${SegmentFile}
!ifdef Version
 !undef Version
!endif
!define Version 9.16.0.0

!addplugindir "$EXEDIR\Other\Source\Plugin"

!define APP_NAME "SoftOrganizer"	
!define APP_NAME64 "SoftOrganizer"
!define APP_DIR "$EXEDIR\App\SoftOrganizer"
!define APP_DIR64 "$EXEDIR\App\SoftOrganizer64"
!define APP_BASE "SoftOrganizer"
!define APP_BASE64 "SoftOrganizer64"
!define APP_MINOS "7"
!include x64.nsh

!define STOP::_PRO `!insertmacro STOP::_PRO`
!macro STOP::_PRO _PRONAME
      ${If} ${ProcessExists} "${_PRONAME}.exe"
	      ${If} ${_PRONAME} == "RegOrganizer"	     
		     MessageBox MB_OK|MB_SETFOREGROUND|MB_ICONEXCLAMATION 'The program $\"${_PRONAME}$\" is already running.$\nneeds to be closed.' 
		     Abort
	      ${Else}
		     MessageBox MB_YESNO|MB_SETFOREGROUND|MB_ICONEXCLAMATION 'The program $\"${_PRONAME}$\" is already running.$\nneeds to be closed.$\nclose it now? ' IDYES +2 IDNO
		     Abort
		     nsExec::ExecToStack `"$SYSDIR\taskkill.exe" /IM "${_PRONAME}.exe" /T /F`
		     Sleep 500
	      ${EndIf} 			
      ${EndIf}
!macroend
!define File::Write `!insertmacro File::_Write`
!macro File::_Write _ININAME _NO _TypeVALUE _FileVALUE _FindVALUE _ReplaceVALUE 
       WriteINIStr "${_ININAME}" "FileWrite${_NO}" "Type" "${_TypeVALUE}"		
       WriteINIStr "${_ININAME}" "FileWrite${_NO}" "File" "${_FileVALUE}"
       WriteINIStr "${_ININAME}" "FileWrite${_NO}" "Find" "${_FindVALUE}"
       WriteINIStr "${_ININAME}" "FileWrite${_NO}" "Replace" "${_ReplaceVALUE}"
       WriteINIStr "${_ININAME}" "FileWrite${_NO}" "B" "B$\n"
       DeleteINIStr "${_ININAME}" "FileWrite${_NO}" "B"       
!macroend

${Segment.onInit}

      ${IfNotThen} ${FileExists} "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini" ${|} Call _CREATDEFINI  ${|} 

      ${If} ${RunningX64} ;if just use 32 bit or 64 bit potplayer
         ${IfNotThen} ${FileExists} "${APP_DIR64}" ${|} CreateDirectory "${APP_DIR64}" ${|} ;tell user copy " 64 bit (files)" to "APP_DIR64" folder
         DeleteINIStr "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini" Launch ProgramExecutable 
      ${Else}
         ${IfNotThen} ${FileExists} "${APP_DIR}" ${|} CreateDirectory "${APP_DIR}" ${|} ;tell user copy " 32 bit (files)" to "APP_DIR" folder	 
         DeleteINIStr "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini" Launch ProgramExecutable64 
      ${EndIf}
      StrCpy $0 "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini"
      ${IfThen} ${RunningX64} ${|} WriteINIStr "$0" Launch ProgramExecutable64 "${APP_BASE64}\${APP_NAME64}.exe" ${|}
      ${IfNotThen} ${RunningX64} ${|} WriteINIStr "$0" Launch ProgramExecutable "${APP_BASE}\${APP_NAME}.exe" ${|}
      
      WriteINIStr "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini" FileWrite1 "File" "%PAL:DataDir%\settings\lrepacks.net.reg"	
      WriteINIStr "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini" FileWrite2 "File" "%PAL:DataDir%\settings\lrepacks.net.reg"
      WriteINIStr "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini" FileWrite3 "File" "%PAL:DataDir%\settings\lrepacks.net.reg"
            
!macroend      
${SegmentPrePrimary}
      Call _STOP_PRO
      Call REG_DLL
!macroend

${SegmentPreExec}  
      ${IfThen} ${RunningX64} ${|} ${DisableX64FSRedirection} ${|}	
      StrCpy $R0 ""
      ${IfThen} ${RunningX64} ${|} StrCpy $R0 "/reg:64" ${|}
      nsExec::ExecToStack '"$SYSDIR\reg.exe" Delete "HKCU\Software\ChemTable Software\Full Uninstall\Settings" /v LatestVersionRanEx /f $R0'
      ;nsExec::ExecToStack '"$SYSDIR\reg.exe" Delete "HKCU\Software\ChemTable Software\Full Uninstall\Settings\ShellContextMenuDll" /f $R0'  
      ${IfThen} ${RunningX64} ${|} ${EnableX64FSRedirection} ${|}
!macroend
${SegmentPostPrimary}
         
      Call UNREG_DLL

      ${IfThen} ${RunningX64} ${|} ${DisableX64FSRedirection} ${|}
      StrCpy $R0 ""
      ${IfThen} ${RunningX64} ${|} StrCpy $R0 "/reg:64" ${|}
      nsExec::ExecToStack '"$SYSDIR\reg.exe" Delete "HKCU\Software\Classes\CLSID\{EF706AB3-1E0E-4C5B-A40F-023F0FA36E12}" /f $R0'
      nsExec::ExecToStack '"$SYSDIR\reg.exe" Delete "HKCU\Software\Microsoft\Windows\CurrentVersion\PushNotifications\Backup\ChemTableSoftware.Soft_Organizer.SoftOrganizer.exe" /f $R0'	  
      nsExec::ExecToStack '"$SYSDIR\reg.exe" Delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings\ChemTableSoftware.Soft_Organizer.SoftOrganizer.exe" /f $R0'	  
      DeleteRegKey /ifempty HKCU "HKCU\Software\ChemTable Software\Full Uninstall"
      DeleteRegKey /ifempty HKCU "HKCU\Software\ChemTable Software"
      DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FeatureUsage\AppBadgeUpdated" "${APP_DIR}\${APP_NAME}.exe"
      DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FeatureUsage\AppBadgeUpdated" "${APP_DIR64}\${APP_NAME64}.exe"
      DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FeatureUsage\AppSwitched" "${APP_DIR}\${APP_NAME}.exe"
      DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FeatureUsage\AppSwitched" "${APP_DIR64}\${APP_NAME64}.exe"
      DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FeatureUsage\AppSwitched" "ChemTableSoftware.Soft_Organizer.SoftOrganizer.exe"      
      ${IfThen} ${RunningX64} ${|} ${EnableX64FSRedirection} ${|}
!macroend

;==========================================Function=============================================;
Function _STOP_PRO
      ${STOP::_PRO} RegOrganizer
      ${STOP::_PRO} StartupCheckingService
      ${STOP::_PRO} Updater
	  ${STOP::_PRO} SoftOrganizer
FunctionEnd

Function _CREATDEFINI
      StrCpy $0 "$EXEDIR\App\AppInfo\Launcher\${AppID}.ini"
      WriteINIStr "$0" Launch ProgramExecutable "${APP_BASE}\${APP_NAME}.exe"
      WriteINIStr "$0" Launch ProgramExecutable64 "${APP_BASE64}\${APP_NAME64}.exe"
      WriteINIStr "$0" Launch  MinOS "${APP_MINOS}"
      WriteINIStr "$0" Launch RunAsAdmin "force"
      WriteINIStr "$0" Launch SinglePortableAppInstance "true" 
      WriteINIStr "$0" Launch DirectoryMoveOK "yes"
      ;WriteINIStr "$0" Launch SupportsUNC "yes" 
      WriteINIStr "$0" Launch "B" "B$\n"
      DeleteINIStr "$0" Launch "B"  
      WriteINIStr "$0" Activate Registry "true"	
      WriteINIStr "$0" Activate "B" "B$\n"
      DeleteINIStr "$0" Activate "B"      
      WriteINIStr "$0" RegistryKeys "lrepacks.net" "$\"HKCU\Software\ChemTable Software\Full Uninstall$\""
      ;WriteINIStr "$0" RegistryKeys "-" "$\"HKCU\Software\ChemTable Software\Full Uninstall$\""
      WriteINIStr "$0" RegistryKeys "B" "B$\n"
      DeleteINIStr "$0" RegistryKeys "B"
      StrCpy $1 "HKCU\Software\ChemTable Software\Full Uninstall\Settings"
      WriteINIStr "$0" RegistryValueWrite "$1\CheckForUpdates" "REG_DWORD:0"
      WriteINIStr "$0" RegistryValueWrite "$1\RepairInvalidShortcuts" "REG_DWORD:0" 
      WriteINIStr "$0" RegistryValueWrite "$1\ShowSplash" "REG_DWORD:0"
      WriteINIStr "$0" RegistryValueWrite "$1\ClassicRegCleanup" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\OnlyImportantUpdates" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\SkipBetas" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\RegOptimization" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\DarkTheme" "REG_DWORD:1"
      ;WriteINIStr "$0" RegistryValueWrite "$1\SubReminderEx" "REG_BINARY:5130ed6f6e98e5401"
      WriteINIStr "$0" RegistryValueWrite "$1\DidYouKnow\1no" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\DidYouKnow\2no" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\DidYouKnow\5no" "REG_DWORD:1"
      WriteINIStr "$0" RegistryValueWrite "$1\DidYouKnow\6no" "REG_DWORD:1"	
      WriteINIStr "$0" RegistryValueWrite "$1\DidYouKnow\230320date" "REG_BINARY:8e14df0551c7e540"
      WriteINIStr "$0" RegistryValueWrite "$1\DidYouKnow\230320no" "REG_DWORD:1"  
      WriteINIStr "$0" RegistryValueWrite "$1\ShellContextMenuDll\RunAndTraceText" "REG_SZ:Run and trace with %s"
      WriteINIStr "$0" RegistryValueWrite "$1\ShellContextMenuDll\CompleteUninstallTextEx" "REG_SZ:Complete Uninstall"	
      WriteINIStr "$0" RegistryValueWrite "$1\ShellContextMenuDll\OpenWithText" "REG_SZ:$\"$\""
      WriteINIStr "$0" RegistryValueWrite "$1\ShellContextMenuDll\CompleteUninstallText" "REG_SZ:Complete Uninstall with %s"	  
      WriteINIStr "$0" RegistryValueWrite "B" "B$\n"
      DeleteINIStr "$0" RegistryValueWrite "B"	
      WriteINIStr "$0" RegistryCleanupIfEmpty "1" "$\"HKCU\Software\ChemTable Software$\""
      WriteINIStr "$0" RegistryCleanupIfEmpty "B" "B$\n"
      DeleteINIStr "$0" RegistryCleanupIfEmpty "B"	      
      WriteINIStr "$0" RegistryValueBackupDelete "B" "B$\n"
      DeleteINIStr "$0" RegistryValueBackupDelete "B"      
      WriteINIStr "$0" DirectoriesCleanupIfEmpty "1" "$\"%APPDATA%\ChemTable Software$\""       
      WriteINIStr "$0" DirectoriesCleanupIfEmpty "2" "$\"%LOCALAPPDATA%\ChemTable Software$\""        
      WriteINIStr "$0" DirectoriesCleanupIfEmpty "B" "B$\n"
      DeleteINIStr "$0" DirectoriesCleanupIfEmpty "B"      
      ;WriteINIStr "$0" FilesMove "B" "B$\n"
      ;DeleteINIStr "$0" FilesMove "B"	      
      WriteINIStr "$0" DirectoriesMove "APPDATA\Full Uninstall" "$\"%APPDATA%\ChemTable Software\Full Uninstall$\""
      WriteINIStr "$0" DirectoriesMove "LOCALAPPDATA\Reg Organizer" "$\"%LOCALAPPDATA%\ChemTable Software\Full Uninstall$\""
      WriteINIStr "$0" DirectoriesMove "LOCALAPPDATA\Shared" "$\"%LOCALAPPDATA%\ChemTable Software\Shared$\""
      WriteINIStr "$0" DirectoriesMove "LOCALAPPDATA\InstallerTracing" "$\"%LOCALAPPDATA%\ChemTable Software\InstallerTracing$\""      
      WriteINIStr "$0" DirectoriesMove "B" "B$\n"
      DeleteINIStr "$0" DirectoriesMove "B"      
      ${File::Write} "$0" "1" "Replace" "" "%PAL:LastDrive%%PAL:LastPackagePartialDir%\" "%PAL:Drive%%PAL:PackagePartialDir%\"	      
      ${File::Write} "$0" "2" "Replace" "" "%PAL:LastPortableAppsBaseDir%\" "%PAL:PortableAppsBaseDir%\"
      ${File::Write} "$0" "3" "Replace" "" "%PAL:LastDrive%\" "%PAL:Drive%\"  
FunctionEnd

Function REG_DLL
     ${IfThen} ${RunningX64} ${|} ${DisableX64FSRedirection} ${|}
     nsExec::ExecToStack '"$SYSDIR\SCHTASKS.exe" /End /TN "Soft Organizer Applications Updates ChecK"'
     nsExec::ExecToStack '"$SYSDIR\SCHTASKS.exe" /Delete /TN "Soft Organizer Applications Updates ChecK" /F'
     ${If} ${RunningX64}
         ${If} ${FileExists} "${APP_DIR64}\Shell\ShellContextMenu_4.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR64}\Shell\ShellContextMenu_4.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR64}\ShellContextMenu_4.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR64}\ShellContextMenu_4.dll"'			 
         ${ElseIf} ${FileExists} "${APP_DIR64}\ShellContextMenu_3.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR64}\ShellContextMenu_3.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR64}\ShellContextMenu_1.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR64}\ShellContextMenu_1.dll"'         
		 ${EndIf}
     ${Else} 
         ${If} ${FileExists} "${APP_DIR}\Shell\ShellContextMenu_4.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR}\Shell\ShellContextMenu_4.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR}\ShellContextMenu_4.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR}\ShellContextMenu_4.dll"'			 
         ${ElseIf} ${FileExists} "${APP_DIR}\ShellContextMenu_3.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR}\ShellContextMenu_3.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR}\ShellContextMenu_1.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR}\ShellContextMenu_1.dll"'
         ${EndIf}
     ${EndIf}	 
     ${IfThen} ${RunningX64} ${|} ${EnableX64FSRedirection} ${|}
FunctionEnd	 

Function UNREG_DLL
     ${IfThen} ${RunningX64} ${|} ${DisableX64FSRedirection} ${|}
     nsExec::ExecToStack '"$SYSDIR\SCHTASKS.exe" /End /TN "Soft Organizer Applications Updates ChecK"'
     nsExec::ExecToStack '"$SYSDIR\SCHTASKS.exe" /Delete /TN "Soft Organizer Applications Updates ChecK" /F'
     ${If} ${RunningX64}
         ${If} ${FileExists} "${APP_DIR64}\Shell\ShellContextMenu_4.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR64}\Shell\ShellContextMenu_4.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR64}\ShellContextMenu_4.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR64}\ShellContextMenu_4.dll"'			 
         ${ElseIf} ${FileExists} "${APP_DIR64}\ShellContextMenu_3.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR64}\ShellContextMenu_3.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR64}\ShellContextMenu_1.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /s "${APP_DIR64}\ShellContextMenu_1.dll"'
         ${EndIf}
     ${Else} 
         ${If} ${FileExists} "${APP_DIR}\Shell\ShellContextMenu_4.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR}\Shell\ShellContextMenu_4.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR}\ShellContextMenu_4.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR}\ShellContextMenu_4.dll"'			 
         ${ElseIf} ${FileExists} "${APP_DIR}\ShellContextMenu_3.dll"			 
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR}\ShellContextMenu_3.dll"'
         ${ElseIf} ${FileExists} "${APP_DIR}\ShellContextMenu_1.dll"
             nsExec::ExecToStack '"$SYSDIR\regsvr32.exe" /u /s "${APP_DIR}\ShellContextMenu_1.dll"'
         ${EndIf}
     ${EndIf}	 
     ${IfThen} ${RunningX64} ${|} ${EnableX64FSRedirection} ${|}
FunctionEnd	
	 