From ab8035806c7d87e9106a2583be8b7930f97b41c6 Mon Sep 17 00:00:00 2001
From: Dominik Daehn <isnoguter@live.de>
Date: Fri, 18 Oct 2019 10:03:51 +0200
Subject: [PATCH] add ProductProperty DesktopLink, CustomPostInstall,
 CustomPostUninstall

---
 sample/All/CLIENT_DATA/delsub32.opsiscript   | 18 +++++++
 sample/All/CLIENT_DATA/delsub3264.opsiscript | 18 +++++++
 sample/All/CLIENT_DATA/delsub64.opsiscript   | 18 +++++++
 sample/All/CLIENT_DATA/setup32.opsiscript    | 38 +++++++++++++
 sample/All/CLIENT_DATA/setup3264.opsiscript  | 57 ++++++++++++++++++++
 sample/All/CLIENT_DATA/setup64.opsiscript    | 38 +++++++++++++
 sample/All/OPSI/postinst                     | 16 ++++++
 sample/All/OPSI/preinst                      | 24 +++++++++
 8 files changed, 227 insertions(+)

diff --git a/sample/All/CLIENT_DATA/delsub32.opsiscript b/sample/All/CLIENT_DATA/delsub32.opsiscript
index 8dd53e4..a307734 100644
--- a/sample/All/CLIENT_DATA/delsub32.opsiscript
+++ b/sample/All/CLIENT_DATA/delsub32.opsiscript
@@ -81,6 +81,17 @@ LinkFolder_uninstall
 comment "File Type Association"
 DosInAnIcon_FileTypeAssoc_uninstall
 
