#!/bin/ksh
# *****************************
# *                           *
# * Make an IRIS Release Tape *
# *                           *
# *****************************
#
#  COPYRIGHT (c) 1993, 1995, 1996, 1997, 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.

TMP=/tmp/$$_tmp
TMPX=/tmp/$$_tmpx
TMPY=/tmp/$$_tmpy

trap "rm -f ${TMP} ${TMPX} ${TMPY} /tmp/needsetown; exit" INT EXIT

# WARNING: Manually maintain this code in: make_iris_tape,
# install_iris, ps_iris & sig_uname_filter.  Anything ever run by root
# cannot use sig_uname_filter.
#
MACHINE=`uname -s`

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

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

# ==============================
# Process argument list
#
export TAPE="" # Exported to be visible to tape utilities
DEBUG=""       # Set to 'echo' for debugging

RECLIST="" ; VERBOSE="" ; BIGOBJ="" ; RDARELEASE=""
OPTIONALSOURCE=""

BASICSOURCE=""
for ITEM in libs/misc/structs.c \
            libs/xsig \
            libs/antenna \
            libs/config \
            libs/dsp \
            libs/himath/MapProjInt.c \
            libs/rtq \
            libs/user \
            ts/archfmt \
            ts/export \
            ts/view \
	    utils/antenna \
	    utils/dsp \
            utils/examples \
            utils/nexrad \
            ; do
  if [ -r ${IRIS_ROOT}/${ITEM} ] ; then
    BASICSOURCE="${BASICSOURCE} ${ITEM}"
  fi
done

IRISONLYSRC=""
for ITEM in iris/examiners \
	    libs/nordrad \
	    libs/nordrad2 \
            utils/adids \
            utils/archive2 \
            utils/asterix \
            utils/bufr \
            utils/custom \
	    utils/ewis \
	    utils/grib1 \
            utils/hdf5 \
            utils/nordrad2 \
            utils/pipes_in \
            utils/pipes_out \
            utils/rainbow \
            utils/tdwr \
            utils/ualf \
            utils/uf \
	    ; do
  if [ -r ${IRIS_ROOT}/${ITEM} ] ; then
    IRISONLYSRC="${IRISONLYSRC} ${ITEM}"
  fi
done

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

  if [ "${OPT}" = "-help" ] ; then
    echo "Command Options"
    echo "          -debug : Print commands, but do not use the tape"
    echo "            -rda : Make RDA file set, else IRIS"
    echo "              -v : Verbose"
    echo "    -tape <name> : Use specified tape drive"
    echo "  -stdout <list> : Stream a list of records to standard output"
    echo "     -source <X> : Add file or dir X to source list"
    echo "         -bigobj : Include all machine generated exe/obj files"
    exit 0

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

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

  elif [  "${OPT}" = "-tape" ] ; then
    if [ "${RECLIST}" != "" ] ; then
      echo "Can not specify both -tape and -stdout" 1>&2 ; exit 1
    fi
    if [ "$1" = "" ] ; then
      echo "Missing tape drive name" 1>&2 ; exit 1
    else
      TAPE="$1" ; shift
    fi

  elif [  "${OPT}" = "-stdout" ] ; then
    if [ "${TAPE}" != "" ] ; then
      echo "Can not specify both -tape and -stdout" 1>&2 ; exit 1
    fi
    if [ "$1" = "" ] ; then
      echo "Missing record list for -stdout" 1>&2 ; exit 1
    else
      RECLIST="$1" ; shift
    fi

  elif [  "${OPT}" = "-source" ] ; then
    if [ "$1" = "" ] ; then
      echo "Missing source file specification" 1>&2 ; exit 1
    fi
    if [ "${1##dpolapp*}" != "" -a \
         "${1##iris*}"    != "" -a \
         "${1##libs*}"    != "" -a \
         "${1##rda*}"     != "" -a \
         "${1##ts*}"      != "" -a \
         "${1##utils*}"   != "" ] ; then
      echo "Source must begin w/ 'dpolapp', 'iris', 'libs', 'rda', 'ts', or 'utils'" 1>&2 ; exit 1
    fi
    OPTIONALSOURCE="${OPTIONALSOURCE} $1" ; shift

  elif [  "${OPT}" = "-bigobj" ] ; then
    BIGOBJ="true"

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

  else
    echo "Unknown option: '${OPT}'" 1>&2
    echo "  (Use -help for help)"   1>&2
    exit 1
  fi

done

