#!/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.
# ----------------------------------------------------------------
#
# Pre installation script
#	This script executes before the package will be unpacked from its archive file
#
# 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 $CLIENT_DATA_DIR exists"
	if [ ! -d $CLIENT_DATA_DIR ]; then
		mkdir $CLIENT_DATA_DIR && echo -e "\e[0;32m\t$CLIENT_DATA_DIR created\e[0m"
	else
		echo -e "\e[0;32m\t$CLIENT_DATA_DIR already exists\e[0m"
	fi

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

echo -e "\nChecking for directories that need to be backed up"
	if [ -d $CLIENT_DATA_DIR ]; then
		for dirname in custom files/preload installfiles* localsetup/custom localsetup/installfiles* opsi; do
			for path in $CLIENT_DATA_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\tConverting symlink $path from relative to abolute and recreating it in $TMP_DIR/files\e[0m\n"
								mkdir -p "$TMP_DIR/files"
								ln -s "$(realpath "$path")" "$TMP_DIR/files/`basename $path`"
						elif [[ $path == *"/localsetup/"* ]]; then
							echo -e "\e[0;32m\t\tConverting symlink $path from relative to abolute and recreating it in $TMP_DIR/localsetup\e[0m\n"
								mkdir -p "$TMP_DIR/localsetup"
								ln -s "$(realpath "$path")" "$TMP_DIR/localsetup/`basename $path`"
						else
							echo -e "\e[0;32m\t\tConverting symlink $path from relative to abolute and recreating it in $TMP_DIR\e[0m\n"
								ln -s "$(realpath "$path")" "$TMP_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 $TMP_DIR/files\e[0m\n"
								mkdir -p "$TMP_DIR/files"
								mv $path $TMP_DIR/files
						elif [[ $path == *"/localsetup/"* ]]; then
							echo -e "\e[0;32m\t\tMoving directory $path to $TMP_DIR/localsetup\e[0m\n"
								mkdir -p "$TMP_DIR/localsetup"
								mv $path $TMP_DIR/localsetup
						else
							echo -e "\e[0;32m\t\tMoving directory $path to $TMP_DIR\e[0m\n"
								mv $path $TMP_DIR || exit 1
						fi
					fi
				else
					echo -e "\e[0;31m\t\t$path does not exist\e[0m\n"
				fi
				done
		done
	fi
	exit 0