# stub on top of certbot scripts

ASCEDSHOME="$(dirname ${SPATH})"                         # asceds home guess
PATH=${SPATH}:${PATH}                                    # access to scripts

# store the arguments
ARGS=$*

# set config locations
if [ -z "${ASCEDSHOME}" ] ; then
   echo "Install dir for ASCEDS cannot be determined. Exiting"
   exit 1
fi
if [ -z "${ASCEDSETCDIR}" ] && [ -d "/etc/asceds" ] ; then
   ASCEDSETCDIR="/etc/asceds"
else
   echo "No local config dir for ASCEDS: /etc/asceds. Exiting"
   exit 1
fi
if [ -z "${ASCEDSCONFDIR}" ] && [ -d "${ASCEDSHOME}/etc" ] ; then
   ASCEDSCONFDIR="${ASCEDSHOME}/etc"
else
   echo "No general config dir for ASCEDS: ${ASCEDSHOME}/etc. Exiting"
   exit 1
fi

# get the configuration
for CONFFILE in ${ASCEDSCONF} ; do
   if [ -r "${ASCEDSCONFDIR}/asceds-${CONFFILE}.conf.proto" ] ; then
      . ${ASCEDSCONFDIR}/asceds-${CONFFILE}.conf.proto
   else
      asceds-error "No config file ${ASCEDSCONFDIR}/asceds-${CONFFILE}.conf.proto
      Reinstall ASCEDS"
   fi
done
for CONFFILE in ${ASCEDSCONF} ; do
   if [ -r "${ASCEDSETCDIR}/asceds-${CONFFILE}.conf" ] ; then
      . ${ASCEDSETCDIR}/asceds-${CONFFILE}.conf
   fi
done
if [ -r "${ASCEDSCONFDIR}/version" ] ; then
   ASCEDSVER=$( cat ${ASCEDSCONFDIR}/version )
else
   echo "No version file ${ASCEDSCONFDIR}/version found"
   ASCEDSVER="Unknown"
fi

# load libraries
if [ -d "${ASCEDSLIBDIR}" ] ; then
   for LIBFILE in ${ASCEDSLIBS} ; do
      if [ -r "${ASCEDSLIBDIR}/asceds-${LIBFILE}" ] ; then
         . ${ASCEDSLIBDIR}/asceds-${LIBFILE}
      else
         asceds-error "No ${LIBFILE} lib ${ASCEDSLIBDIR}/asceds-${LIBFILE}"
      fi
   done
else
   echo "No dir ${ASCEDSHOME}/lib ... reinstall ASCEDS"
   exit 1
fi

#asceds-greetings "$0 $*"

ARGS1=''
for ARG in ${ARGS} ; do 
   if [[ "${ARG}" = -* ]] ; then 
      OPTS=${ARG//[^a-zA-Z0-9]/} 
      for OPT in $( cat <<< ${OPTS//[^a-zA-Z0-9]/} | sed 's/./& /g' ) ; do 
         ARGS1="${ARGS1} -${OPT} "
      done
   else 
      ARGS1="${ARGS1} ${ARG} " 
   fi
done
ARGS=${ARGS1}
set - ${ARGS}

# check logging system
asceds-logdir-check

