#!/bin/ksh

#   ********************************
#   *                              *
#   *  Checkup SIGMET Environment  *
#   *                              *
#   ********************************
#
# COPYRIGHT (c) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 BY
#         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
# 
# THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
# ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
# INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
# COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
# OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
# TRANSFERED.

ERROR="false"
RDA="false"

# Process command line options
while [ ! "$1" = "" ] ; do
  OPT="$1" ; shift

  if [  "${OPT}" = "-nodepend" ] ; then
    MAKEFILE=${TMPMAKE} ; NODEPEND="true"

  elif [  "${OPT}" =  "help"  -o \
          "${OPT}" = "-help"  ] ; then
    echo "Sigmet_env Options:"
    echo "    -rda : RDA version"
    echo "   -iris : Full IRIS version (default)"
    echo
    exit 0

  elif [  "${OPT}" = "-rda"   ] ; then
    RDA="true"
  fi
done


OURNODE="`uname -n`"
if [ "${OURNODE}" = "" ] ; then
  echo "Warning: Node name of local machine is not defined"
fi

# ==================================================
# Verify that the user running this script appears in the list of operators.
#
echo "Checking IRIS_OPERATORS list..."

if [ "${IRIS_OPERATORS}" = "" ] ; then
  echo "No translation for 'IRIS_OPERATORS'" ; ERROR="true"
else
  echo "${IRIS_OPERATORS}" | grep ${LOGNAME} > /dev/null
  if [ "$?" != "0" ] ; then
    echo "Warning: User '${LOGNAME}' does not appear in the IRIS_OPERATORS"
    echo "         list -- some checking may fail as a result." ; ERROR="true"
  fi
fi

# ==================================================
# Verify that the environment variables for all installed directories
# are defined, and that the directories exist.
#
echo "Checking installation directories..."

if [ "${RDA}" = "true" ] ; then
ENV_LIST="IRIS_APP_DEFAULTS \
  IRIS_BIN \
  IRIS_BIN_ACROBAT \
  IRIS_BITMAPS \
  IRIS_INIT \
  IRIS_KEYS \
  IRIS_MANUALS_EXTRA \
  IRIS_MANUALS_INST \
  IRIS_MANUALS_NOTE \
  IRIS_MANUALS_RCP8 \
  IRIS_MANUALS_RVP8 \
  IRIS_MANUALS_UTIL \
  IRIS_NLS \
  IRIS_ROOT \
  IRIS_SCRIPT"
else
ENV_LIST="IRIS_APP_DEFAULTS \
  IRIS_BIN \
  IRIS_BIN_ACROBAT \
  IRIS_BITMAPS \
  IRIS_INIT \
  IRIS_KEYS \
  IRIS_MANUALS_EXTRA \
  IRIS_MANUALS_INST \
  IRIS_MANUALS_IRIS \
  IRIS_MANUALS_IRISRAD \
  IRIS_MANUALS_NOTE \
  IRIS_MANUALS_PROG \
  IRIS_MANUALS_RCP02 \
  IRIS_MANUALS_RCP8 \
  IRIS_MANUALS_RVP7 \
  IRIS_MANUALS_RVP8 \
  IRIS_MANUALS_UTIL \
  IRIS_NLS \
  IRIS_PIPES \
  IRIS_ROOT \
  IRIS_SCRIPT"
fi

for ITEM in ${ENV_LIST} ; do
  alias CMD="echo ""\$$ITEM""" ; TRANS="`CMD`"
  if [ "${TRANS}" = "" ] ; then
    echo "No translation for '${ITEM}'" ; ERROR="true"
  else
    if [ ! -d ${TRANS} ] ; then
      echo "Missing Installation Directory: ${ITEM} (${TRANS})" ; ERROR="true"
    else
      if [ ! -r ${TRANS}/. ] ; then
        echo "Unreadable Installation Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      else
        if [ "${TRANS##*/}" != "" ] ; then
          if [ "${ITEM}" != "IRIS_ROOT" ] ; then
            echo "Missing '/' at the end of ${ITEM} (${TRANS})" ; ERROR="true"
          fi
        else
          if [ "${ITEM}" = "IRIS_ROOT" ] ; then
            echo "Spurious '/' at the end of ${ITEM} (${TRANS})" ; ERROR="true"
          fi
        fi
      fi
    fi
  fi
done
# ==================================================
# Verify that the environment variables for all the writeable config directories
# are defined, and that the directories exist, and are writable.
#
echo "Checking configuration directories..."

if [ "${RDA}" = "true" ] ; then
ENV_LIST="IRIS_CONFIG \
  IRIS_IMAGES \
  IRIS_LISTINGS \
  IRIS_OVERLAY"
else
ENV_LIST="IRIS_CONFIG \
  IRIS_IMAGES \
  IRIS_LISTINGS \
  IRIS_MENU \
  IRIS_OVERLAY"
fi

