#!/bin/bash
#
# postinst script
# This script executes after unpacking files from that archive and registering the product at the depot.
#
# The following environment variables can be used to obtain information about the current installation:
#   PRODUCT_ID: id of the current product
#   PRODUCT_TYPE: type of the current product
#   PRODUCT_VERSION: product version
#   PACKAGE_VERSION: package version
#   CLIENT_DATA_DIR: directory where client data will be installed
#


#-------------------------------------------------------------------------------
# Create configs (if not already exists)
#-------------------------------------------------------------------------------
EMPTYARRAY=""

CHECKCONFIG="software-on-demand.installation-now-button"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
ANSWERARRAY=`opsi-admin -dS method config_getObjects '[]' ${CHECKSTRING}`
if [ "${EMPTYARRAY}" == "${ANSWERARRAY}" ] ; then
  echo "config ${CHECKCONFIG} does not exists - creating"
  opsi-admin -d method config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "true"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="software-on-demand.admin-mode"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
ANSWERARRAY=`opsi-admin -dS method config_getObjects '[]' ${CHECKSTRING}`
if [ "${EMPTYARRAY}" == "${ANSWERARRAY}" ] ; then
  echo "config ${CHECKCONFIG} does not exists - creating"
  opsi-admin -d method config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="software-on-demand.disable-tilesview"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
ANSWERARRAY=`opsi-admin -dS method config_getObjects '[]' ${CHECKSTRING}`
if [ "${EMPTYARRAY}" == "${ANSWERARRAY}" ] ; then
  echo "config ${CHECKCONFIG} does not exists - creating"
  opsi-admin -d method config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

#-------------------------------------------------------------------------------
# END of Create configs
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Restore ock_custom directory from ${CLIENT_DATA_DIR}.tmp 
#-------------------------------------------------------------------------------
TMP_DIR=${CLIENT_DATA_DIR}.tmp
if [ -d $TMP_DIR ]; then
	echo "Restoring previous ock_custom directory..."
	echo "   coping ${TMP_DIR}/files/app/ock_custom to ${CLIENT_DATA_DIR}/files/app"
	cp -r --remove-destination ${TMP_DIR}/files/app/ock_custom ${CLIENT_DATA_DIR}/files/app || exit 1
	#sudo opsi-set-rights $OCK_APP_DIR
	echo "Removing tempory directory..."
	rm -rf $TMP_DIR 
fi
#-------------------------------------------------------------------------------
# END of Restore ock_custom directory from ${CLIENT_DATA_DIR}.tmp
#-------------------------------------------------------------------------------

exit 0