#!/bin/ksh
# *****************************************
# *                                       *
# * Prepare for Making IRIS Release Tapes *
# *                                       *
# *****************************************
# Copyright (c) 1997, 1999, 2000, 2003, 2004
# File: install/prepare

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

TMP=/tmp/$$_tmp
trap 'rm -f ${TMP} ; exit 0' INT EXIT

# ==============================
# Check IRIS_ROOT environment variable
#
if [ ! -r ${IRIS_ROOT}/install/prepare ] ; then
  echo "The 'IRIS_ROOT' env variable is not set properly" 1>&2 ; exit 1
fi

# ==============================
# Set specific values for the run from this machine
#
MACHINE="`uname -s`"
HOSTNAME=`hostname` ; HOSTNAME="${HOSTNAME%%.*}"

if [ "$MACHINE" = 'HP-UX' ] ; then
  alias REMSH="remsh"
  GROUP="users"
elif [ "$MACHINE" = 'IRIX' ] ; then
  alias REMSH="rsh"
  GROUP="user"
elif [ "$MACHINE" = 'Linux' ] ; then
  alias REMSH="rsh"
  GROUP="users"
else
  echo "Unknown Machine: '${MACHINE}'" 1>&2 ; exit 1
fi

PREPARE=${IRIS_ROOT}/install/prepare

#ROOTCHOWN=${IRIS_ROOT}/bin/rootchown
#ROOTCHGRP=${IRIS_ROOT}/bin/rootchgrp
#ROOTCHMOD=${IRIS_ROOT}/bin/rootchmod

IRISAPPDIR=${IRIS_ROOT}/bin/app-defaults

# Default style
STYLE=$1
if [ "$1" = "" ] ; then
    STYLE="-iris"
fi

# ================================================================================
# Check style for valid options
# ================================================================================

case ${STYLE} in
    -iris|-rda) ;;
    *)
    echo "Unknown argument '$1', try:"
    echo "  prepare -iris"
    echo "  prepare -rda"
    exit 1
	;;
esac

if [ ${STYLE} = "-iris" ] ; then
# The IRIS master reference system is iris-rel.  All machines
# reference that, except iris-rel will reference snow.
#
    if [ ${HOSTNAME} = "iris-rel"  ] ; then
	NTARG1="snow    " ; TARG1=/nfs/snow/${IRIS_ROOT}
    else
	NTARG1="iris-rel" ; TARG1=/nfs/iris-rel/${IRIS_ROOT}
    fi
else
# The RDA master reference system is rvp8-rel.  All machines
# reference that, except rvp8-rel will reference rvp8.
#
    if [ ${HOSTNAME} = "rvp8-rel"  ] ; then
	NTARG1="rvp8    " ; TARG1=/nfs/rvp8/${IRIS_ROOT}
    else
	NTARG1="rvp8-rel" ; TARG1=/nfs/rvp8-rel/${IRIS_ROOT}
    fi

fi

IRISDIRS="./config ./include ./install ./iris ./libs ./script ./utils"

