#!/bin/ksh
# **************************************
# *                                    *
# * Complete IRIS Installation/Upgrade *
# *                                    *
# **************************************
#
#  COPYRIGHT (c) 1993, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
#                       2004, 2005, 2006 BY
#            VAISALA INC., 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.

# Note the invocation directory, and its parent, on entry.
#
RUNDIR=`pwd`
cd .. ; RUNDIRUP=`pwd`

# Be sure to keep the following app-defaults definitions consistent in
# the "prepare" and "instiris" scripts.
#
IRISAPPS="agcal antenna ascope bitex colormap color_setup dspx_scope gaincal iris irisnet manuals overlay ribsetup rtdisp runways setup sigaudio sigbru stcwave trigger tsarchive tsswitch utils vribbon window zauto6 zauto7"
X11APPDIR=/usr/lib/X11/app-defaults

# WARNING: Manually maintain this code in: make_iris_tape,
# instiris, ps_iris & sig_uname_filter.  Anything ever run by root
# cannot use sig_uname_filter.
#              SEE ALSO "RMACHINE" PROCESSING BELOW.
#
MACHINE="`uname -s`"

if [ "$MACHINE" = 'IRIX64' ] ; then
  MACHINE="IRIX"
fi

# Remove any troublesome aliases that we may have entered with.
#
unalias cp 2> /dev/null ; unalias mv 2> /dev/null
unalias ls 2> /dev/null ; unalias rm 2> /dev/null

# ==============================
# Verify that there is an 'operator' UID, and get the name of the
# primary group for that user.  This will be the default group to
# which files will be assigned.
#
NUMGROUP=""
if [ -r /etc/passwd ] ; then
  NUMGROUP=`egrep "^operator:" /etc/passwd | sed "s/^operator:[^:]*:[^:]*://g" |
    sed "s/:.*//"`
fi
if [ "${NUMGROUP}" = "" ] ; then
  echo " = = = = = = = = = = = = = = = = = = = = = = = = = = = ="  1>&2
  echo "Could not locate username 'operator' in /etc/passwd file." 1>&2
  echo "Please create that user before proceeding"                 1>&2
  echo " = = = = = = = = = = = = = = = = = = = = = = = = = = = ="  1>&2
  exit 1
fi

GROUP=""
if [ -r /etc/group ] ; then
  GROUP=`egrep "^[^:]*:[^:]*:${NUMGROUP}:" /etc/group | sed "s/:.*//"`
fi
if [ "${GROUP}" = "" ] ; then
  echo " = = = = = = = = = = = = = = = = = = = = = = = = = = = ="     1>&2
  echo "WARNING: Could not translate group number ${NUMGROUP} into a" 1>&2
  echo "symbolic group name via /etc/group file."                     1>&2
  echo "Proceeding using the numerical value..."                      1>&2
  echo " = = = = = = = = = = = = = = = = = = = = = = = = = = = ="     1>&2
  GROUP="${NUMGROUP}"
fi

# ==============================
# Process argument list
#
if [ "$MACHINE" = 'HP-UX' ] ; then
  CDROM="/cdrom/"     ; CDSUBDIR="hp-ux"
elif [ "$MACHINE" = 'IRIX' ] ; then
  CDROM="/CDROM/"     ; CDSUBDIR="irix"
elif [ "$MACHINE" = 'Linux' ] ; then
  CDROM="/mnt/cdrom/" ; CDSUBDIR="linux"
else
  echo "Unknown Machine: '${MACHINE}'" 1>&2 ; exit 1
fi

   TARGNODE="`uname -n`" ; OPERATION="" ; VERBOSE="" ; MEDIA=""
    FILEDIR="${RUNDIR}"  ;   ROOTDIR="" ;  RDAOPT=""

       COPY="false" ;  EMACS="false" ;  HEADERS="false" ; OBJECTS="false"
     SOURCE="false" ; SETOWN="false" ; TARGETED="false" ; MANUALS="false"
   EXAMPLES="false" ;    WEB="false" ; TOMCAT="false"   ; NODELETE="false"

if [ "$1" = "" ] ; then HELP="true" ; else HELP="false" ; fi