for ITEM in ${ENV_LIST} ; do
  alias CMD="echo ""\$$ITEM""" ; TRANS="`CMD`"
  if [ "${TRANS}" = "" ] ; then
    echo "No translation for '${ITEM}'" ; ERROR="true"
  else
    if [ ! -d ${TRANS} ] ; then
      echo "Missing Config Directory: ${ITEM} (${TRANS})" ; ERROR="true"
    else
      if [ ! -w ${TRANS}/. ] ; then
        echo "Unwritable Config Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
      if [ ! -r ${TRANS}/. ] ; then
        echo "Unreadable Config Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      else
        if [ "${TRANS##*/}" != "" ] ; then
	  echo "Missing '/' at the end of ${ITEM} (${TRANS})" ; ERROR="true"
        fi
      fi
    fi
  fi
done

# ==================================================
# Verify that the data directories exist and are both readable and
# writable.
#
echo "Checking data directories..."

if [ "${RDA}" = "true" ] ; then
ENV_LIST="IRIS_LOG \
  IRIS_TEMP"
else
ENV_LIST="IRIS_INGEST \
  IRIS_LOG \
  IRIS_PRODUCT \
  IRIS_PRODUCT_RAW \
  IRIS_TAPE_INV \
  IRIS_TEMP"
fi

for ITEM in ${ENV_LIST} ; do
  alias CMD="echo ""\$$ITEM""" ; TRANS="`CMD`"
  if [ "${TRANS}" = "" ] ; then
    echo "No translation for '${ITEM}'" ; ERROR="true"
  else
    if [ ! -d ${TRANS} ] ; then
      echo "Missing Data Directory: ${ITEM} (${TRANS})" ; ERROR="true"
    else
      if [ ! -w ${TRANS}/. ] ; then
        echo "Unwritable Data Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
      if [ ! -r ${TRANS}/. ] ; then
        echo "Unreadable Data Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      else
        if [ "${TRANS##*/}" != "" ] ; then
          echo "Missing '/' at the end of ${ITEM} (${TRANS})" ; ERROR="true"
        fi
      fi
    fi
  fi
done

# ==================================================
# Check the contents of the IRIS_MENU directory, and verify that all
# file names consist of exactly one dot, and no characters from a
# stop list.
#
if [ ! "${RDA}" = "true" ] ; then
  echo "Checking file names in IRIS_MENU..."

  if [ "${IRIS_MENU}" != "" ] ; then
    if [ -d "${IRIS_MENU}" ] ; then
      cd ${IRIS_MENU}
      for ITEM in `ls -1a` ; do
	if [ "${ITEM}" = "."  ] ; then continue ; fi
	if [ "${ITEM}" = ".." ] ; then continue ; fi
	LEFT="${ITEM%%.*}" ; RIGHT="${ITEM##*.}"
	if [ "${ITEM}" = "${LEFT}.${RIGHT}" -a \
	  "${LEFT}"  != ""               -a \
	  "${RIGHT}" != ""                ] ; then
          echo "${ITEM}" | egrep " |\(|\)|\[|\]|\{|\}|\?|\*" > /dev/null
	  if [ "$?" = "0" ] ; then
	    echo "Illegal character in menu filename: '${IRIS_MENU}${ITEM}'" ; ERROR="true"
	  fi
	else
	  echo "Bad menu filename: '${IRIS_MENU}${ITEM}'" ; ERROR="true"
	fi
      done
    else
	echo "Warning: IRIS_MENU directory is unreadable" ; ERROR="true"
    fi
  else
    echo "Warning: IRIS_MENU variable is improperly defined" ; ERROR="true"
  fi
fi


# ==================================================
# Check root ownership and modes for certain executable files.
#
echo "Checking root file ownerships..."

if [ "${ERROR}" = "false" ] ; then
  cd ${IRIS_BIN}
  if [ ! "${RDA}" = "true" ] ; then
      SUID_LIST="ant_rcv \
      ant_xmt \
      antsim_rcv \
      antsim_xmt \
      ant_log \
      archive \
      copy_data_tape \
      init_iris_lda \
      init_iris_mo \
      init_iris_tape \
      iris \
      irisnet \
      qant \
      qiris \
      restart_iris \
      rib_rcv \
      rib_xmt \
      siris \
      tapex"
  else
      SUID_LIST="ant_rcv \
      ant_xmt \
      antsim_rcv \
      antsim_xmt \
      ant_log \
      qant"
  fi

  for ITEM in ${SUID_LIST} ; do
    if [ ${ITEM} != "`find ${ITEM} -user root -perm 6775 -print`" ] ; then
      echo "Incorrect owner/mode for '${IRIS_BIN}${ITEM}'" ; ERROR="true"
    fi
  done
fi  

# ==================================================
# Exit with error status.
#
if [ "${ERROR}" = "false" ] ; then
  exit 0
else
  echo "\007"
  echo "========================================"
  echo "    Errors Detected -- Please Repair    "
  echo "========================================"
  exit 1
fi
