#!/usr/bin/env bash

SPATH="$(dirname "$(readlink -f ${BASH_SOURCE[0]})")"    # dir of the script
SNAME="$(basename ${BASH_SOURCE[0]})"                    # name of the script

ASCEDSCBDIR=$( dirname "$( readlink -f /usr/bin/asceds-certbot-gencert )" )
ASCEDSHOME="$( dirname ${ASCEDSCBDIR} )"
ASCEDSLIBDIR="$( dirname ${ASCEDSCBDIR} )"/lib
ASCEDSCONFDIR="$( dirname ${ASCEDSCBDIR} )"/etc
ASCEDSETCDIR="/etc/asceds"
ASCEDSCONF='local site hostconf certbot'
SERVICE_LOCK_FILE="${ASCEDSETCDIR}/.servPort80"

# get configuration
for CONFFILE in ${ASCEDSCONF} ; do 
   . ${ASCEDSCONFDIR}/asceds-${CONFFILE}.conf.proto
   if [ -r "${ASCEDSETCDIR}/asceds-${CONFFILE}.conf" ] ; then
      . ${ASCEDSETCDIR}/asceds-${CONFFILE}.conf
   fi
done

# load the functions
. ${ASCEDSLIBDIR}/asceds-utils


P80_SERV=''
if [ -r "${SERVICE_LOCK_FILE}" ] ; then
   P80_SERV="$( cat ${SERVICE_LOCK_FILE} )"
fi

if [ -n "${P80_SERV}" ] ; then
   systemctl start "${P80_SERV}" && /bin/rm "${SERVICE_LOCK_FILE}" || \
      asceds-mailto-alarm "Standalone host $( hostname -f ) cannot restart port 80 process" \
                          "${ASCEDSLOGF}"
fi