while [ ! "$1" = "" ] ; do
  OPT="$1" ; shift

  if [ "${OPT}" = "-help" ] ; then
    HELP="true"

  elif [ "${OPT}" = "-new" ] ; then
    if [ "${OPERATION}" = "upgrade" ] ; then
      echo "Can not specify both -new and -upgrade" 1>&2 ; exit 1
    fi
    OPERATION="new"

  elif [ "${OPT}" = "-upgrade" ] ; then
    if [ "${OPERATION}" = "new" ] ; then
      echo "Can not specify both -new and -upgrade" 1>&2 ; exit 1
    fi
    OPERATION="upgrade"

  elif [ "${OPT}" = "-emacs" ] ; then
    EMACS="true"

  elif [ "${OPT}" = "-setown" ] ; then
    SETOWN="true"

  elif [ "${OPT}" = "-manuals" ] ; then
    MANUALS="true"

  elif [ "${OPT}" = "-headers" ] ; then
    HEADERS="true"

  elif [ "${OPT}" = "-objects" ] ; then
    OBJECTS="true"

  elif [ "${OPT}" = "-source" ] ; then
    SOURCE="true"

  elif [ "${OPT}" = "-web" ] ; then
    WEB="true"

  elif [ "${OPT}" = "-tomcat" ] ; then
    TOMCAT="true"

  elif [ "${OPT}" = "-nodelete" ] ; then
    NODELETE="true"

  elif [ "${OPT}" = "-rda" ] ; then
    RDAOPT="-rda"

  elif [ "${OPT}" = "-examples" ] ; then
    if [ "$1" = "" ] ; then
      echo "'-examples' requires args for the PRODUCT and PRODUCT_RAW directories." 1>&2
      exit 1
    else
      PRODUCT_DIR="$1" ; shift
      if [ "$1" = "" ] ; then
        echo "'-examples' requires args for the PRODUCT and PRODUCT_RAW directories." 1>&2
        exit 1
      else
        PRODUCT_RAW_DIR="$1" ; shift
        if [ "${PRODUCT_DIR##/*}" != "" -o "${PRODUCT_RAW_DIR##/*}" != "" ] ; then
          echo "PRODUCT and PRODUCT_RAW directories for examples must begin with '/'" 1>&2
          exit 1
        fi
      fi
    fi
    EXAMPLES="true"

  elif [  "${OPT}" = "-v" ] ; then
    VERBOSE="v"

  elif [  "${OPT}" = "-copy" ] ; then
    if [ "${MEDIA}" != "" ] ; then
      echo "Can not specify -copy with -cdrom or -files." 1>&2 ; exit 1
    fi
    COPY="true"

  elif [  "${OPT}" = "-cdrom" ] ; then
    if [ "${COPY}" = "true" -o "(" "${MEDIA}" != "cdrom" -a "${MEDIA}" != "" ")" ] ; then
      echo "Can not specify -cdrom with -copy or -files." 1>&2 ; exit 1
    fi
    MEDIA="cdrom"
    if [ "$1" != "" -a "${1#-*}" = "$1" ] ; then
      CDROM="$1" ; shift
    fi

  elif [  "${OPT}" = "-files" ] ; then
    if [ "${COPY}" = "true" -o "(" "${MEDIA}" != "files" -a "${MEDIA}" != "" ")" ] ; then
      echo "Can not specify -files with -copy or -cdrom." 1>&2 ; exit 1
    fi
    MEDIA="files"
    if [ "$1" != "" -a "${1#-*}" = "$1" ] ; then
      FILEDIR="$1" ; shift
    fi

  elif [  "${OPT}" = "-group" ] ; then
    if [ "$1" = "" ] ; then
      echo "Missing group name" 1>&2 ; exit 1
    else
      GROUP="$1" ; shift
    fi

  elif [  "${OPT}" = "-target" ] ; then
    if [ "$1" = "" ] ; then
      echo "'-target' requires a network node name" 1>&2 ; exit 1
    else
      TARGETED="true" ; TARGNODE="$1" ; shift
    fi

  elif [  "${OPT}" = "-root" ] ; then
    if [ "$1" = "" ] ; then
      echo "'-root' requires a root point for IRIS" 1>&2 ; exit 1
    else
      ROOTDIR="$1" ; shift
      if [ "${ROOTDIR##/*}" != "" ] ; then
        echo "Root point for IRIS must begin with '/'" 1>&2 ; exit 1
      fi
      if [ "${ROOTDIR%%*/}" = "" ] ; then
        echo "Root point for IRIS must not end with '/'" 1>&2 ; exit 1
      fi
      if [ "${ROOTDIR##*//*}" != "${ROOTDIR}" ] ; then
        echo "Root point for IRIS must not contain repeated '/'" 1>&2 ; exit 1
      fi
    fi

  else
    echo "Unknown option: '${OPT}'" 1>&2 ; HELP="true"
  fi

done

# Print HELP messages if these were requested, either directly or
# indirectly.
#
if [ "${HELP}" = "true" ] ; then
  echo "Command Line Options:"
  echo "                     -new : Install a new IRIS from scratch"
  echo "                 -upgrade : Upgrade IRIS, preserving previous configurations"
  echo "                 -manuals : Install Manuals and Adobe Acrobat Reader"
  echo "                 -headers : Install optional definition files"
  echo "                 -objects : Install compiled objects and executables"
  echo "                   -emacs : Install GNU EMACS text editor"
  echo "                  -source : Install standard and optionally purchased source"
  echo "                -nodelete : Keep all old files from prior installations"
  echo "   -examples <prod> <raw> : Install example products from CDROM"
  echo ""
  echo "     -cdrom [mount point] : CDROM drive       \  Chooses where the"
  echo "             -copy [-rda] : Installed IRIS/RDA > IRIS installation"
  echo "       -files [directory] : Individual files  /    is read from."
  echo ""
  echo "                  -setown : Set all file ownerships and modes"
  echo "            -group <name> : Set specific group, rather than operator's"
  echo "           -target <node> : Choose the node name of the target computer"
  echo "        -root <directory> : Choose IRIS root directory (e.g. /usr/sigmet)"
  echo "                       -v : Verbose (print progress messages)"
  echo "                     -web : Install Web server code"
  echo "                  -tomcat : Install tomcat.  Used with Web server code"
  exit 0
fi

# ==============================
# Determine whether the command line options actually request that
# something be installed.
#
INSTALL_SOMETHING="false"

if [ "${OPERATION}" != ""     -o \
     "${EMACS}"      = "true" -o \
     "${MANUALS}"    = "true" -o \
     "${HEADERS}"    = "true" -o \
     "${OBJECTS}"    = "true" -o \
     "${EXAMPLES}"   = "true" -o \
     "${SOURCE}"     = "true" -o \
     "${WEB}"        = "true" -o \
     "${TOMCAT}"     = "true"    ] ; then
  INSTALL_SOMETHING="true"
fi ;

# ==============================
# Do some simple sanity checks on what has been requested
#
if [ "${INSTALL_SOMETHING}" = "true" ] ; then
  if [ "$COPY" = "false" -a "$MEDIA" = "" ] ; then
    echo "Must specify installation source (-copy, -cdrom, or -files)." 1>&2 
    exit 1
  fi
else
  if [ "${SETOWN}" = "false" ] ; then
    echo "Nothing to install; Nothing to setown.  Exiting..." 1>&2 
    exit 1
  fi
fi

if [ "$COPY" = "true" -a "$TARGETED" = "false" ] ; then
  echo "For a copy, must specify a remote target with -target." 1>&2
  exit 1
fi

# ==============================
# Check for problems in defining the root point for the installation.
# The IRISROOT variable will be get defined here if all goes well.
#
if [ "${ROOTDIR}" != "" ] ; then
  # Always use any root point that was specifed on the command line.
  #
  IRISROOT="${ROOTDIR%/}"
