#!/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
#   CLIENT_DATA_DIR: directory which contains the installed client data
#

PUBLIC_DIR="/var/lib/opsi/public"
INSTALLER="${CLIENT_DATA_DIR}/oca-installation-helper"

if [ -e "${INSTALLER}" -a -d "${PUBLIC_DIR}" ]; then
	mkdir -p "${PUBLIC_DIR}/opsi-client-agent" || true
	if [ -d "${PUBLIC_DIR}/opsi-client-agent" ]; then
		cp "${INSTALLER}" "${PUBLIC_DIR}/opsi-client-agent/opsi-mac-client-agent-installer.command" || true
	fi
fi

chmod u+x,g+x $CLIENT_DATA_DIR/opsi-deploy-client-agent

TMP_DIR=${CLIENT_DATA_DIR}/../${PRODUCT_ID}.tmp
if [ ! -d $TMP_DIR ]; then
	echo "Nothing to do!"
	exit 0
fi

if [ -d "${TMP_DIR}/custom" ]; then
	echo "Restoring custom dir"
	[ -e "${CLIENT_DATA_DIR}/files/custom" ] && rm -rf "${CLIENT_DATA_DIR}/files/custom"
	mv "${TMP_DIR}/custom" "${CLIENT_DATA_DIR}/files/custom" || exit 1
fi

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

[ -e "${CLIENT_DATA_DIR}/files/custom/opsi-script-skin" ] && rm -rf "${CLIENT_DATA_DIR}/files/custom/opsi-script-skin"
[ -e "${CLIENT_DATA_DIR}/files/custom/notifier" ] && rm -rf "${CLIENT_DATA_DIR}/files/custom/notifier"

[ -e "${CLIENT_DATA_DIR}/files/custom/opsi-notifier" ] || mkdir -p "${CLIENT_DATA_DIR}/files/custom/opsi-notifier"
[ -e "${CLIENT_DATA_DIR}/files/custom/motd-notifier" ] || mkdir -p "${CLIENT_DATA_DIR}/files/custom/motd-notifier"
[ -e "${CLIENT_DATA_DIR}/files/custom/opsi-script/skin" ] || mkdir -p "${CLIENT_DATA_DIR}/files/custom/opsi-script/skin"


CHECKCONFIG="opsi-script.global.debug_prog"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
if [ -z $(opsi-cli --output-format=csv --no-header jsonrpc execute config_getObjects '[]' ${CHECKSTRING}) ] ; then
  echo "config ${CHECKCONFIG} does not exist - creating"
  opsi-cli jsonrpc execute config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="opsi-script.global.debug_lib"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
if [ -z $(opsi-cli --output-format=csv --no-header jsonrpc execute config_getObjects '[]' ${CHECKSTRING}) ] ; then
  echo "config ${CHECKCONFIG} does not exist - creating"
  opsi-cli jsonrpc execute config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="opsi-script.global.default_loglevel"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
if [ -z $(opsi-cli --output-format=csv --no-header jsonrpc execute config_getObjects '[]' ${CHECKSTRING}) ] ; then
  echo "config ${CHECKCONFIG} does not exist - creating"
  opsi-cli jsonrpc execute config_createUnicode "${CHECKCONFIG}" "${CHECKCONFIG}"  "[\"4\",\"5\",\"6\",\"7\",\"8\"]" "6" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="opsi-script.global.force_min_loglevel"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
if [ -z $(opsi-cli --output-format=csv --no-header jsonrpc execute config_getObjects '[]' ${CHECKSTRING}) ] ; then
  echo "config ${CHECKCONFIG} does not exist - creating"
  opsi-cli jsonrpc execute config_createUnicode "${CHECKCONFIG}" "${CHECKCONFIG}"  "[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]" "0"  "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="opsi-script.global.AutoActivityDisplay"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
if [ -z $(opsi-cli --output-format=csv --no-header jsonrpc execute config_getObjects '[]' ${CHECKSTRING}) ] ; then
  echo "config ${CHECKCONFIG} does not exist - creating"
  opsi-cli jsonrpc execute config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "true"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="opsi-script.global.ScriptErrorMessages"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\"}"
if [ -z $(opsi-cli --output-format=csv --no-header jsonrpc execute config_getObjects '[]' ${CHECKSTRING}) ] ; then
  echo "config ${CHECKCONFIG} does not exist - creating"
  opsi-cli jsonrpc execute config_createBool "${CHECKCONFIG}" "${CHECKCONFIG}" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

exit 0