if [ "$2" = "" ] ; then

  # ========================================
  # Copy app-defaults from working location to tape location.  Make sure
  # that all utilities/applications are copied here.
  #
  echo "Making local copies of app-defaults..."
  cd ${X11APPDIR}
  if [ ${STYLE} = "-iris" ] ; then
      cp ${IRISAPPS} ${IRISAPPDIR}
  else
      cp ${RDAAPPS} ${IRISAPPDIR}
  fi

  if [ ${STYLE} = "-iris" ] ; then
  # ========================================
  # Copy menu files to the template directory.  Create the directory if is
  # is not there already.
  #
      if [ ! -r "${IRIS_ROOT}/config_template/menu" ] ; then
	  echo "Creating menu templates directory."
	  mkdir ${IRIS_ROOT}/config_template/menu
      fi
      
      echo "Copying DEFAULT menus to template directory..."
      rm -rf ${IRIS_ROOT}/config_template/menu/*
      cp ${IRIS_ROOT}/config/menu/DEFAULT.* ${IRIS_ROOT}/config_template/menu/
      cp ${IRIS_ROOT}/config/menu/Default.* ${IRIS_ROOT}/config_template/menu/
      chown operator ${IRIS_ROOT}/config_template/menu/*.*
      chgrp ${GROUP} ${IRIS_ROOT}/config_template/menu/*.*

  fi

  # ========================================
  # Copy pipe files to the template directory.  Create the directory if is
  # is not there already.
  #
  if [ ${STYLE} = "-iris" ] ; then
      if [ ! -r "${IRIS_ROOT}/config_template/pipes" ] ; then
	  echo "Creating pipe templates directory."
	  mkdir ${IRIS_ROOT}/config_template/pipes
      fi
      
      echo "Copying pipe to template directory..."
      rm -rf ${IRIS_ROOT}/config_template/pipes/*
      cp -p ${IRIS_ROOT}/config/pipes/* ${IRIS_ROOT}/config_template/pipes/
      chown operator ${IRIS_ROOT}/config_template/pipes/*
      chgrp ${GROUP} ${IRIS_ROOT}/config_template/pipes/*
  fi
  # ========================================
  # Make sure that the "C" version of the message catalogs exists
  #
  echo "Checking message catalogs..."
  if [ ! -r "${IRIS_ROOT}/bin/nls/C" ] ; then
    echo "  Missing C message catalog '${IRIS_ROOT}/bin/nls/C'"
  fi

  # ========================================
  # Remove any junk
  #
  echo "Removing spurious files..."
  cd ${IRIS_ROOT}
  find ./bin ${IRISDIRS} -type f \
    \( \
       -name '.*'     -o \
       -name '*~'     -o \
       -name '*.bak'  -o \
       -name '*.tmp'  -o \
       -name 'core'   -o \
       -name '#*#'       \
    \) -print -exec rm -f {} \;

  # ========================================
  # Check that the -setown option to INSTALL_IRIS leaves the files in the
  # same state that the makefiles initially create them.  This assures that
  # the customer's directory state will be the same as the development state
  # that has been tested at SIGMET.
  #
  if [ ! -w / ] ; then
    echo "Skipping '-setown' checks (must be run as 'root')"

  else
    cd ${IRIS_ROOT} ; rm -rf ${TMP}
    echo ""
    echo "Checking consistency of '-setown' installation option..."
    echo "Scanning directories in present form..."
    find bin config -print | sort | xargs ls -ld > ${TMP}

    cd ${IRIS_ROOT}/install
    ./instiris -setown -v

    echo "Comparing initial and final directories..."
    cd ${IRIS_ROOT}
    find bin config -print | sort | xargs ls -ld |
      sdiff -s -w 192 ${TMP} - | egrep -v '^[0-9c,]*$' |
      sed 's/ *\| */\|/' | tr '|' '\012'

    echo ""
  fi

  # ========================================
  # Check to make sure we still have a symbolic link to the manuals,
  # but only if we are not on the source machine,
  #
  if [ ${HOSTNAME} != "haze" ] ; then
    if [ -h ${IRIS_ROOT}/manuals ] ; then
      echo "Viewable manuals are symbolicly linked, OK."
    else
      echo "Manuals directory is not a link, please repair" 1>&2 ; exit 1
    fi
  fi

  # Now make sure that the directory for each manual actually exists.  Do this
  # regardless of which machine we are on.
  #
  for ITEM in \
	      ${IRIS_ROOT}/manuals/IrisInstall.ilcab/instapdf \
              ${IRIS_ROOT}/manuals/IrisProgram.ilcab/irisppdf \
              ${IRIS_ROOT}/manuals/IrisUsers.ilcab/irisupdf   \
              ${IRIS_ROOT}/manuals/IrisUtils.ilcab/irisupdf   \
              ${IRIS_ROOT}/manuals/rvp7_ug.ilcab/rvp7updf     \
              ${IRIS_ROOT}/manuals/rvp8_ug.ilcab/rvp8updf     \
              ${IRIS_ROOT}/manuals/relnotes.ilcab/relnopdf    \
              ${IRIS_ROOT}/manuals/rcp02_ug.ilcab/rcp02pdf    \
              ${IRIS_ROOT}/manuals/rcp8_ug.ilcab/rcp8updf      ; do
    if [ ! -d ${ITEM} ] ; then
      echo "Manuals directory '${ITEM}' is not readable'" 1>&2 ; exit 1
    fi
  done

  # ========================================
  # Verify that all IRIS trees look pretty much the same
  #
  echo "Cross-checking all development systems..."

  echo "  Comparing overall directory structures..."
  ${PREPARE} ${STYLE} ALL_PAIRS TREECOMPARE

  echo "  Comparing './bin' directory..."
  ${PREPARE} ${STYLE} ALL_PAIRS BINCOMPARE

  echo "  Comparing './bin/keys' directory..."
  ${PREPARE} ${STYLE} ALL_PAIRS KEYCOMPARE

  echo "  Comparing './bin/rda' directory..."
  ${PREPARE} ${STYLE} ALL_PAIRS RDACOMPARE


# ================================================================================
# Internal routines called by prepare script itself
# ================================================================================

elif [ "$2" = "ALL_PAIRS" ] ; then
  ${PREPARE} ${STYLE} $3 "LOCAL" ${IRIS_ROOT} "${NTARG1}" ${TARG1}

  ${PREPARE} ${STYLE} $3 "${NTARG1}" ${TARG1} "LOCAL" ${IRIS_ROOT}

elif [ "$2" = "BINCOMPARE" ] ; then
  cd $4 ; cd bin ;
  for ITEM in * ; do
    if [ ! -r $6/bin/${ITEM} ] ; then
      echo "    In $3 but not in $5 : ${ITEM}"
    fi
  done

elif [ "$2" = "KEYCOMPARE" ] ; then
  cd $4 ; cd bin/keys ;
  for ITEM in * ; do
    if [ ! -r $6/bin/keys/${ITEM} ] ; then
      echo "    In $3 but not in $5 : ${ITEM}"
    fi
  done

elif [ "$2" = "RDACOMPARE" ] ; then
  cd $4 ; cd bin/rda ;
  for ITEM in * ; do
    if [ ! -r $6/bin/rda/${ITEM} ] ; then
      echo "    In $3 but not in $5 : ${ITEM}"
    fi
  done

elif [ "$2" = "TREECOMPARE" ] ; then
  cd $4
  for ITEM in \
    `find ${IRISDIRS} -type d -print` ; do
    if [ ! -d $6/${ITEM} ] ; then
      echo "    In $3 but not in $5 : ${ITEM}"
    fi
  done


else
  echo "Unknown 2nd argument to prepare: '$2'"
fi
