# functions for handling ASCEDS_HOSTS=/etc/hosts
# 
# asceds-hosts-check "<hostname>"
# checks if <hostname> is configured into hosts
#    returns 0 if configured, 1 if not configured
# 
# asceds-hosts-add "<hostname>" "<IPaddress>"
# adds <hostname> to the hostname list in hosts
#    returns 0 if successful, 1 if not
# 
# asceds-hosts-del "<hostname>"
# deletes <hostname> from the hostname list in hosts
#    returns 0 if successful, 1 if not
# 
# 
# 
# 
##################################################################
# checks if <hostname> is configured into hosts
#    returns 0 if configured, 1 if not configured

# Usage: asceds-hosts-check "<hostname>"
asceds-hosts-check()
{
   local HSTNAME="$1"
   if [ -z "${HSTNAME}" ] ; then
      asceds-error "Empty value for the hostname"
   fi

   if [ ! -r "${ASCEDS_HOSTS}" ] ; then
      asceds-warning "No ${ASCEDS_HOSTS} file on this system"
      return 1
   fi

   TESTHST=$( sed -e "s/#.*$//g" ${ASCEDS_HOSTS} | \
              ${GREP} "[[:space:]]${HSTNAME}" | wc -l )
   if [ "${TESTHST}" = "0" ] ; then
      asceds-echo "${HSTNAME} not configured into ${ASCEDS_HOSTS}"
      return 1
   elif [ "${TESTHST}" = "1" ] ; then
      asceds-echo "${HSTNAME} configured into ${ASCEDS_HOSTS}"
      return 0
   else
      asceds-warning "${HSTNAME} configured into ${ASCEDS_HOSTS} multiple times"
      return 0
   fi
}

##################################################################
# adds <hostname> to the hostname list in hosts
#    returns 0 if successful, 1 if not

# Usage: asceds-hosts-add "<hostname>" "<IPaddress>"
asceds-hosts-add()
{
   local HSTNAME="$1"
   if [ -z "${HSTNAME}" ] ; then
      asceds-error "Empty value for the hostname"
   fi

   local IPADDR="$2"
   if [ -z "${IPADDR}" ] ; then
      asceds-error "Empty value for the IP address"
   elif [ "${IPADDR}" != "${IPADDR//[^0-9.]/}" ] ; then
      asceds-error "Bad string for the IP address"
   fi

   if [ ! -r "${ASCEDS_HOSTS}" ] ; then
      asceds-warning "No ${ASCEDS_HOSTS} file on this system"
      return 1
   fi

   # short hostname
   SHORTHSTNAME=${HSTNAME%%.*}

   # add the info to /etc/hosts
   cat <<< "${IPADDR}   ${HSTNAME}   ${SHORTHSTNAME}" >> ${ASCEDS_HOSTS}

   asceds-hosts-check "${HSTNAME}" || return 0
   return 1   
}

##################################################################
# deletes <hostname> from the hostname list in hosts
#    returns 0 if successful, 1 if not

# Usage: asceds-hosts-del "<hostname>"
asceds-hosts-del()
{
   local HSTNAME="$1"
   if [ -z "${HSTNAME}" ] ; then
      asceds-error "Empty value for the hostname"
   fi

   if [ ! -r "${ASCEDS_HOSTS}" ] ; then
      asceds-warning "No ${ASCEDS_HOSTS} file on this system"
      return 1
   fi

   # short hostname
   SHORTHSTNAME=${HSTNAME%%.*}

   sed -e "/^[[:space:]]*[0-9]/ s/[[:space:]]\+${HSTNAME}\b//" \
      ${ASCEDS_HOSTS} > ${ASCEDS_HOSTS}.new
   sed -i -e "s/^[[:space:]]*//" ${ASCEDS_HOSTS}.new
   sed -i -e "/^[0-9.][0-9.]*[[:space:]]\+${SHORTHSTNAME}\b[[:space:]]*$/d" \
      ${ASCEDS_HOSTS}.new

   ${CP} -af ${ASCEDS_HOSTS} ${ASCEDS_HOSTS}.bak
   ${MV} -f ${ASCEDS_HOSTS}.new ${ASCEDS_HOSTS}

   asceds-hosts-check "${HSTNAME}" || return 1
   return 0
}

##################################################################



