#!/bin/bash
#
# postinst script
# This script executes after unpacking files from that archive and registering the product at the server.
#
# The following environment variables can be used to obtain information about the current installation:
#   PRODUCT_ID, PRODUCT_TYPE, PRODUCT_VERSION, PACKAGE_VERSION, CLIENT_DATA_DIR, DEPOT_ID

TMP_DIR=${CLIENT_DATA_DIR}/../${PRODUCT_ID}.tmp

if [ -d $TMP_DIR ]; then
	echo 'Restoring previous directories...'
	for dirname in drivers custom installfiles* winpe* ; do
		for path in $TMP_DIR/$dirname; do
			if [ -d $path ]; then
				test -e $CLIENT_DATA_DIR/`basename $path` && rm -rf $CLIENT_DATA_DIR/`basename $path`
				echo "   moving $path to $CLIENT_DATA_DIR"
				mv $path $CLIENT_DATA_DIR/ || exit 1
			fi
		done
	done

	if [ -e $CLIENT_DATA_DIR/opsi/unattend.xml.template ]; then
		if [ ! -e $CLIENT_DATA_DIR/custom/unattend.xml ]; then
			echo "   no unattend.xml found in custom dir, copying template"
			cp $CLIENT_DATA_DIR/opsi/unattend.xml.template $CLIENT_DATA_DIR/custom/unattend.xml
			chmod 660 $CLIENT_DATA_DIR/custom/unattend.xml
		fi
		MD5SUM=$(md5sum $CLIENT_DATA_DIR/opsi/unattend.xml.template | awk '{ print $1 }')
		echo "    md5sum of unattend.xml.template is ${MD5SUM}"
		sed -i s/UNATTENDMD5SUM/${MD5SUM}/g $CLIENT_DATA_DIR/setup.py
	fi
	[ -e $CLIENT_DATA_DIR/custom/unattend.xml ] && chmod 660 $CLIENT_DATA_DIR/custom/unattend.xml
	sed -i "s/c:\drv/#@winpe_partition_letter*#:\drv" $CLIENT_DATA_DIR/custom/unattend.xml
fi

# 4.0.7.4 (22.9.2017) create winpe_uefi as symlink if not existing (do)
if [ -e $CLIENT_DATA_DIR/winpe ]; then
	if [ ! -e $CLIENT_DATA_DIR/winpe_uefi ] ; then
		echo "Creating winpe_uefi as symlink to winpe..."
		cd $CLIENT_DATA_DIR
		ln -s winpe winpe_uefi
	fi
fi


# Check if there is not only the installfiles dir but other (do 21.4.2016)
echo "Check if there are additional installfiles directories ..."
property_id="installfiles_dir"
ADDITIONAL_INSTALLFILES_DIR=`ls -d $CLIENT_DATA_DIR/installfiles?*`
if [ -n "$ADDITIONAL_INSTALLFILES_DIR" ]; then
	echo "Adding product property definition installfiles_dir..." 
	possible_values=()
	default_value=""
	for dirname in $CLIENT_DATA_DIR/installfiles*; do
		echo $dirname
		if [ -f "${dirname}/setup.exe" ] && ([ -f "${dirname}/install.wim" ] || [ -f "${dirname}/sources/install.wim" ] || [ -f "$    {dirname}/sources/install.swm" ] || [ -f "$    {dirname}/sources/install.esd" ]); then
			dirname=`basename $dirname`
			if [ "$default_value" = "" ]; then
				default_value="$dirname"
			fi
			possible_values=(${possible_values[@]} \"${dirname}\")
		fi
	done

	# read current possible values from file created in preinst
	current_values=$(head -n 1 /tmp/current_installfiles)
	current_values=(${current_values[@]})
	# merge current and new possible values and make sure the list contains installfiles 
	possible_values=(${possible_values[@]} ${current_values[@]} \"installfiles\")
	# create comma separated string
	possible_values=($(echo "${possible_values[@]}" | tr ' ' '\n' | sort -u | tr '\n' ','))
	possible_values=${possible_values::-1} 


	echo "$property_id current possible values: ${current_values[@]}"
	echo "$property_id new possible values: ${possible_values}"
	echo "Executing: 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"

	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

	echo "Executing: opsi-admin -d method productPropertyState_create $PRODUCT_ID $property_id $DEPOT_ID ${default_value}" 
	opsi-admin -d method productPropertyState_create $PRODUCT_ID $property_id $DEPOT_ID ${default_value}
else
	echo ""
	echo "No installfiles dirs found..." 
fi


# Patching scripts to work with opsi 4.1 and opsi 4.2
if [ -e /usr/bin/opsi-python ]; then
	# Python 3 - opsi 4.2 or later
	echo "Running on opsi 4.2 or later. Nothing to do."
else
	echo "Running on opsi 4.1. Patching scripts..."
	# Python 2 - opsi 4.1
	sed --in-place "s_/usr/bin/opsi-python_/usr/bin/python_" "$CLIENT_DATA_DIR/show_drivers.py"
	sed --in-place "s_/usr/bin/opsi-python_/usr/bin/python_" "$CLIENT_DATA_DIR/create_driver_links.py"
fi

echo "Updating PCI and USB IDs"
if [ -f $CLIENT_DATA_DIR/drivers/pci.ids ]; then
	rm $CLIENT_DATA_DIR/drivers/pci.ids
fi
if [ -f $CLIENT_DATA_DIR/drivers/usb.ids ]; then
	rm $CLIENT_DATA_DIR/drivers/usb.ids
fi

echo "Removing temporary files..."
rm -rf $TMP_DIR

chmod u+x,g+x $CLIENT_DATA_DIR/*.py
