#!/bin/bash
# ----------------------------------------------------------------
# Copyright (c) uib gmbh (www.uib.de)
# This sourcecode is owned by uib
# and published under the Terms of the General Public License.
# ----------------------------------------------------------------
#
# Post installation script
#	This script executes after unpacking files from the archive and registering the product at the server
#
# The following environment variables can be used to obtain information about the current installation:
#	CLIENT_DATA_DIR: directory where client data will be installed
#	DEPOT_ID: id of the depot
#	PACKAGE_VERSION: package version
#	PRODUCT_ID: id of the current product
#	PRODUCT_TYPE: type of the current product
#	PRODUCT_VERSION: product version

echo -e "Setting variables"
	TMP_DIR=${CLIENT_DATA_DIR}/../${PRODUCT_ID}.tmp

echo -e "\nChecking if $TMP_DIR exists"
	if [ -d $TMP_DIR ]; then
		echo -e "\e[0;32m\tTemporary directory $TMP_DIR exist\e[0m"
	else
		echo -e "\e[0;31m\tTemporary directory $TMP_DIR does not exist. Aborting!\e[0m" 1>&2
		exit 1
	fi

echo -e "\nChecking for directories that need to be restored"
	for dirname in custom files/preload installfiles* localsetup/custom localsetup/installfiles* opsi; do
		for path in $TMP_DIR/$dirname; do
			echo -e "\tChecking if $path exists"
			if [ -e $path ]; then
				echo -e "\e[0;32m\t\t$path exists\n\e[0m"
				echo -e "\tChecking if $path is a symlink or a directory"
				if [ -L $path ]; then
					echo -e "\e[0;32m\t\t$path is a symlink\e[0m"
					if [[ $path == *"/files/"* ]]; then
						echo -e "\e[0;32m\t\tRecreating $path in $CLIENT_DATA_DIR/files/\e[0m\n"
							test -e $CLIENT_DATA_DIR/files/`basename $path` && rm -rf $CLIENT_DATA_DIR/files/`basename $path`
							ln -sr "$(readlink -f "$path")" "$CLIENT_DATA_DIR/files/`basename $path`"
					elif [[ $path == *"/localsetup/"* ]]; then
						echo -e "\e[0;32m\t\tRecreating $path in $CLIENT_DATA_DIR/localsetup/\e[0m\n"
							test -e $CLIENT_DATA_DIR/localsetup/`basename $path` && rm -rf $CLIENT_DATA_DIR/localsetup/`basename $path`
							ln -sr "$(readlink -f "$path")" "$CLIENT_DATA_DIR/localsetup/`basename $path`"
					else
						echo -e "\e[0;32m\t\tRecreating $path in $CLIENT_DATA_DIR/\e[0m\n"
							test -e $CLIENT_DATA_DIR/`basename $path` && rm -rf $CLIENT_DATA_DIR/`basename $path`
							ln -sr "$(readlink -f "$path")" "$CLIENT_DATA_DIR/`basename $path`"
					fi
				elif [ -d $path ]; then
					echo -e "\e[0;32m\t\t$path is a directory\e[0m"
					if [[ $path == *"/files/"* ]]; then
						echo -e "\e[0;32m\t\tMoving directory $path to $CLIENT_DATA_DIR/files\e[0m\n"
							test -e $CLIENT_DATA_DIR/files/`basename $path` && rm -rf $CLIENT_DATA_DIR/files/`basename $path`
							mv $path $CLIENT_DATA_DIR/files || exit 1
					elif [[ $path == *"/localsetup/"* ]]; then
						echo -e "\e[0;32m\t\tMoving directory $path to $CLIENT_DATA_DIR/localsetup\e[0m\n"
							test -e $CLIENT_DATA_DIR/localsetup/`basename $path` && rm -rf $CLIENT_DATA_DIR/localsetup/`basename $path`
							mv $path $CLIENT_DATA_DIR/localsetup || exit 1
					else
						echo -e "\e[0;32m\t\tMoving directory $path to $CLIENT_DATA_DIR\e[0m\n"
							test -e $CLIENT_DATA_DIR/`basename $path` && rm -rf $CLIENT_DATA_DIR/`basename $path`
							mv $path $CLIENT_DATA_DIR || exit 1
					fi
				fi
			else
				echo -e "\e[0;31m\t\t$path does not exist\e[0m\n"
			fi
		done
	done

echo "Checking if multiple installfiles directories are present"
	ADDITIONAL_INSTALLFILES_DIR=`ls -d $CLIENT_DATA_DIR/localsetup/installfiles*`
	if [ -n "$ADDITIONAL_INSTALLFILES_DIR" ]; then
		echo "Determining the former default value of the installfiles_dir property"
			default_value=`opsi-cli --output-format json jsonrpc execute productPropertyState_getValues windows11-upgrade installfiles_dir $DEPOT_ID | cut -d'[' -f2 | cut -d'"' -f2`
			property_id="installfiles_dir"
			possible_values=""

		for dirname in $CLIENT_DATA_DIR/localsetup/installfiles*; do
			dirname=`basename $dirname`
			if [ "$default_value" = "" ]; then
				echo "Setting first found dirname as default if default_value is empty"
					default_value="$dirname"
			fi

			if [ -n "$possible_values" ]; then
				echo "Separating entries of possible_values by comma and whitespace if possible_values is not empty"
					possible_values="${possible_values}, "
			fi

			echo "Adding $dirname to $possible_values"
				possible_values=${possible_values}\"${dirname}\"
		done

		echo "Creating product property installfiles_dir with all found installfiles dirs as selectable values"
			create_property="opsi-admin -d method productProperty_createUnicode $PRODUCT_ID \"$PRODUCT_VERSION\" \"$PACKAGE_VERSION\" $property_id 'installfiles dir to use as installation source' '[${possible_values}]' '[${default_value}]' true false"
			$create_property

		echo "Setting the default value for the property"
			opsi-admin -d method productPropertyState_create $PRODUCT_ID $property_id $DEPOT_ID ${default_value}
	else
		echo "No additional installfiles directories found"
	fi

echo "Generating the .files file with symlink support"
	set +e
	if [ $(uname -m) == 'x86_64' ]; then
		# 64-bit
		chmod +x $(dirname $0)/generatepcf
		$(dirname $0)/generatepcf -p $PRODUCT_ID -d $CLIENT_DATA_DIR
	else
		# 32-bit
		chmod +x $(dirname $0)/generatepcf-x86
		$(dirname $0)/generatepcf-x86 -p $PRODUCT_ID -d $CLIENT_DATA_DIR
	fi
	set -e

echo -e "Removing temporary directory"
	rm -rf $TMP_DIR
	exit 0