#!/bin/bash

BE_DIR=/etc/sys-admin

BE_CONF=${BE_DIR}/config/sys-admin.conf

#####################################################################

CFG_DIR=${BE_DIR}/config
CFGX_DIR=${BE_DIR}/config-example

for cfgp in `ls -1 ${CFGX_DIR}/*`; do

	cfgf=`basename ${cfgp}`

	if ! [ -e ${CFG_DIR}/${cfgf} ]; then
		cp ${CFGX_DIR}/${cfgf} ${CFG_DIR}/
	fi

done


#####################################################################

TPL_DIR=${BE_DIR}/templates
TPLX_DIR=${BE_DIR}/templates-example


for tplp in `ls -1 ${TPLX_DIR}/*.tpl`; do

	tplf=`basename $tplp`

	if ! [ -e ${TPL_DIR}/$tplf ]; then
		ln -s ../templates-example/${tplf} ${TPL_DIR}/${tplf}
	fi

done

for txtp in `ls -1 ${TPLX_DIR}/*.txt`; do

	txtf=`basename ${txtp}`

	if ! [ -e ${TPL_DIR}/${txtf} ]; then
		cp ${TPLX_DIR}/${txtf} ${TPL_DIR}/
	fi

done

#####################################################################

SKL_DIR=${BE_DIR}/skeleton
SKLX_DIR=${BE_DIR}/skeleton-example

for sklp in `ls -1 ${SKLX_DIR}/*`; do

	sklf=`basename ${sklp}`

	if ! [ -e ${SKL_DIR}/${sklf} ]; then
		cp ${SKLX_DIR}/${sklf} ${SKL_DIR}/
	fi

done

#####################################################################

if [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi

#####################################################################

deb-systemd-helper unmask sys-admin.socket >/dev/null || true
if deb-systemd-helper --quiet was-enabled sys-admin.socket; then
	deb-systemd-helper enable sys-admin.socket >/dev/null || true
	systemctl start sys-admin.socket || true
else
	deb-systemd-helper update-state sys-admin.socket >/dev/null || true
fi

#####################################################################

if [ -f "${BE_CONF}" ]; then
	echo "sys-admin DB:"
	${BE_DIR}/installation/db/db-schema.php

	echo "sys-admin CONFIG:"
	${BE_DIR}/installation/config/config-schema.php
fi

#####################################################################

#deb-systemd-helper unmask php-tmpdirs.service >/dev/null || true
#if deb-systemd-helper --quiet was-enabled php-tmpdirs.service; then
#	deb-systemd-helper enable php-tmpdirs.service >/dev/null || true
##	systemctl start php-tmpdirs.service || true
#else
#	deb-systemd-helper update-state php-tmpdirs.service >/dev/null || true
#fi

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -x "/etc/init.d/php-tmpdirs" ]; then
		update-rc.d php-tmpdirs defaults >/dev/null
#		invoke-rc.d php-tmpdirs start || exit 1
	fi
fi