# ========================================
# Machine specific inits
#
if [ "${RECLIST}" = "" ] ; then

  # If the record list is empty, then we are writing to tape.  Setup
  # the default tape drive if non was specified already, and appropriate
  # 'tar' command options.
  #
  if [ "${MACHINE}" = 'HP-UX' ] ; then
    if [ "${TAPE}" = "" ] ; then TAPE=/dev/rmt/0mn ; fi
    TAR="tar cbf${VERBOSE} 20 ${TAPE}"
  elif [ "${MACHINE}" = 'IRIX' ] ; then
    if [ "${TAPE}" = "" ] ; then TAPE=/dev/nrtape ; fi
    TAR="tar cbf${VERBOSE} 20 ${TAPE}"
  elif [ "${MACHINE}" = 'Linux' ] ; then
    if [ "${TAPE}" = "" ] ; then TAPE=/dev/st0 ; fi
    TAR="tar cbf${VERBOSE} 20 ${TAPE}"
  else
    echo "Unknown Machine: '${MACHINE}'" 1>&2 ; exit 1
  fi

  RECLIST="123456789ABCDEF" ; MESSAGES="true"
  ${DEBUG} mt rewind

else
  # If a record list was specified, then setup for writing to the standard
  # output.
  #
  MESSAGES="false"

  if [ "${MACHINE}" = 'HP-UX' ] ; then
    TAR="tar cbf 20 -"
  elif [ "${MACHINE}" = 'IRIX' ] ; then
    TAR="tar cbf 20 -"
  elif [ "${MACHINE}" = 'Linux' ] ; then
    TAR="tar cbf 20 -"
  else
    echo "Unknown Machine: '${MACHINE}'" 1>&2 ; exit 1
  fi
fi

# ========================================
# Check for inconsistent release options.
#
if [ "${RDARELEASE}" = "true" ] ; 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

# ========================================
# Create filters to generate the different types of release file sets
#
TOPLEVELDIR_FILES="iris/topLevelDir   libs/topLevelDir  rda/topLevelDir
                     ts/topLevelDir  utils/topLevelDir"

  # Note: do not include "rda" or "ts" in RDAONLY.  It is handled
  #       specially a few lines down from here because of problems
  #       with including the topLevelDir files.
  #
RDAONLY="bin/rda              bin/dynamic/libipp   bin/dynamic/librvp8
         bin/dynamic/librcp8  config_template/LINUX/intelipp
        "
RDASTOP="config/dict     config_template/dict
         config/menu     config_template/menu
         config/pipes    config_template/pipes
        "
RDABOTH="bin/dsp       bin/app-defaults/dsp
         bin/ant       bin/app-defaults/ant
         bin/ascope    bin/app-defaults/ascope
         bin/audio     bin/app-defaults/audio
         bin/setup     bin/app-defaults/setup
         bin/bitex     bin/app-defaults/bitex
         bin/color_setup bin/app-defaults/color_setup 
         bin/sigbru    bin/app-defaults/sigbru
         bin/sigterm   bin/app-defaults/sigterm
         bin/manuals   bin/app-defaults/manuals
         bin/rtd       bin/app-defaults/rtd
         bin/zauto7    bin/app-defaults/zauto7  
         bin/zcal      bin/app-defaults/zcal

         bin/compall     bin/errdiag     bin/makedepend  bin/mk_iris_dir 
	 bin/speed       bin/structmap   bin/tags        bin/show_machine_code
	 bin/IRIS        bin/ps_iris     bin/qant        bin/sig_lpstat 
         bin/sigmet_env  bin/sigxbell    bin/sig_uname_filter

         bin/init_sigbru_dvd   bin/sigdvdformat   bin/sigdvdrecord
         bin/gnutar            bin/gnufind

	 bin/dynamic bin/keys bin/nls 

	 libs/lib/libantenna.a      libs/lib/libbxutils.a
	 libs/lib/libconfig.a       libs/lib/libkillconfig.a
         libs/lib/libdsp.a          libs/lib/libdspi.a   
         libs/lib/libfileformats.a  libs/lib/libhimath.a
         libs/lib/libprivate.a      libs/lib/librtq.a     
         libs/lib/libuser.a         libs/lib/libuxsig.a    libs/lib/libXpm.a

	 script dt config include ${TOPLEVELDIR_FILES}

	 ${BASICSOURCE} ${OPTIONALSOURCE}

	 config_template/LINUX      config_template/HPUX  

	 manuals/rcp8.*.ilcab       manuals/rvp8.*.ilcab
         manuals/IrisUtils.*.ilcab  manuals/IrisInstall.*.ilcab  manuals/relnotes.ilcab
         "