+DefVar $Property_CustomPostUninstall$
+Set $Property_CustomPostUninstall$ = getProductProperty("custom-post-uninstall","none")
+if not ($Property_CustomPostUninstall$ = "none")
+	comment "Include custom post uninstall file"
+	if FileExists("%ScriptPath%\custom\" + $Property_CustomPostUninstall$)
+		sub_CustomPostUninstall
+	else
+		LogError "Include script NOT exists (" + $Property_CustomPostUninstall$ +")"
+	endif
+endif
+
 if $Reboot$="1"
 	ExitWindows /ImmediateReboot
 endif
@@ -124,6 +135,10 @@ msiexec /x $MsiId$ /qb-! REBOOT=ReallySuppress
 ; deletekey [HKEY_LOCAL_MACHINE\Software\$ProductId$]
 
 [LinkFolder_uninstall]
+set_basefolder common_desktopdirectory
+set_subfolder ""
+delete_element $NAME$
+
 ; Example of deleting a folder from AllUsers startmenu:
 ;
 ; set_basefolder common_programs
@@ -146,6 +161,9 @@ msiexec /x $MsiId$ /qb-! REBOOT=ReallySuppress
 ;
 ; SFTA --unreg Applications\SumatraPDF.exe .pdf
 
+[sub_CustomPostUninstall]
+include_insert "%ScriptPath%\custom\" + $Property_CustomPostUninstall$
+
 [Sub_check_exitcode]
 comment "Test for installation success via exit code"
 set $ExitCode$ = getLastExitCode
diff --git a/sample/All/CLIENT_DATA/delsub3264.opsiscript b/sample/All/CLIENT_DATA/delsub3264.opsiscript
index c90e26c..eb8f6c7 100644
--- a/sample/All/CLIENT_DATA/delsub3264.opsiscript
+++ b/sample/All/CLIENT_DATA/delsub3264.opsiscript
@@ -152,6 +152,17 @@ endif
 comment "Delete program shortcuts"
 LinkFolder_uninstall
 
+DefVar $Property_CustomPostUninstall$
+Set $Property_CustomPostUninstall$ = getProductProperty("custom-post-uninstall","none")
+if not ($Property_CustomPostUninstall$ = "none")
+	comment "Include custom post uninstall file"
+	if FileExists("%ScriptPath%\custom\" + $Property_CustomPostUninstall$)
+		sub_CustomPostUninstall
+	else
+		LogError "Include script NOT exists (" + $Property_CustomPostUninstall$ +")"
+	endif
+endif
+
 if $Reboot$="1"
 	ExitWindows /ImmediateReboot
 endif
@@ -229,6 +240,10 @@ msiexec /x $MsiId64$ /qb-! REBOOT=ReallySuppress
 ; deletekey [HKEY_LOCAL_MACHINE\Software\$ProductId$]
 
 [LinkFolder_uninstall]
+set_basefolder common_desktopdirectory
+set_subfolder ""
+delete_element $NAME$
+
 ; Example of deleting a folder from AllUsers startmenu:
 ;
 ; set_basefolder common_programs
@@ -251,6 +266,9 @@ msiexec /x $MsiId64$ /qb-! REBOOT=ReallySuppress
 ;
 ; SFTA --unreg Applications\SumatraPDF.exe .pdf
 
+[sub_CustomPostUninstall]
+include_insert "%ScriptPath%\custom\" + $Property_CustomPostUninstall$
+
 [Sub_check_exitcode]
 comment "Test for installation success via exit code"
 set $ExitCode$ = getLastExitCode
diff --git a/sample/All/CLIENT_DATA/delsub64.opsiscript b/sample/All/CLIENT_DATA/delsub64.opsiscript
index 661159a..7f70bce 100644
--- a/sample/All/CLIENT_DATA/delsub64.opsiscript
+++ b/sample/All/CLIENT_DATA/delsub64.opsiscript
@@ -81,6 +81,17 @@ LinkFolder_uninstall
 comment "File Type Association"
 DosInAnIcon_FileTypeAssoc_uninstall
 
+DefVar $Property_CustomPostUninstall$
+Set $Property_CustomPostUninstall$ = getProductProperty("custom-post-uninstall","none")
+if not ($Property_CustomPostUninstall$ = "none")
+	comment "Include custom post uninstall file"
+	if FileExists("%ScriptPath%\custom\" + $Property_CustomPostUninstall$)
+		sub_CustomPostUninstall
+	else
+		LogError "Include script NOT exists (" + $Property_CustomPostUninstall$ +")"
+	endif
+endif
+
 if $Reboot$="1"
 	ExitWindows /ImmediateReboot
 endif
@@ -124,6 +135,10 @@ msiexec /x $MsiId$ /qb-! REBOOT=ReallySuppress
 ; deletekey [HKEY_LOCAL_MACHINE\Software\$ProductId$]
 
 [LinkFolder_uninstall]
+set_basefolder common_desktopdirectory
+set_subfolder ""
+delete_element $NAME$
+
 ; Example of deleting a folder from AllUsers startmenu:
 ;
 ; set_basefolder common_programs
@@ -146,6 +161,9 @@ msiexec /x $MsiId$ /qb-! REBOOT=ReallySuppress
 ;
 ; SFTA --unreg Applications\SumatraPDF.exe .pdf
 
+[sub_CustomPostUninstall]
+include_insert "%ScriptPath%\custom\" + $Property_CustomPostUninstall$
+
 [Sub_check_exitcode]
 comment "Test for installation success via exit code"
 set $ExitCode$ = getLastExitCode
diff --git a/sample/All/CLIENT_DATA/setup32.opsiscript b/sample/All/CLIENT_DATA/setup32.opsiscript
index 37d34f7..e61d3dd 100644
--- a/sample/All/CLIENT_DATA/setup32.opsiscript
+++ b/sample/All/CLIENT_DATA/setup32.opsiscript
@@ -34,6 +34,10 @@ set  $languageInfo$ = getLocaleInfoMap
 DefVar $LanguageId$
 set $LanguageId$ = getValue("default_language_id_decimal", $languageInfo$)
 
+;Property Variables
+DefVar $Property_CustomPostInstall$
+DefVar $Property_DesktopLink$
+
 Set $LogDir$ = "%opsiLogDir%"
 
 ; The token BUILDER_VARIABLES will be replaced by opsi-builder.sh
@@ -120,9 +124,24 @@ else
 	comment "Create shortcuts"
 	LinkFolder_install
 	
+	Set $Property_DesktopLink$ = GetProductProperty("DesktopLink","false")
+	if ($Property_DesktopLink$ = "true")
+		LinkFolder_install_DesktopLink
+	endif
+	
 	comment "File Type Association"
 	DosInAnIcon_FileTypeAssoc_install
 	
+	Set $Property_CustomPostInstall$ = getProductProperty("custom-post-install","none")
+	if not ($Property_CustomPostInstall$ = "none")
+		comment "Include custom post install file"
+		if FileExists("%ScriptPath%\custom\" + $Property_CustomPostInstall$)
+			sub_CustomPostInstall
+		else
+			LogError "Include script NOT exists (" + $Property_CustomPostInstall$ +")"
+		endif
+	endif
+	
 endif
 
 [Winbatch_install]
@@ -206,6 +225,22 @@ delete_element "$NAME$"
 ; 	icon_index: 2
 ; end_link
 
+[LinkFolder_install_DesktopLink]
+;
+; Example of creating an shortcut to the installed exe on AllUsers desktop:
+;
+set_basefolder common_desktopdirectory
+set_subfolder ""
+;
+set_link
+	name: $NAME$
+	target: "$InstallDir$\$ProgramExecutable$"
+	parameters:
+	working_dir: $InstallDir$
+	icon_file:
+	icon_index:
+end_link
+
 [DosInAnIcon_FileTypeAssoc_install]
 ; Requires danysys.sfta
 ; Sample Usage:
@@ -229,6 +264,9 @@ delete_element "$NAME$"
 ; Update value of an element
 ; xml ed -u "/xml/table/rec[@id=1]/numField" -v 0 "$InstallDir$\config.xml"> "$InstallDir$\config.model.xml"
 
+[sub_CustomPostInstall]
+include_insert "%ScriptPath%\custom\" + $Property_CustomPostInstall$
+
 [Sub_get_licensekey]
 if opsiLicenseManagementEnabled
 	comment "License management is enabled and will be used"
diff --git a/sample/All/CLIENT_DATA/setup3264.opsiscript b/sample/All/CLIENT_DATA/setup3264.opsiscript
index db07b1c..9a9065a 100644
--- a/sample/All/CLIENT_DATA/setup3264.opsiscript
+++ b/sample/All/CLIENT_DATA/setup3264.opsiscript
@@ -44,6 +44,10 @@ set  $languageInfo$ = getLocaleInfoMap
 DefVar $LanguageId$
 set $LanguageId$ = getValue("default_language_id_decimal", $languageInfo$)
 
+;Property Variables
+DefVar $Property_CustomPostInstall$
+DefVar $Property_DesktopLink$
+
 Set $LogDir$ = "%opsiLogDir%"
 
 ; The token BUILDER_VARIABLES will be replaced by opsi-builder.sh
@@ -127,6 +131,10 @@ else
 		Registry_install /32Bit
 		comment "Create shortcuts"
 		LinkFolder_install_32
+		Set $Property_DesktopLink$ = GetProductProperty("DesktopLink","false")
+		if ($Property_DesktopLink$ = "true")
+			LinkFolder_install_DesktopLink_32
+		endif
 		comment "File Type Association"
 		DosInAnIcon_FileTypeAssoc_install
 	endif
@@ -151,10 +159,24 @@ else
 		Registry_install /64Bit
 		comment "Create shortcuts"
 		LinkFolder_install_64
+		Set $Property_DesktopLink$ = GetProductProperty("DesktopLink","false")
+		if ($Property_DesktopLink$ = "true")
+			LinkFolder_install_DesktopLink_64
+		endif
 		comment "File Type Association"
 		DosInAnIcon_FileTypeAssoc_install
 	endif
 	
+	Set $Property_CustomPostInstall$ = getProductProperty("custom-post-install","none")
+	if not ($Property_CustomPostInstall$ = "none")
+		comment "Include custom post install file"
+		if FileExists("%ScriptPath%\custom\" + $Property_CustomPostInstall$)
+			sub_CustomPostInstall
+		else
+			LogError "Include script NOT exists (" + $Property_CustomPostInstall$ +")"
+		endif
+	endif
+	
 endif
 
 [Winbatch_install_32]
@@ -335,6 +357,38 @@ delete_element "$NAME$"
 ; 	icon_index: 2
 ; end_link
 
+[LinkFolder_install_DesktopLink_32]
+;
+; Example of creating an shortcut to the installed exe on AllUsers desktop:
+;
+set_basefolder common_desktopdirectory
+set_subfolder ""
+;
+set_link
+	name: $NAME$
+	target: "$InstallDir32$\$ProgramExecutable32$"
+	parameters:
+	working_dir: $InstallDir32$
+	icon_file:
+	icon_index:
+end_link
+
+[LinkFolder_install_DesktopLink_64]
+;
+; Example of creating an shortcut to the installed exe on AllUsers desktop:
+;
+set_basefolder common_desktopdirectory
+set_subfolder ""
+;
+set_link
+	name: $NAME$
+	target: "$InstallDir64$\$ProgramExecutable64$"
+	parameters:
+	working_dir: $InstallDir64$
+	icon_file:
+	icon_index:
+end_link
+
 [DosInAnIcon_FileTypeAssoc_install]
 ; Requires danysys.sfta
 ; Sample Usage:
@@ -346,6 +400,9 @@ delete_element "$NAME$"
 ;
 ; SFTA Applications\SumatraPDF.exe .pdf
 
+[sub_CustomPostInstall]
+include_insert "%ScriptPath%\custom\" + $Property_CustomPostInstall$
+
 [Sub_get_licensekey]
 comment "License management is enabled and will be used"
 
diff --git a/sample/All/CLIENT_DATA/setup64.opsiscript b/sample/All/CLIENT_DATA/setup64.opsiscript
index 53efa23..34d0fee 100644
--- a/sample/All/CLIENT_DATA/setup64.opsiscript
+++ b/sample/All/CLIENT_DATA/setup64.opsiscript
@@ -34,6 +34,10 @@ set  $languageInfo$ = getLocaleInfoMap
 DefVar $LanguageId$
 set $LanguageId$ = getValue("default_language_id_decimal", $languageInfo$)
 
+;Property Variables
+DefVar $Property_CustomPostInstall$
+DefVar $Property_DesktopLink$
+
 Set $LogDir$ = "%opsiLogDir%"
 
 ; The token BUILDER_VARIABLES will be replaced by opsi-builder.sh
@@ -120,9 +124,24 @@ else
 	comment "Create shortcuts"
 	LinkFolder_install
 
+	Set $Property_DesktopLink$ = GetProductProperty("DesktopLink","false")
+	if ($Property_DesktopLink$ = "true")
+		LinkFolder_install_DesktopLink
+	endif
+
 	comment "File Type Association"
 	DosInAnIcon_FileTypeAssoc_install
 
+	Set $Property_CustomPostInstall$ = getProductProperty("custom-post-install","none")
+	if not ($Property_CustomPostInstall$ = "none")
+		comment "Include custom post install file"
+		if FileExists("%ScriptPath%\custom\" + $Property_CustomPostInstall$)
+			sub_CustomPostInstall
+		else
+			LogError "Include script NOT exists (" + $Property_CustomPostInstall$ +")"
+		endif
+	endif
+
 endif
 
 [Winbatch_install]
@@ -206,6 +225,22 @@ delete_element "$NAME$"
 ; 	icon_index: 2
 ; end_link
 
+[LinkFolder_install_DesktopLink]
+;
+; Example of creating an shortcut to the installed exe on AllUsers desktop:
+;
+set_basefolder common_desktopdirectory
+set_subfolder ""
+;
+set_link
+	name: $NAME$
+	target: "$InstallDir$\$ProgramExecutable$"
+	parameters:
+	working_dir: $InstallDir$
+	icon_file:
+	icon_index:
+end_link
+
 [DosInAnIcon_FileTypeAssoc_install]
 ; Requires danysys.sfta
 ; Sample Usage:
@@ -229,6 +264,9 @@ delete_element "$NAME$"
 ; Update value of an element
 ; xml ed -u "/xml/table/rec[@id=1]/numField" -v 0 "$InstallDir$\config.xml"> "$InstallDir$\config.model.xml"
 
+[sub_CustomPostInstall]
+include_insert "%ScriptPath%\custom\" + $Property_CustomPostInstall$
+
 [Sub_get_licensekey]
 if opsiLicenseManagementEnabled
 	comment "License management is enabled and will be used"
diff --git a/sample/All/OPSI/postinst b/sample/All/OPSI/postinst
index a8e33fc..b55179e 100644
--- a/sample/All/OPSI/postinst
+++ b/sample/All/OPSI/postinst
@@ -7,3 +7,19 @@
 #   PRODUCT_ID: id of the current product
 #   CLIENT_DATA_DIR: directory which contains the installed client data
 #
+
+TMP_DIR=${CLIENT_DATA_DIR}/../${PRODUCT_ID}.tmp
+
+if [ -d $TMP_DIR ]; then
+	echo 'Restoring previous directories...'
+	
+	echo 'Restoring previous custom dir...'
+	if [ -d $TMP_DIR/custom ]; then
+		test -e $CLIENT_DATA_DIR/custom && rm -rf $CLIENT_DATA_DIR/custom
+		echo "   moving $TMP_DIR/custom to $CLIENT_DATA_DIR/"
+		mv $TMP_DIR/custom $CLIENT_DATA_DIR/ || exit 1	
+	fi
+fi
+
+echo "Removing temporary files..."
+rm -rf $TMP_DIR
diff --git a/sample/All/OPSI/preinst b/sample/All/OPSI/preinst
index 15a5320..34674a6 100644
--- a/sample/All/OPSI/preinst
+++ b/sample/All/OPSI/preinst
@@ -7,3 +7,27 @@
 #   PRODUCT_ID: id of the current product
 #   CLIENT_DATA_DIR: directory where client data will be installed
 #
+
+TMP_DIR=${CLIENT_DATA_DIR}/../${PRODUCT_ID}.tmp
+
+if [ -d $TMP_DIR ]; then
+	echo "Temporary directory $TMP_DIR already exist, aborting!" 1>&2
+	exit 1
+fi
+
+[ ! -d $CLIENT_DATA_DIR ] && mkdir $CLIENT_DATA_DIR
+mkdir $TMP_DIR
+
+if [ -d $CLIENT_DATA_DIR ]; then
+	echo "Saving previous directories..."
+	for dirname in custom ; do
+		for path in $CLIENT_DATA_DIR/$dirname; do
+			if [ -e $path ]; then
+				echo "   moving $path to $TMP_DIR"
+				mv $path $TMP_DIR/ || exit 1
+			fi
+		done
+	done
+fi
+
+exit 0