#! /bin/bash
#
# postinst script for opsi-winst
# 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: id of the current product
#   CLIENT_DATA_DIR: directory which contains the installed client data
#

EMPTYARRAY=""

CHECKCONFIG="opsi-script.global.testsyntax"
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="opsi-script.global.debug_prog"
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="opsi-script.global.debug_lib"
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="opsi-script.global.default_loglevel"
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_createUnicode "${CHECKCONFIG}" "${CHECKCONFIG}"  "[\"4\",\"5\",\"6\",\"7\",\"8\"]" "7" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi
## 4.12.0.7 create with new default loglevel
#echo "config  ${CHECKCONFIG}  - creating"
#opsi-admin -d method config_createUnicode "${CHECKCONFIG}" "${CHECKCONFIG}"  "[\"4\",\"5\",\"6\",\"7\",\"8\"]" "7" "false"


CHECKCONFIG="opsi-script.global.force_min_loglevel"
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_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.ScriptErrorMessages"
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="opsi-script.global.AutoActivityDisplay"
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="opsi-script.global.w10BitlockerSuspendOnReboot"
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="opsi-script.global.ReverseProductOrderByUninstall"
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="opsi-script.global.supressSystemEncodingWarning"
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="opsi-script.global.log_rotation_count"
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_createUnicode "${CHECKCONFIG}" "${CHECKCONFIG}"  "[\"1\",\"4\",\"8\",\"16\",\"32\"]" "8" "true" "false"
else
  echo "config ${CHECKCONFIG} already exists"
fi

CHECKCONFIG="opsi-script.global.writeProductLogFile"
CHECKSTRING="{\"id\":\"${CHECKCONFIG}\",\"type\":\"BoolConfig\"}"
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

# Skin files
if [ -d "${CLIENT_DATA_DIR}/common/skin-old" ]; then
	if [[ $OPSI_SERVER_VERSION = 4.3.* ]]; then
		rm -r "${CLIENT_DATA_DIR}/common/skin-old"
	else
		rm -r "${CLIENT_DATA_DIR}/common/skin"
		mv "${CLIENT_DATA_DIR}/common/skin-old" "${CLIENT_DATA_DIR}/common/skin"
	fi
fi