RELGREPARGS="" ; RELSTOPARGS=""
if [ "${RDARELEASE}" = "true" ] ; then
  for ITEM in rda ts ${RDAONLY} ${RDABOTH} ; do
    RELGREPARGS="${RELGREPARGS}|^${ITEM}"
  done
  for ITEM in ${RDASTOP} ; do
    RELSTOPARGS="${RELSTOPARGS}|^${ITEM}"
  done
  alias relfilter="egrep -i \"${RELGREPARGS#\|}\" | egrep -vi \"${RELSTOPARGS#\|}\""

else
  for ITEM in ${RDAONLY} ; do
    RELSTOPARGS="${RELSTOPARGS}|^${ITEM}"
  done
  alias relfilter="egrep -v \"/\.|^rda/[^t]|^rda/t[^o]|^rda/to[^p]\" | \
                   egrep -v \"^ts/[^t]|^ts/t[^o]|^ts/to[^p]\" | \
                   egrep -vi \"${RELSTOPARGS#\|}\""
fi

# ========================================
# First record is just the installation files
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}"  = "v"    ] ; then echo "" ; fi
  echo "Writing installation files..."
fi

if [ "${RECLIST##*1*}" = "" ] ; then
  cd ${IRIS_ROOT}
  ${DEBUG} ${TAR} install
  RECLIST="${RECLIST#1}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Executables and configuration files that are required both for
# initial installations and for upgrades.  Note that only the "C"
# language directory is written in the bin/nls directory.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing IRIS executables and app-defaults..."
fi