else
  # Targeted installations must specify the root -- give up if not.
  #
  if [ "${TARGETED}" = "true" ] ; then
    echo "For targeted installations you must also use '-root'." 1>&2
    exit 1
  fi
  # If the IRIS_ROOT external environment variable is already defined
  # but does not match the parent of the directory that we are running
  # in, then do not use either as the root point.
  #
  if [ "${IRIS_ROOT}" != "" ] ; then
    if [ "${IRIS_ROOT%/}" != "${RUNDIRUP%/}" ] ; then
      echo "You do not appear to be running in IRIS_ROOT (${IRIS_ROOT})." 1>&2
      echo "Please specify the installation directory with '-root'." 1>&2
      exit 1
    fi
  else
    echo "Cowardly refusing to infer the intended root point." 1>&2
    echo "Please specify the installation directory with '-root'." 1>&2
    exit 1
  fi
  # If no root point was chosen on the command line, but an IRIS_ROOT
  # definition exists that matches our parent directory, then use the
  # parent directory as the root point.
  #
  IRISROOT="${RUNDIRUP%/}"
fi

# Force SETOWN if we are installing anything at all.
#
if [ "${INSTALL_SOMETHING}" = "true" ] ; then
  SETOWN="true"
fi

# ==============================
# Machine specific inits.
#   1) Define the remote shell command on the local machine.
#   2) Setup default drive name if none has been specified so far.
#   3) Define the 'tar' command that can read from its standard input and
#      extract the necessary files.
#
if [ "$MACHINE" = 'HP-UX' ] ; then
  alias REMSH="remsh ${TARGNODE} -l operator"
  TAR="tar x${VERBOSE}f -"
  EMACSDIR=/usr/local/bin
elif [ "$MACHINE" = 'IRIX' ] ; then
  alias REMSH="rsh ${TARGNODE} -l operator"
  TAR="tar xB${VERBOSE}f -"
  EMACSDIR=/usr/local/emacs-20.3
elif [ "$MACHINE" = 'Linux' ] ; then
  alias REMSH="rsh ${TARGNODE} -l operator"
  TAR="tar x${VERBOSE}f -"
  EMACSDIR=/usr/local/bin
fi

# Handle targeted versus local installations.  Setup some of the same
# shell variables as above, except for the remote machine.
#
if [ "$TARGETED" = "true" ] ; then

  # For this determination of remote OS type, see comments/warnings
  # above for "uname -s and sig_uname_filter"
  #
  RMACHINE="`REMSH 'uname -s' 2>/dev/null`"
  if [ "$?" != "0" ] ; then
    echo "Could not access node '${TARGNODE}'." 1>&2 ; exit 1
  fi
  if [ "$RMACHINE" = 'IRIX64' ] ; then
    RMACHINE="IRIX"
  fi

  if [ "$RMACHINE" = 'HP-UX' ] ; then
    TAR="tar x${VERBOSE}f -"
    EMACSDIR=/usr/local/bin
  elif [ "$RMACHINE" = 'IRIX' ] ; then
    TAR="tar xB${VERBOSE}f -"
    EMACSDIR=/usr/local/emacs-20.3
  elif [ "$RMACHINE" = 'Linux' ] ; then
    TAR="tar x${VERBOSE}f -"
    EMACSDIR=/usr/local/bin
  else
    echo "Unknown operating system (${RMACHINE}) on node '${TARGNODE}'." 1>&2
    exit 1
  fi

  if [ "$COPY" = "true" ] ; then
    if [ "$MACHINE" != "$RMACHINE" ] ; then
      echo "Local and remote machines do not match -- Can't Copy/Install." 1>&2
      exit 1
    fi
    if [ "${TARGNODE}" = "`uname -n`" ] ; then
      echo "Can't Copy/Install onto the same machine." 1>&2
      exit 1
    fi
  fi

fi

# ==============================
# Setup the name of the source directory that holds the installation
# files, and the aliases for reading and skipping those files.
#
if [ "${COPY}" = "true" ] ; then
  if [ "${RDAOPT}" = "-rda" ] ; then
    if [ ! -d ${IRIS_ROOT}/bin/rda ] ; then
      echo "Can not make an RDA release from this machine" 1>&2 ; exit 1
    fi
  else
    if [ ! -r ${IRIS_ROOT}/bin/siris ] ; then
      echo "Can not make an IRIS release from this machine" 1>&2 ; exit 1
    fi
  fi
  alias READFILE='${RUNDIR}/make_iris_tape ${RDAOPT} -stdout ${RECNUM}'

else
  if [ "$MEDIA" = "cdrom" ] ; then
    if [ "$MACHINE" = 'HP-UX' ] ; then
      if [ ! -d ${CDROM%/}/hp-ux -a -d ${CDROM%/}/HP-UX  ] ; then
        echo "Please remount the CD-ROM using '-ocdcase' under HP-UX." 1>&2
        exit 1
      fi
    fi
    SOURCEDIR="${CDROM%/}/${CDSUBDIR}/"
  else
    SOURCEDIR="${FILEDIR%/}/"
  fi
  alias READFILE='cat ${SOURCEFILE} | gunzip -c'
fi

alias SKIPRECORD='echo "Skipping ${THISRECORD}..."'
alias MISSINGRECORD='echo "${THISRECORD} not installed because ${SOURCEFILE} is missing"'

# Function to set the SOURCEFILE variable to the input file having
# either a ".tgz" (prefered) or ".gz" suffix (secondary).
#
setSourceFile() {
  SOURCEFILE="${SOURCEDIR}$1.tgz"
  if [ ! -r "${SOURCEFILE}" ] ; then
    SOURCEFILE="${SOURCEDIR}$1.gz"
  fi
}

# ==============================
# Make sure destination directories exist and are writable
#
if [ "${TARGETED}" = "true" ] ; then
  TEST=`REMSH if test -d ${IRISROOT} -a -w ${IRISROOT} ';' then echo okay ';' fi 2>/dev/null`
else
  TEST=`if test -d ${IRISROOT} -a -w ${IRISROOT} ; then echo okay ; fi`
fi
if [ "${TEST}" != "okay" ] ; then
  echo "Can not access '${IRISROOT}' on node '${TARGNODE}'" 1>&2 ; exit 1
fi

