#!/bin/bash
# cleans asceds certificate data on the client

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

ASCEDSCONF='local site hostconf'
ASCEDSLIBS='utils hosts postfix openssl'

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

. ${SPATH}/asceds-head

asceds-clean-usage()
{
  cat << ENDUSAGE

Usage: asceds-clean [-h]
Cleans asceds certificate data on the client
-h --> display usage and exit

Removes the certificate request ${ASCEDSHOMEDIR}/cert.conf
Removes any existing certificates from ${ASCEDSHOMEDIR}/certs/

ENDUSAGE
exit 0
}

# general validation and parse of the command line
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-clean-usage
   else
      shift
   fi
done

# run as root only
asceds-run-asroot

# remove the certificate request
if [ -f "${ASCEDSHOMEDIR}/cert.conf" ] ; then
   ${RM} -f ${ASCEDSHOMEDIR}/cert.conf
else 
   asceds-echo "certificate request file ${ASCEDSHOMEDIR}/cert.conf \
                does not exist"
fi

# remove any existing certificates
if [ -d "${ASCEDSHOMEDIR}/certs" ] ; then
   ${RM} -Rf ${ASCEDSHOMEDIR}/certs
   mkdir ${ASCEDSHOMEDIR}/certs
   chown asceds:asceds ${ASCEDSHOMEDIR}/certs
else 
   asceds-echo "certificate dir ${ASCEDSHOMEDIR}/certs \
                does not exist"
fi




