#!/bin/bash
# configure services handled by ASCEDS

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

ASCEDSCONF='local site'
ASCEDSLIBS='utils'

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

. ${SPATH}/asceds-head

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

Configure services handled by ASCEDS.
Usage: asceds-services [-h] [-a <service>] [-d <service>]
-h --> display usage and exit
-d <service> --> deactivate one service (before activating)
                 multiple [-d <service>] are allowed
-a <service> --> activate or refresh one service, 
                 multiple [-a <service>] are allowed,
                 each activated service is reconfigured through 
                      asceds-service-reconfig -q -s <service>   

Shows available services to be updated by
      scripts in ${ASCEDSSERVDIR}/*.sh.proto;
Shows activated service update scripts;
Shows which local scripts (if any) are different from templates;
Shows which local scripts (if any) have no template;
Deactives local .sh scripts in ${ASCEDSSERVCONF}/, keeping a backup;
Activates local .sh scripts in ${ASCEDSSERVCONF}/ starting from templates; 
With no argument, offers a simple interface to activate/deactivate services
      by typing in a space separated list.
To refresh a local script from template, both deactivate and activate it.


ENDUSAGE
exit 0
}

# initialize service list to be added
ADDSERV=""

# initialize service list to be deleted
DELSERV=""

# 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-services-usage
   elif [ "${ARG}" == "-a" ] ; then
      shift
      if [[ "$1" != -* ]] && [ -n "$1" ] ; then
         ADDSERV="${ADDSERV} $1"
      fi
   elif [ "${ARG}" == "-d" ] ; then
      shift
      if [[ "$1" != -* ]] && [ -n "$1" ] ; then
         DELSERV="${DELSERV} $1"
      fi
   else
      shift
   fi
done

# run as root only
asceds-run-asroot

# display available services
AVAILABLESERV=""
if [ -d "${ASCEDSSERVDIR}" ] && 
   [ "$( echo ${ASCEDSSERVDIR}/*.sh.proto )" != "${ASCEDSSERVDIR}/*.sh.proto" ]  ; then
   cd ${ASCEDSSERVDIR}
   for SERV in *.sh.proto ; do
      if [ -f "${SERV}" ] ; then
         AVAILABLESERV="${AVAILABLESERV} ${SERV%.sh.proto}"
      fi
   done
elif [ ! -d "${ASCEDSSERVDIR}" ] ; then
   asceds-error "No dir for service templates: ${ASCEDSSERVDIR}"
fi
asceds-echo "Available services: ${AVAILABLESERV}"

# display activated services
ACTIVESERV=''
DIFFFROMTEMP=''
NOTEMPSERV=''
if [ -d "${ASCEDSSERVCONF}" ] && \
   [ "$( echo ${ASCEDSSERVCONF}/*.sh )" != "${ASCEDSSERVCONF}/*.sh" ]  ; then
   cd ${ASCEDSSERVCONF}
   for SERV in *.sh ; do
      if [ -f "${SERV}" ] ; then
         ACTIVESERV="${ACTIVESERV} ${SERV%.sh}"
      fi
      
      if [ -r ${ASCEDSSERVDIR}/${SERV}.proto ] ; then
         # take into account: service scripts are selfconfiguring
         if [ -n "$( diff -I "SITESDIR=" ${ASCEDSSERVDIR}/${SERV}.proto ${SERV} )" ] ; then
            DIFFFROMTEMP="${DIFFFROMTEMP} ${SERV%.sh}"
         fi
      else
         NOTEMPSERV="${NOTEMPSERV} ${SERV%.sh}"
      fi
   done
elif [ ! -d "${ASCEDSSERVCONF}" ] ; then
   asceds-error "No dir for services: ${ASCEDSSERVCONF}"
fi
asceds-echo "Active services: ${ACTIVESERV}"
if [ -n "${DIFFFROMTEMP}" ] ; then
   asceds-echo "Not matching templates: ${DIFFFROMTEMP}"
   for SERV in ${DIFFFROMTEMP} ; do
      echo -n "Do you want to replace the script for refreshing ${SERV}? [y/N] "
      read YE
      if [ "${YE}" == 'y' ] || [ "${YE}" == 'Y' ] ; then
         DELSERV="${DELSERV} ${SERV}"
         ADDSERV="${ADDSERV} ${SERV}"
      fi
   done
fi
if [ -n "${NOTEMPSERV}" ] ; then
   asceds-echo "No templates found for: ${NOTEMPSERV}"
fi

if [ -z "${ADDSERV}" ] && [ -z "${DELSERV}" ] ; then
   # dialog populating ADDSERV and DELSERV
   echo "Please specify services to be deactivated/activated"
   echo "  (services separated by spaces, <Enter> if none)"
   # if any activated services
   ACTIVESERV=$( ls -1 ${ASCEDSSERVCONF} | ${GREP} -v "\.bak$" | wc -l )
   if [ "${ACTIVESERV}" != "0" ] ; then
      read -e -p "Deactivate currently active services: " DELSERV
      echo
      if [ "${DELSERV}" != "${DELSERV//[^-_a-zA-Z0-9. ]/}" ] ; then
         asceds-error "Services to be deactivated contain illegal characters."
      fi
   fi
   # if any services available but not activated
   AVSERV=$( ls -1 ${ASCEDSSERVDIR} | ${GREP} "\.sh\.proto$" | wc -l )
   if [ "${ACTIVESERV}" != "${AVSERV}" ] ; then
      read -e -p "ACTIVATE/refresh services: " ADDSERV
      echo
      if [ "${ADDSERV}" != "${ADDSERV//[^-_a-zA-Z0-9. ]/}" ] ; then
         asceds-error "Services to be activated contain illegal characters."
      fi
   fi 
fi

# delete services
if [ -n "${DELSERV}" ] ; then
   for SERV in ${DELSERV} ; do
      if [ -f "${ASCEDSSERVCONF}/${SERV}.sh" ] ; then
         ${MV} -f ${ASCEDSSERVCONF}/${SERV}.sh \
            ${ASCEDSSERVCONF}/${SERV}.sh.bak
         asceds-echo "Deactivated service ${SERV}"
      else
         asceds-warning "No file ${SERV}.sh found in ${ASCEDSSERVCONF}/"
      fi
   done
fi

# add services
if [ -n "${ADDSERV}" ] ; then
   for SERV in ${ADDSERV} ; do
      if [ -f "${ASCEDSSERVDIR}/${SERV}.sh.proto" ] ; then
         if [ -f "${ASCEDSSERVCONF}/${SERV}.sh" ] ; then
            ${MV} -f ${ASCEDSSERVCONF}/${SERV}.sh \
               ${ASCEDSSERVCONF}/${SERV}.sh.bak
         fi
         ${CP} -f ${ASCEDSSERVDIR}/${SERV}.sh.proto \
            ${ASCEDSSERVCONF}/${SERV}.sh
         ${SPATH}/asceds-service-reconfig -q -s ${SERV}
         asceds-echo "Activated service ${SERV}"
      else
         asceds-warning "No file ${SERV}.sh.proto found in ${ASCEDSSERVDIR}/"
      fi
   done
fi