if [ "$EXAMPLES" = "true" ] ; then
#  if [ "$MEDIA" != "cdrom" ] ; then
#    echo "To install examples, you must use a CDROM (specify the -cdrom flag)." 1>&2 
#    exit 1
#  fi
  if [ "${TARGETED}" = "true" ] ; then
    TEST1=`REMSH if test -d ${PRODUCT_DIR}     -a -w ${PRODUCT_DIR}     ';' then echo okay ';' fi 2>/dev/null`
    TEST2=`REMSH if test -d ${PRODUCT_RAW_DIR} -a -w ${PRODUCT_RAW_DIR} ';' then echo okay ';' fi 2>/dev/null`
  else
    TEST1=`if test -d ${PRODUCT_DIR}     -a -w ${PRODUCT_DIR}     ; then echo okay ; fi`
    TEST2=`if test -d ${PRODUCT_RAW_DIR} -a -w ${PRODUCT_RAW_DIR} ; then echo okay ; fi`
  fi
  if [ "${TEST1}" != "okay" -o "${TEST2}" != "okay" ] ; then
    echo "Can not access '${PRODUCT_DIR}' and '${PRODUCT_RAW_DIR}' on node '${TARGNODE}'" 1>&2 ; exit 1
  fi
  TMP_DIR=${IRISROOT}"/tmp_prod"
  if [ "${TARGETED}" = "false" ] ; then
    rm -rf ${TMP_DIR} 1>/dev/null 2>/dev/null
    TEST1=`if test -d ${TMP_DIR} ; then echo okay ; fi 2>/dev/null`
    mkdir ${TMP_DIR} 1>/dev/null 2>/dev/null
    TEST2=`if test -d ${TMP_DIR} ; then echo okay ; fi 2>/dev/null`
  fi
  if [ "${TEST1}" = "okay" -o "${TEST2}" != "okay" ] ; then
    echo "Can not create temporary directory '${TMP_DIR}' on node '${TARGNODE}'" 1>&2 ; exit 1
  fi
fi

if [ "${EMACS}" = "true" ] ; then
  if [ "${TARGETED}" = "true" ] ; then
    TEST=`REMSH if test -d ${EMACSDIR} -a -w ${EMACSDIR} ';' then echo okay ';' fi 2>/dev/null`
  else
    TEST=`if test -d ${EMACSDIR} -a -w ${EMACSDIR} ; then echo okay ; fi`
  fi
  if [ "${TEST}" != "okay" ] ; then
    echo "Can not access '${EMACSDIR}' on node '${TARGNODE}'" 1>&2 ; exit 1
  fi
fi

# ==============================
# If it looks like there is an IRIS already installed at the target point,
# then check to make sure that none of those processes are currently running.
#
PSIRIS=""
if [ "${TARGETED}" = "true" ] ; then
  TEST=`REMSH if test -r ${IRISROOT}/bin/ps_iris ';' then echo okay ';' fi 2>/dev/null`
  if [ "${TEST}" = "okay" ] ; then
    if [ "${VERBOSE}" = "v" ] ; then
      echo "Checking for IRIS already running on node '${TARGNODE}'..." ; fi
    PSIRIS="`REMSH export IRIS_BIN=${IRISROOT}/bin/ \; ${IRISROOT}/bin/ps_iris 2>/dev/null`"
  fi
else
  if [ -r ${IRISROOT}/bin/ps_iris ] ; then
    if [ "${VERBOSE}" = "v" ] ; then
      echo "Checking for local IRIS already running..." ; fi
    PSIRIS="`export IRIS_BIN=${IRISROOT}/bin/ ; ${IRISROOT}/bin/ps_iris`"
  fi
fi

TEST=`echo "${PSIRIS}" | sed 's/^IRIS Activity on.*$//'`
if [ "${TEST}" != "" ] ; then
  echo "" 1>&2
  echo "Sorry, processes are still running on node '${TARGNODE}'" 1>&2
  echo "" 1>&2
  echo "${PSIRIS}" 1>&2
  exit 1
fi

# ==============================
# Prevent various lossages associated with trying to install the manual
# pages on systems that are symbolically linked back to the original source.
# Also, do not allow the original source to be clobbered during any kind of
# installation.
#
if [ "${MANUALS}" = "true" ] ; then
  if [ "${TARGETED}" = "true" ] ; then
    TEST=`REMSH if '[' -h ${IRISROOT}/manuals ']' ';' then echo symlink ';' fi 2>/dev/null`
    if [ "${TEST}" = "symlink" ] ; then
      echo "Can not install manuals on top of a symbolic link" 1>&2 ; exit 1
    fi
  fi
  if [ "${TARGNODE}" = "haze" -a "${IRISROOT}" = "/usr/sigmet" ] ; then
    echo "Can not overwrite source version of manuals" 1>&2 ; exit 1
  fi
fi

# ==============================
# If we are eventually going to be doing a "setown", then verify right
# away that we are running as root.
#
if [ "${SETOWN}" = "true" -a "${TARGETED}" = "false" ] ; then
  if [ ! -w / ] ; then
    echo "Can not perform 'setown' operations -- please run as 'root'." 1>&2 ; exit 1
  fi
fi

# ==============================
# Print diagnostics and possibly exit here for debugging
#
DEBUG="false"
if [ "${DEBUG}" = "true" ] ; then
  echo "         IRISROOT = '${IRISROOT}'"
  echo "        OPERATION = '${OPERATION}'"
  echo "             COPY = '${COPY}'"
  echo "            EMACS = '${EMACS}'"
  echo "          HEADERS = '${HEADERS}'"
  echo "          OBJECTS = '${OBJECTS}'"
  echo "           SOURCE = '${SOURCE}'"
  echo "           SETOWN = '${SETOWN}'"
  echo "         TARGETED = '${TARGETED}'"
  echo "          MANUALS = '${MANUALS}'"
  echo "         EXAMPLES = '${EXAMPLES}'"
  echo "              WEB = '${WEB}'"
  echo "           TOMCAT = '${TOMCAT}'"
  echo "          FILEDIR = '${FILEDIR}'"
  echo "        SOURCEDIR = '${SOURCEDIR}'"
  echo "            MEDIA = '${MEDIA}'"
  echo "INSTALL_SOMETHING = '${INSTALL_SOMETHING}'"
  exit 0
fi

#        ***************************
#        *                         *
#        * Extract Data from Media *
#        *                         *
#        ***************************