if [ "${RECLIST##*2*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}
  find bin                   "(" -type f -o -type l ")" -print | \
    egrep -v "bin/nls"  > ${TMP}
  find bin/nls/C             -type f -print                    >> ${TMP}  
  find script                -type f -print                    >> ${TMP}
  ${DEBUG} ${TAR} `sort -u ${TMP} | relfilter`
  RECLIST="${RECLIST#2}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Files that are required for initial installation only.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing user and site configurations..."
fi

if [ "${RECLIST##*3*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}
  find config      -type f -print | egrep -v '^config/menu/' > ${TMP}
  find config/menu -type f -name "DEFAULT.*" -print         >> ${TMP}
  find config/menu -type f -name "Default.*" -print         >> ${TMP}

  # Note that this copies the entire tree below ${IRIS_ROOT}/config,
  # including unwanted files.  This should be improved in the next version.

  # Add some directories that may be empty on the machine making the
  # release and therefore might not exist after installation.
  #
  for DIR in backups bufr images init listings menu overlay pipes ; do
    if [ -d "config/${DIR}/" ] ; then 
      egrep "^config/${DIR}" ${TMP} > /dev/null
      EMPTY=$?
      if [ "1" = "${EMPTY}" ] ; then
        echo "config/${DIR}/" >> ${TMP} ; fi
    fi
  done

  ${DEBUG} ${TAR} `sort -u ${TMP} | relfilter`
  RECLIST="${RECLIST#3}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Manuals and Adobe Acrobat Reader.  Extract all '.pdf' files from the
# various subdirectories of the manuals tree.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing manuals and viewer..."
fi

if [ "${RECLIST##*4*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}

  find manuals/IrisUsers.ilcab/irisupdf/irisug \
       manuals/IrisProgram.ilcab/irisppdf/program \
       manuals/IrisRadar.ilcab/irisrpdf/irisrad \
       manuals/IrisUtils.ilcab/irisupdf/irisutl \
       manuals/IrisInstall.ilcab/instapdf/install \
       manuals/rvp7_ug.ilcab/rvp7updf/rvp7user \
       manuals/rvp8_ug.ilcab/rvp8updf/rvp8user \
       manuals/rcp02_ug.ilcab/rcp02pdf/rcp02 \
       manuals/rcp8_ug.ilcab/rcp8updf/rcp8 \
       manuals/relnotes.ilcab/relnopdf/relnotes \
       -type f -name '*.pdf' >> ${TMP}

  ${DEBUG} ${TAR} `sort -u ${TMP} | relfilter`

  RECLIST="${RECLIST#4}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

if [ "${RECLIST##*5*}" = "" ] ; then
  cd ${IRIS_ROOT} ;

  ${DEBUG} ${TAR} acrobat

  RECLIST="${RECLIST#5}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# EMACS
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing emacs..."
fi

if [ "${RECLIST##*6*}" = "" ] ; then
  rm -f /tmp/needsetown ; cat < /dev/null > /tmp/needsetown

# Ship all of emacs only on SGI IRIX
#
  if [ "${MACHINE}" = 'IRIX' ] ; then
    cd /usr/local/emacs-20.3
    ${DEBUG} ${TAR} . ../bin/emacs -C /tmp ./needsetown
  else
    cd /usr/local/bin
    ${DEBUG} ${TAR} ./emacs -C /tmp ./needsetown
  fi
  RECLIST="${RECLIST#6}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# The source and object area consists of three sections on tape.
# The first and third are always supplied in their entirety; the second
# is filled in according to how much optional source this particular
# customer has purchased.
#
#          First                 Second          Third
#          -----                 ------          -----
#      All other files             .c            .usg
#   (headers, Makefiles,           .C            .cat
#       scripts, etc)              .f            .a
#                                              executables

# ------------------------------

# First Record : Everything except the contents of #2 and #3.
# However, all '.o' files are excluded to save space, the '.a' files
# are included to allow linking.  Also, all .i and .prj files are
# excluded since they are for internal development only.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing headers and public source..."
fi

# Make up a list of files for the first data set, and whittle it down
# by removing the stuff that will end up in the other two. Executable
# files are located by having the 110 permission bits set, but scripts
# are then removed from this list, i.e., retained in the first record,
# by checking for "#!" as the very first two characters.
#
cd ${IRIS_ROOT} ; rm -f ${TMP} ${TMPX} ${TMPY} ;

find include libs iris rda ts utils -type f -print |
  egrep -v "^\.|\.c$|\.C$|\.f$|\.i$|\.prj$|\.a$|\.o$|\.cat$|\.usg$|\.bak$|makefile.d$|\.list" > ${TMP}

for EXE in `find libs iris rda ts utils -type f -perm -110 -print` ; do \
  if [ "#!" != "`dd bs=2 count=1 if=${EXE} 2>/dev/null`" ] ; then \
  echo ${EXE} >> ${TMPX} ; fi ; done

GREPARG=""
for A in `cat ${TMPX}` ; do
  GREPARG="${GREPARG}|^${A}$"
done

if [ "${RECLIST##*7*}" = "" ] ; then
  ${DEBUG} ${TAR} `egrep -v "${GREPARG#\|}" ${TMP} | sort -u | relfilter`
  RECLIST="${RECLIST#7}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ------------------------------
# Second Record : Source files
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing source files ${OPTIONALSOURCE} ..."
fi

rm -f ${TMPY}
find ${TOPLEVELDIR_FILES} ${BASICSOURCE} ${IRISONLYSRC} ${OPTIONALSOURCE} \
  -type f -print | egrep "topLevelDir|\.c$|\.C$|\.f$" > ${TMPY}

if [ "${RECLIST##*8*}" = "" ] ; then
  ${DEBUG} ${TAR} `sort -u < ${TMPY} | relfilter`
  RECLIST="${RECLIST#8}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ------------------------------
# Third Record : Machine generated files
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing machine generated files..."
fi

if [ "${BIGOBJ}" != "true" ] ; then rm -f ${TMPX} ; fi
find ${TOPLEVELDIR_FILES} libs iris rda ts utils -type f -print |
  egrep "topLevelDir|\.a$|\.cat$|\.usg$" >> ${TMPX}

if [ "${RECLIST##*9*}" = "" ] ; then
  ${DEBUG} ${TAR} `sort -u < ${TMPX} | relfilter`
  RECLIST="${RECLIST#9}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Patch files and configuration templates that are required both for
# initial installations and for upgrades.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing patches and templates..."
fi

if [ "${RECLIST##*A*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}

  find config_template       -type f -print        > ${TMP}
  find dt                    -type f -print       >> ${TMP}

  ${DEBUG} ${TAR} `sort -u ${TMP} | relfilter`
  RECLIST="${RECLIST#A}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Webserver files
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing webserver..."
fi

if [ "${RECLIST##*B*}" = "" ] ; then
  cd / ; rm -f ${TMP} ;

  # Add in the rest of the stuff...
  #
  if [ -r var/www/html/weblook ] ; then
    find var/www/html/weblook              -type f -print     >> ${TMP}  
  fi
  if [ -r opt/tomcat/webapps/irisservlets ] ; then
    find opt/tomcat/webapps/irisservlets  -type f -print     >> ${TMP}
  fi

  if [ -r ${TMP} ] ; then ${DEBUG} ${TAR} `sort -u ${TMP} | relfilter` ; fi

  RECLIST="${RECLIST#B}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# All done
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Rewinding and unloading..."
fi
if [ "${TAPE}" != "" ] ; then
  ${DEBUG} mt offl
fi
