#!/bin/bash
# remove an existing client client

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

ASCEDSCONF='local site certbot'
ASCEDSLIBS='utils'

if [ ! -r "${SPATH}/asceds-head" ] ; then
   echo "No header ${SPATH}/asceds-head. Exiting"
   exit 9
fi

. ${SPATH}/asceds-head

asceds-client-remove-usage()
{
  cat << ENDUSAGE

Usage: asceds-client-remove [-h] [-q] -c <client_name>

Removes existing client: cert files from /etc/letsencrypt and from 
${ASCEDSHOMEDIR} (create backup copy);
to be run on the cert manager as root/sudo, directly or through 
asceds-propagate-certbot in /etc/cron.d/asceds-cert-propagate 
if the client is offline for more than 30 days;
-h --> display usage and exit
-c <client_name> --> client to be setup; print usage if missing
-q --> no question asked; sends output to the logs

Removes letsencrypt certificate files
Moves ${ASCEDSHOMEDIR} certificate files to .bak copy 
Removes host down flag

ENDUSAGE
exit 0
}

# output to terminal by default
QUIETRUN=''

# initialize CLIENT_NAME
CLIENT_NAME=''

# general validation and parse of the command line
if  [ -z "${ARGS}" ] ; then
   asceds-client-setup-usage
else
   for ARG in ${ARGS} ; do
      #echo "ARG=${ARG}"
      if [ "${ARG}" != "${ARG//[^-_a-zA-Z0-9.]/}" ] ; then
         asceds-error "Malformed argument ${ARG}"
      elif [ "${ARG}" == "-h" ] ; then
         asceds-client-remove-usage
      elif [ "${ARG}" == "-q" ] ; then
         shift
         asceds-exec-noninteractive "$*"
         # bring other scripts in non-interactive mode
         QUIETRUN="y"
      elif [ "${ARG}" == "-c" ] ; then
         shift
         if [[ "$1" != -* ]] && [ -n "$1" ] ; then
            CLIENT_NAME=$( cat <<< $1 | tr '[:upper:]' '[:lower:]' )
            asceds-expand-hostname ${CLIENT_NAME} || CLIENT_NAME=${ASCEDS_FQDN}
            asceds-validate-fqdn "${CLIENT_NAME}" || \
               asceds-error "Invalid client FQDN ${CLIENT_NAME}"
         fi
      else
         shift
      fi
   done
fi
if [ -z "${CLIENT_NAME}" ] ; then
   asceds-client-remove-usage
fi
# run as root only
asceds-run-asroot

# checks on client name
if [ -z "${CLIENT_NAME}" ] || [[ "${CLIENT_NAME}" = "-"* ]] ; then
   asceds-error "No client name passed as argument"
elif [ ! -d "${ASCEDSHOMEDIR}/${CLIENT_NAME}" ] && 
     [ ! -e "${ASCEDSHOMEDIR}/${CLIENT_NAME}_cert.conf" ] ; then
   asceds-error "No client ${CLIENT_NAME} registered on this server"
else
   asceds-echo "Setting up client ${CLIENT_NAME}"
fi

# remove letsencrypt files and dirs
LE_DIR="/etc/letsencrypt"
if [ -e "${LE_DIR}/renewal/${CLIENT_NAME}.conf" ] ; then
   ${RM} -f ${LE_DIR}/renewal/${CLIENT_NAME}.conf
fi
if [ -d "${LE_DIR}/archive/${CLIENT_NAME}" ] ; then
   ${RM} -Rf ${LE_DIR}/archive/${CLIENT_NAME}
fi
if [ -d "${LE_DIR}/live/${CLIENT_NAME}" ] ; then
   ${RM} -Rf ${LE_DIR}/live/${CLIENT_NAME}
fi

# backup asceds config
if [ -d "${ASCEDSHOMEDIR}/${CLIENT_NAME}" ] ; then
   ${MV} ${ASCEDSHOMEDIR}/${CLIENT_NAME} ${ASCEDSHOMEDIR}/${CLIENT_NAME}.bak
fi
if [ -e "${ASCEDSHOMEDIR}/${CLIENT_NAME}_cert.conf" ] ; then
   ${MV} ${ASCEDSHOMEDIR}/${CLIENT_NAME}_cert.conf \
         ${ASCEDSHOMEDIR}/${CLIENT_NAME}_cert.conf.bak
fi

# host down cleanup
if [ -e "${ASCEDSHOMEDIR}/down/${CLIENT_NAME}" ] ; then
   ${RM} -f ${ASCEDSHOMEDIR}/down/${CLIENT_NAME}
fi