# Skip this section entirely if we are not installing anything at all.
#
if [ "${INSTALL_SOMETHING}" = "true" ] ; then

  if [ "${OPERATION}" = "new" ] ; then
    echo ""
    echo "===== Installing New IRIS on '${TARGNODE}' rooted at '${IRISROOT}' ====="
    echo ""
    echo "  (This is a complete installation, and will overwrite any IRIS"
    echo "   customization that you may have done already)"
    echo ""

  elif [ "${OPERATION}" = "upgrade" ] ; then
    echo ""
    echo "===== Upgrading IRIS on '${TARGNODE}' rooted at '${IRISROOT}' ====="
    echo ""
    echo "  (This is an incremental upgrade, and will preserve all IRIS"
    echo "   customization that you have done already)"
    echo ""
  fi

# ============================================================
# Always read in the first "installation" record.  You should
# never be executing from this target directory.
#
  THISRECORD="IRIS installation files" ; RECNUM="1"
  CONTENTS="install" ; setSourceFile "install"

  if [ "${OPERATION}" != "" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      if [ "${TARGETED}" = "true" ] ; then
        echo "Extracting ${THISRECORD}..."

	if [ "${NODELETE}" = "false" ] ; then
          TEST=`REMSH cd ${IRISROOT} ';' rm -rf 'install/*' ';' echo '$?' 2>/dev/null`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS} 
        if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi

      else
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`cd ${IRISROOT} ; rm -rf install/* ; echo $?`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi
    else
      MISSINGRECORD
    fi

  else
    SKIPRECORD
  fi

