#!/bin/sh

# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj. All rights reserved.

# ==================================================
#
# Checkup SIGMET Environment
# Set ownership of files and directories
#
# ==================================================

CHECK_FAIL="false"
RDA="false"
SETOWN="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 " -setown : Set correct ownership for files and directories"
    echo
    exit 0

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

if [ ${SETOWN} = "true" ] ; then
  echo "Setting the ownership and permissions of the configuration files."
  sudo chown operator:users /etc/vaisala/irisrda
  sudo chmod 6775 /etc/vaisala/irisrda
  sudo chown -R operator:users /etc/vaisala/irisrda/*
  sudo chmod -R 664 /etc/vaisala/irisrda/*
  sudo chmod 6775 /etc/vaisala/irisrda/listings
  sudo chmod 2775 /etc/vaisala/irisrda/images
  sudo chmod 2775 /etc/vaisala/irisrda/init
  sudo chmod 2775 /etc/vaisala/irisrda/menu
  sudo chmod 2775 /etc/vaisala/irisrda/overlay
  exit 0
fi

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 -n "Checking IRIS_OPERATORS list..."

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

if [ "${ERROR}" = "true" ] ; then
  CHECK_FAIL="true"
else
  echo -n " OK"
fi

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

ENV_LIST="IRIS_APP_DEFAULTS \
  IRIS_BIN \
  IRIS_BIN_ACROBAT \
  IRIS_BITMAPS \
  IRIS_INIT \
  IRIS_KEYS \
  IRIS_FLOCKS \
  IRIS_LIBEXEC \
  IRIS_LIBEXEC_SERVICES \
  IRIS_NLS \
  IRIS_PIPES \
  IRIS_ROOT \
  IRIS_SHARE \
  IRIS_SOUNDS"

ERROR="false"
for ITEM in ${ENV_LIST} ; do
  alias CMD="echo ""\$$ITEM""" ; TRANS="`CMD`"
  if [ "${TRANS}" = "" ] ; then
    echo -ne "\nNo translation for '${ITEM}'" ; ERROR="true"
  else
    if [ ! -d ${TRANS} ] ; then
      echo -ne "\nMissing Installation Directory: ${ITEM} (${TRANS})" ; ERROR="true"
    else
      if [ ! -r ${TRANS}/. ] ; then
        echo -ne "\nUnreadable Installation Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
    fi
  fi
done

if [ "${ERROR}" = "true" ] ; then
  CHECK_FAIL="true"
else
  echo -n " OK"
fi

# ==================================================
# Verify that the environment variables for all the writeable config directories
# are defined, and that the directories exist, and are writable.
#
echo -ne "\nChecking configuration directories..."
ERROR="false"

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 -ne "\nNo translation for '${ITEM}'" ; ERROR="true"
  else
    if [ ! -d ${TRANS} ] ; then
      echo -ne "\nMissing Config Directory: ${ITEM} (${TRANS})" ; ERROR="true"
    else
      if [ ! -w ${TRANS}/. ] ; then
        echo -ne "\nUnwritable Config Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
      if [ ! -r ${TRANS}/. ] ; then
        echo -ne "\nUnreadable Config Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
    fi
  fi
done

if [ "${ERROR}" = "true" ] ; then
  CHECK_FAIL="true"
else
  echo -n " OK"
fi

# Check that is a config/retired file exists, it is a directory
if [ -r ${IRIS_CONFIG}/retired ] ; then
    if [ ! -d ${IRIS_CONFIG}/retired ] ; then
       echo -e "\nConfig directory is a file: ${IRIS_CONFIG}/retired" ; CHECK_FAIL="true"
    fi
fi

# ==================================================
# Verify that the data directories exist and are both readable and
# writable.
#
echo -ne "\nChecking data directories..."
ERROR="false"

if [ "${RDA}" = "true" ] ; then
ENV_LIST="IRIS_DATA \
  IRIS_INGEST \
  IRIS_LOG \
  IRIS_MANUALS_EXTRA_DIR \
  IRIS_MANUALS_NOTE_DIR \
  IRIS_PRINT_DIR \
  IRIS_PRODUCT \
  IRIS_PRODUCT_RAW \
  IRIS_SPOOL \
  IRIS_TAPE_INV \
  IRIS_TEMP"
fi

for ITEM in ${ENV_LIST} ; do
  alias CMD="echo ""\$$ITEM""" ; TRANS="`CMD`"
  if [ "${TRANS}" = "" ] ; then
    echo -ne "\nNo translation for '${ITEM}'" ; ERROR="true"
  else
    if [ ! -d ${TRANS} ] ; then
      echo -ne "\nMissing Data Directory: ${ITEM} (${TRANS})" ; ERROR="true"
    else
      if [ ! -w ${TRANS}/. ] ; then
        echo -ne "\nUnwritable Data Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
      if [ ! -r ${TRANS}/. ] ; then
        echo -ne "\nUnreadable Data Directory: ${ITEM} (${TRANS})" ; ERROR="true"
      fi
    fi
  fi
done

if [ "${ERROR}" = "true" ] ; then
  CHECK_FAIL="true"
else
  echo -n " OK"
fi

# ==================================================
# 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 -ne "\nChecking file names in IRIS_MENU..."
  ERROR="false"

  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 -ne "\n${ITEM}" | grep -E " |\(|\)|\[|\]|\{|\}|\?|\*" > /dev/null
	  if [ "$?" = "0" ] ; then
	    echo -ne "\nIllegal character in menu filename: '${IRIS_MENU}${ITEM}'" ; ERROR="true"
	  fi
	else
	  echo -ne "\nBad menu filename: '${IRIS_MENU}${ITEM}'" ; ERROR="true"
	fi
      done
    else
	echo -ne "\nWarning: IRIS_MENU directory is unreadable" ; ERROR="true"
    fi
  else
    echo -ne "\nWarning: IRIS_MENU variable is improperly defined" ; ERROR="true"
  fi

  if [ "${ERROR}" = "true" ] ; then
    CHECK_FAIL="true"
  else
    echo -n " OK"
  fi
fi

# ==================================================
# Check root ownership and modes for certain executable files.
# This list must match that in utils/install_tools/instiris.
#
echo -ne "\nChecking root file ownerships..."
ERROR="false"
COMMON_EXECS="ps_iris qant sant sig_lpstat sigbrush sigmet_env sigterm structmap"
IRIS_EXECS="qiris sigmet-iris-error siris"
RDA_EXECS="rdaflash tsquit"
IRISAPPS="antenna ascope bitex color_setup dspx_scope errdiag iris irisnet manuals overlay ribsetup rtdisp runways setup sigaudio sigbru sigxbell tsarchive tsswitch utils vribbon zauto7"
ALL_EXECS="${COMMON_EXECS} ${RDA_EXECS} ${IRIS_EXECS} ${IRISAPPS}"

cd ${IRIS_BIN}

for ITEM in ${ALL_EXECS} ; do
  if [ ${ITEM} != "`find ${ITEM} -user root -print -follow`" ] ; then
    echo -ne "\nIncorrect owner for '${IRIS_BIN}${ITEM}'" ; ERROR="true"
  fi
done

if [ "${ERROR}" = "true" ] ; then
  CHECK_FAIL="true"
else
  echo -n " OK"
fi

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