# ============================================================
# Extract files that are needed for upgrades as well as initial
# installations.
#
  THISRECORD="IRIS executables and app-defaults" ; RECNUM="2"
  CONTENTS="bin script" ; setSourceFile "app"

  BIN_STOPLIST="bin/ls|bin/grep|bin/find|bin/login|bin/mail|bin/spell|bin/tar|bin/cp"

  if [ ! "${OPERATION}" = "" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      FILES=""
      if [ "${TARGETED}" = "true" ] ; then
        TEST=`REMSH if test -d ${IRISROOT}/bin ';' then echo okay ';' fi 2>/dev/null`
        if [ "${TEST}" = "okay" ] ; then
          FILES="`REMSH cd ${IRISROOT} ';' find bin -type f -print 2>/dev/null`"
        fi
        echo ${FILES} | egrep ${BIN_STOPLIST} > /dev/null
        if [ "$?" = "0" ] ; then
          echo "ERROR: ${TARGNODE}:${IRISROOT}/bin contains UNIX system files." 1>&2 ; exit 1
        else
	  if [ "${NODELETE}" = "false" ] ; then
            TEST=`REMSH cd ${IRISROOT} ';' rm -rf ${CONTENTS} ';' echo '$?' 2>/dev/null`
            if [ "${TEST}" != "0" ] ; then exit 1 ; fi
          fi
          READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
        fi

      else
        if [ -d ${IRISROOT}/bin ] ; then
          FILES="`cd ${IRISROOT} ; find bin -type f -print`"
        fi
        echo ${FILES} | egrep ${BIN_STOPLIST} > /dev/null
        if [ "$?" = "0" ] ; then
          echo "ERROR: ${IRISROOT}/bin contains UNIX system files." 1>&2 ; exit 1
        else
	  if [ "${NODELETE}" = "false" ] ; then
            TEST=`cd ${IRISROOT} ; rm -rf ${CONTENTS} ; echo $?`
            if [ "${TEST}" != "0" ] ; then exit 1 ; fi
          fi
          READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
        fi
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Skip user and site files if this is an incremental upgrade
#
  THISRECORD="user and site configurations" ; RECNUM="3"
  CONTENTS="config" ; setSourceFile "config"

  if [ "${OPERATION}" = "new" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`REMSH cd ${IRISROOT} ';' rm -rf ${CONTENTS} ';' echo '$?' 2>/dev/null`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`cd ${IRISROOT} ; rm -rf ${CONTENTS} ; echo $?`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Manuals 
#
  THISRECORD="Manuals..." ; RECNUM="4"
  CONTENTS="manuals" ; setSourceFile "man"

  if [ "${MANUALS}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`REMSH cd ${IRISROOT} ';' rm -rf ${CONTENTS} ';' echo '$?' 2>/dev/null`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`cd ${IRISROOT} ; rm -rf ${CONTENTS} ; echo $?`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Adobe Acrobat
#
  THISRECORD="Acrobat Reader..." ; RECNUM="5"
  CONTENTS="acrobat" ; setSourceFile "acroread"

  if [ "${MANUALS}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`REMSH cd ${IRISROOT} ';' rm -rf ${CONTENTS} ';' echo '$?' 2>/dev/null`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`cd ${IRISROOT} ; rm -rf ${CONTENTS} ; echo $?`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Extract EMACS
#
  THISRECORD="EMACS" ; RECNUM="6" ; setSourceFile "emacs"

  if [ "${EMACS}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD} (into ${EMACSDIR})..."

      # Note that on Linux the standard release contains just
      # /usr/local/bin/emacs
      #
      if [ "${TARGETED}" = "true" ] ; then
	if [ "${NODELETE}" = "false" ] ; then
          if [ "$RMACHINE" = 'Linux' ] ; then 
            TEST=`REMSH cd ${EMACSDIR} ';' rm -f emacs ';' echo '$?' 2>/dev/null`
          else
            TEST=`REMSH cd ${EMACSDIR} ';' rm -rf '*' ../bin/emacs ';' echo '$?' 2>/dev/null`
          fi
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | REMSH cd ${EMACSDIR} ';' ${TAR}

      else
	if [ "${NODELETE}" = "false" ] ; then
          if [ "$MACHINE" = 'Linux' ] ; then 
            TEST=`cd ${EMACSDIR} ; rm -f emacs ; echo $?`
          else
            TEST=`cd ${EMACSDIR} ; rm -rf * ../bin/emacs ; echo $?`
          fi
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | ( cd ${EMACSDIR} ; ${TAR} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi
    
# ============================================================
# Optionally extract headers
#
  THISRECORD="headers and public source" ; RECNUM="7"
  CONTENTS="include libs iris rda ts utils" ; setSourceFile "headers"

  if [ "${HEADERS}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi
    
# ============================================================
# Optionally extract source
#
  THISRECORD="source files" ; RECNUM="8"
  CONTENTS="libs iris rda ts utils" ; setSourceFile "source"

  if [ "${SOURCE}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi

    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Optionally extract objects
#
  THISRECORD="objects and machine generated files" ; RECNUM="9"
  CONTENTS="libs iris rda ts utils" ; setSourceFile "objects"

  if [ "${OBJECTS}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    else
      MISSINGRECORD
    fi

  else
    SKIPRECORD
  fi

# ============================================================
# Extract patch and template files that are needed for upgrades as
# well as initial installations.  These are broken off from the
# primary APP section to make the later smaller.
#
  THISRECORD="templates and dt" ; RECNUM="A"
  CONTENTS="dt config_template" ; setSourceFile "tplates"

  if [ ! "${OPERATION}" = "" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`REMSH cd ${IRISROOT} ';' rm -rf ${CONTENTS} ';' echo '$?' 2>/dev/null`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | REMSH cd ${IRISROOT} ';' ${TAR} ${CONTENTS}
      else
	if [ "${NODELETE}" = "false" ] ; then
          TEST=`cd ${IRISROOT} ; rm -rf ${CONTENTS} ; echo $?`
          if [ "${TEST}" != "0" ] ; then exit 1 ; fi
        fi
        READFILE 2>/dev/null | ( cd ${IRISROOT} ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Optionally Webserver
# Tomcat needs to be installed first before IRIS/Web can be installed.
#
  THISRECORD="IRIS Web server" ; RECNUM="B"
  CONTENTS="" ; setSourceFile "web"

  if [ "${WEB}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
        READFILE 2>/dev/null | REMSH cd / ';' ${TAR} ${CONTENTS}
      else
        READFILE 2>/dev/null | ( cd / ; ${TAR} ${CONTENTS} )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi

# ============================================================
# Optionally Tomcat servlet container.  Used with IRIS/Web.
# Tomcat needs to be installed first before IRIS/Web can be installed.
# When installing a new version of jakarta-tomcat you will need to
# change the symbolic link
#
  THISRECORD="Tomcat " ; RECNUM="C"
  CONTENTS="" ; setSourceFile "tomcat"

  if [ "${TOMCAT}" = "true" ] ; then
    if [ "${COPY}" = "true" -o -r "${SOURCEFILE}" ] ; then
      echo "Extracting ${THISRECORD}..."

      if [ "${TARGETED}" = "true" ] ; then
        READFILE 2>/dev/null | REMSH cd /opt ';' ${TAR} ${CONTENTS} ';' ln -s jakarta-tomcat-5.0.28 tomcat
      else
        READFILE 2>/dev/null | ( cd /opt ; ${TAR} ${CONTENTS} ; ln -s jakarta-tomcat-5.0.28 tomcat )
      fi

      if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
    
    else
      MISSINGRECORD
    fi
  else
    SKIPRECORD
  fi
    
# ============================================================
# Optionally extract examples (cdrom only) 
#
  THISRECORD="example product files" ; RECNUM="X"
  CONTENTS=""

  if [ "${EXAMPLES}" = "true" ] ; then
    echo "Extracting ${THISRECORD}..."

    setSourceFile "prod"

    if [ "${TARGETED}" = "true" ] ; then
      READFILE 2>/dev/null | REMSH cd ${PRODUCT_DIR} ';' ${TAR} ${CONTENTS}
    else
      READFILE 2>/dev/null | ( cd ${TMP_DIR} ; ${TAR} ${CONTENTS} )
      find ${TMP_DIR} -type f -exec chown operator {} ';'
      find ${TMP_DIR} -type f -exec chgrp ${GROUP} {} ';'
      find ${TMP_DIR} -type f -exec chmod ug+w,ugo+r {} ';'
      mv ${TMP_DIR}/* ${PRODUCT_DIR} 
      rm -rf ${TMP_DIR}/* 
    fi
     
    setSourceFile "prod_raw"

    if [ "${TARGETED}" = "true" ] ; then
      READFILE 2>/dev/null | REMSH cd ${PRODUCT_RAW_DIR} ';' ${TAR} ${CONTENTS}
      REMSH mv ${TMP_DIR}"/*" ${PRODUCT_RAW_DIR}
      REMSH rm -rf ${TMP_DIR}
    else
      READFILE 2>/dev/null | ( cd ${TMP_DIR} ; ${TAR} ${CONTENTS} )
      find ${TMP_DIR} -type f -exec chown operator {} ';'
      find ${TMP_DIR} -type f -exec chgrp ${GROUP} {} ';'
      find ${TMP_DIR} -type f -exec chmod ug+w,ugo+r {} ';'
      mv ${TMP_DIR}/* ${PRODUCT_RAW_DIR}
      rm -rf ${TMP_DIR}
    fi
   
    if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi

  else
    SKIPRECORD
  fi

# ============================================================
# All done
#

fi # Ends overall test on media usage...


#        *********************************
#        *                               *
#        * Set File Ownerships and Modes *
#        *                               *
#        *********************************

# This procedure will be executed whenever we do a new or upgrade
# installation, or when it is explicitly asked for on the command
# line.

if [ "${SETOWN}" = "true" ] ; then
  echo "Setting file ownerships and modes..."

  # If this is a targeted install, then try to execute "instiris -setown" as
  # root on the remote system.
  #
  if [ "${TARGETED}" = "true" ] ; then
    TEST1=`REMSH "/usr/local/bin/sudo ${IRISROOT}/install/instiris -setown -root ${IRISROOT}" 2>/dev/null`
    TEST2=`echo ${TEST1} | grep "Setting file ownerships"`
    if [ "${TEST2}" = "" ] ; then
      echo ""
      echo "Can not set file ownerships on ${TARGNODE}."
      echo "To finish installation, login to ${TARGNODE} as root and type:"
      echo ""
      echo "# ${IRISROOT}/install/instiris -setown -root ${IRISROOT}"
    fi

  else

    # Put the sudo stuff in the system directories unless its already there.
    #
    if [ ! -d /usr/local/bin ] ; then 
      mkdir /usr/local/bin 1>/dev/null 2>/dev/null
    fi  
    if [ ! -f /usr/local/bin/sudo ] ; then
      cp -f ${IRISROOT}/config_template/extras/sudo /usr/local/bin/sudo 1>/dev/null 2>/dev/null
      chmod 4111 /usr/local/bin/sudo 1>/dev/null 2>/dev/null
      chgrp 0 /usr/local/bin/sudo 1>/dev/null 2>/dev/null
    fi 
    if [ ! -f /etc/sudoers ] ; then
      cp -f ${IRISROOT}/config_template/extras/sudoers /etc/sudoers 1>/dev/null 2>/dev/null
      echo "Cmnd_Alias  INSTIRIS = ${IRISROOT}/install/instiris" >> /etc/sudoers 2>/dev/null 
      echo "Cmnd_Alias  SIGBRU   = ${IRISROOT}/bin/sigbru"       >> /etc/sudoers 2>/dev/null 
      echo "operator  ALL = NOPASSWD: INSTIRIS, SIGBRU"          >> /etc/sudoers 2>/dev/null
      chmod 440 /etc/sudoers 1>/dev/null 2>/dev/null
      chgrp 0 /etc/sudoers 1>/dev/null 2>/dev/null
    fi

# ------------------------------
# Make sure that the IRIS root directory has correct permissions
#
    echo "   Setting ${IRISROOT} root directory..."

    chown operator ${IRISROOT}
    chgrp ${GROUP} ${IRISROOT}
    chmod 775      ${IRISROOT}

# ------------------------------
# Set owners, groups, and modes appropriately for everything in the
# main installation tree.
#
    DIRS="acrobat bin config config_template dt include install \
          iris libs manuals rda script ts utils"

    for ITEM in ${DIRS} ; do

      # Must be an existing directory 
      #
      if [ -d ${IRISROOT}/${ITEM} ] ; then 

        # Must not be symbolic link
        #
        if [ ! -h ${IRISROOT}/${ITEM} ] ; then
          if [ "${VERBOSE}" = "v" ] ; then echo "   Scanning $ITEM..." ; fi

	  # Set default owner and group in all cases
	  #
          cd ${IRISROOT}
          find ${ITEM} -exec chown operator {} ';'
          find ${ITEM} -exec chgrp ${GROUP} {} ';'

          # Set default modes in all directories except for those
          # which we assume had better be right from the start.
	  #
	  case ${ITEM} in
	    install|iris|libs|rda|ts|utils) ;;
	    *)
	      DIRSTOP="^bin/rda|^bin/keys"
	      find ${ITEM} -type f | egrep -v "${DIRSTOP}" | xargs chmod 664
	      find ${ITEM} -type d | egrep -v "${DIRSTOP}" | xargs chmod 775
	      ;;
	  esac

          # ------------------------
          # Set x bits back on for config/pipes and for init files
          #
	  if [ "${ITEM}" = "config" ] ; then 
            cd ${IRISROOT}/${ITEM}
	    for INITFILE in *_init ; do
              if [ "${INITFILE}" != "*_init" ] ; then chmod +x ${INITFILE} ; fi
	    done
            if [ -d "pipes" ] ; then
              cd pipes ; chmod +x *
            fi
          fi

          # ------------------------
	  # Set x bits back on for config_template/pipes.  This is
	  # because these may be used in symbolic links.  Also for
	  # config_template/init/DiffHelper, config_template/overlay/cphelper and
	  # config_template/extras.
          #
	  if [ "${ITEM}" = "config_template" ] ; then 
            cd ${IRISROOT}/${ITEM}
            if [ -d "pipes" ] ; then
              cd pipes ; chmod +x *
            fi
            cd ${IRISROOT}/${ITEM}
            if [ -d "init" ] ; then
              cd init ; chmod +x DiffHelper
            fi
            cd ${IRISROOT}/${ITEM}
            if [ -d "overlay" ] ; then
              cd overlay ; chmod +x cphelper
            fi
            cd ${IRISROOT}/${ITEM}
            if [ -d "extras" ] ; then
              cd extras ; chmod +x *
            fi
          fi

          # ------------------------
          # Set x bits back on for dt/appmanager files
          #
	  if [ "${ITEM}" = "dt" ] ; then 
            cd ${IRISROOT}/${ITEM}
            if [ -d "appmanager" ] ; then
              cd appmanager ; chmod +x *
            fi
          fi

          # ------------------------
          # Set x bits back on for Acrobat Reader
          #
	  if [ "${ITEM}" = "acrobat" ] ; then 
            cd ${IRISROOT}/${ITEM}
            if [ "$MACHINE" = 'HP-UX' ] ; then 
              chmod +x Browsers/netscape Browsers/hppahpux/nppdf.so \
                Reader/hppahpux/bin/acroread Reader/hppahpux/lib/libAGM.sl \
                Reader/hppahpux/lib/libCoolType.sl Reader/hppahpux/lib/libICC.sl \
                Reader/hppahpux/lib/libreadcore.sl Reader/hppahpux/plug_ins/AcroForm.api \
                Reader/hppahpux/plug_ins/Highlight.api Reader/hppahpux/plug_ins/ewh.api \
                Reader/hppahpux/plug_ins/wwwlink.api Reader/hppahpux/plug_ins/autoIndx.api \
                Reader/hppahpux/plug_ins/search.api bin/acroread
            elif [ "$MACHINE" = 'IRIX' ] ; then
              chmod +x bin/acroread Reader/mipsirix/bin/acroread \
                Reader/mipsirix/lib/libAGM.so Reader/mipsirix/lib/libreadcore.so.4.0 \
                Reader/mipsirix/lib/libICC.so.1.0 Reader/mipsirix/lib/libAGM.so.3.0 \
                Reader/mipsirix/lib/libCoolType.so.1.0 Reader/mipsirix/lib/libICC.so \
                Reader/mipsirix/lib/libCoolType.so Reader/mipsirix/lib/libreadcore.so \
                Reader/mipsirix/plug_ins/Highlight.api Reader/mipsirix/plug_ins/wwwlink.api \
                Reader/mipsirix/plug_ins/search.api Reader/mipsirix/plug_ins/ewh.api \
                Reader/mipsirix/plug_ins/AcroForm.api Reader/mipsirix/plug_ins/autoIndx.api \
                Browsers/netscape Browsers/mipsirix/nppdf.so
            else
              chmod +x Browsers/netscape Browsers/intellinux/nppdf.so \
                Reader/intellinux/bin/acroread Reader/intellinux/lib/libAGM.so \
                Reader/intellinux/lib/libAGM.so.3.0 Reader/intellinux/lib/libreadcore.so \
                Reader/intellinux/lib/libreadcore.so.4.0 Reader/intellinux/lib/libCoolType.so \
                Reader/intellinux/lib/libCoolType.so.1.0 Reader/intellinux/lib/libICC.so \
                Reader/intellinux/lib/libICC.so.1.0 Reader/intellinux/plug_ins/Highlight.api \
                Reader/intellinux/plug_ins/ewh.api Reader/intellinux/plug_ins/wwwlink.api \
                Reader/intellinux/plug_ins/AcroForm.api bin/acroread
            fi
	  fi
        fi
      fi
    done

# ------------------------------
# Get modes and owners correct in the "bin" directory.  Note that the
# owner and group have already been set to the defaults for 'operator',
# so we only need to make the changes from there.
#
    if [ "${VERBOSE}" = "v" ] ; then echo "   Setting modes and owners in bin..." ; fi
    cd ${IRISROOT}/bin

    # First set default mode on all executable files to normal
    # execution.  Then, correct any files that must run with
    # set-uid-on-execute.
    #
    chmod +x *

    # This list is for files which run as operator.
    #
    SET_OPER_UIDS="
      gaintest
      ingest
      ingfio
      input
      linkx
      network
      nordrad
      output
      product
      reingest
      ribbuild
      ribsetup
      rtd_nids3_xmt
      rtd_v1_xmt
      rtd_v2_xmt
      server
      show_iris
      show_machine_code
      signal_iris
      speed
      sserver
      tags
      vribbon
      watchdog
      rda/tsswitch
      "
    for ITEM in ${SET_OPER_UIDS} ; do
      if [ -r ${ITEM} ] ; then
        chmod 6775 ${ITEM}
      fi
    done
    unset SET_OPER_UIDS

    # Set mode and ownership on IRIS files that must run as root.
    # This list must match the utils/misc/sigmet_env list.
    #
    SET_ROOT_UIDS="
      ant_rcv
      ant_xmt
      antsim_rcv
      antsim_xmt
      ant_log
      archive
      copy_data_tape
      init_iris_dvd
      init_iris_lda
      init_iris_mo
      init_iris_tape
      init_sigbru_dvd
      iris
      irisnet
      qant
      qiris
      restart_iris
      rda/rcp8 
      rda/rtnice 
      rda/rvp8
      rda/rvp8proc
      rda/tsexport
      rda/tsimport
      rib_rcv
      rib_xmt
      sigrfopen
      siris
      tapex
      "
    for ITEM in ${SET_ROOT_UIDS} ; do
      if [ -r ${ITEM} ] ; then 
        chown root ${ITEM} ; chmod 6775 ${ITEM}
      fi
    done
    unset SET_ROOT_UIDS


    cd ${IRISROOT}/bin/dynamic
    # Now do dynamic libraries.  Must have execute turned on.
    #
    chmod +x *

# Setup modes and ownership of root utilities.  These vary somewhat depending
# on the platform.
#
#  if [ "$MACHINE" = 'IRIX' ] ; then
#    chmod  775 rootcp rootchown rootchgrp rootchmod
#    chmod 6775 IRIX/bin/cp IRIX/bin/chown IRIX/bin/chgrp IRIX/bin/chmod
#    chown root IRIX/bin/cp IRIX/bin/chown IRIX/bin/chgrp IRIX/bin/chmod
#  else
#    chown root rootcp rootchown rootchgrp rootchmod
#    chmod 6775 rootcp rootchown rootchgrp rootchmod
#  fi

# ------------------------------
# Handle special files.
#
    if [ "${VERBOSE}" = "v" ] ; then echo "   Checking special files..." ; fi

    for ITEM in "/dev/gkscsi" "/dev/scsi/gkscsi" ; do
      if [ -c ${ITEM} -o -b ${ITEM} ] ; then
        chown operator ${ITEM}
        chgrp ${GROUP} ${ITEM}
        chmod 664      ${ITEM}
      fi
    done

# ------------------------------
# Make sure library directories exist if the rest of the tree does.
#
    if [ "${VERBOSE}" = "v" ] ; then echo "   Checking library directories..." ; fi

    cd ${IRISROOT}
    for ITEM in "libs" "rda" "ts" ; do
      if [ -d ${ITEM} ] ; then
        if [ ! -d ${ITEM}/lib ] ; then
          mkdir          ${ITEM}/lib
          chown operator ${ITEM}/lib
          chgrp ${GROUP} ${ITEM}/lib
          chmod 775      ${ITEM}/lib
        fi
      fi
    done

# ------------------------------
# Install APP-Defaults.
#
    echo "Installing Application Defaults..."

    REALAPPS=""
    for ITEM in ${IRISAPPS} ; do
      if [ -r ${IRISROOT}/bin/app-defaults/${ITEM} ] ; then
        REALAPPS="${REALAPPS} ${ITEM}"
      fi
    done
    cd ${IRISROOT}/bin/app-defaults ; cp ${REALAPPS} ${X11APPDIR}

    cd ${X11APPDIR} ; chmod 444 ${REALAPPS}
    chown operator ${REALAPPS} ; chgrp ${GROUP} ${REALAPPS}

# ------------------------------
# Creating CDE links
#
    if [ "$MACHINE" = 'HP-UX' ] ; then
      if [ "${VERBOSE}" = "v" ] ; then echo "Setting CDE links..." ; fi
      ln -s -f ${IRISROOT}/dt/types/sigmet.dt /etc/dt/appconfig/types/C/sigmet.dt
    fi

# ------------------------------
# Fix up EMACS, but only if it looks like it has not been done already
#
    if [ -d ${EMACSDIR} ] ; then
      if [ -r ${EMACSDIR}/needsetown ] ; then
        if [ "${VERBOSE}" = "v" ] ; then echo "Setting modes and owners for EMACS..." ; fi

	if [ "${EMACSDIR}" != "/usr/local/bin" ] ; then
          cd ${EMACSDIR}
          find . ../bin/emacs -exec chown operator {} ';'
          find . ../bin/emacs -exec chgrp ${GROUP} {} ';'
	fi
        rm -f ${EMACSDIR}/needsetown

      else
        echo "EMACS already setup (no work)..."
      fi
    fi
  fi
fi # Ends test on setown
