#!/bin/ksh
# *********************************************
# *                                           *
# *  Prepare local files for an IRIS Release  *
# *                                           *
# *********************************************
#
#             COPYRIGHT (c) 1996, 2000, 2004, 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.

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

VERSION="`show_machine_code -version | tr -dc '.[0-9]'`"
MACHINE="`uname -s`"

# ------------------------------
# Process argument list
#
DEBUG="" ; VERBOSE="" ; PASSARGS="" ; IRISRELEASE="" ; RDARELEASE="" ; NOMANUALS=""

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

  if [ "${OPT}" = "-help" -o "${OPT}" = "-?" ] ; then
    echo "Command Options"
    echo "          -debug : Print commands, but do not make files"
    echo "           -iris : Make IRIS file set"
    echo "            -rda : Make RDA file set, else IRIS"
    echo "              -v : Verbose progress messages"
    echo "     -source <X> : Add file or dir X to source list"
    echo "         -bigobj : Include all machine generated exe/obj files"
    echo "      -nomanuals : Do not make the 'man.tgz' manuals component"
    exit 0

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

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

  elif [  "${OPT}" = "-nomanuals" ] ; then
    NOMANUALS="true"

  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
    PASSARGS="${PASSARGS} -source $1" ; shift

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

  elif [  "${OPT}" = "-iris" ] ; then
    IRISRELEASE="true"
    if [ "${RDARELEASE}" = "true" ] ; then 
	echo "Cannot supply both -rda and -iris options" 1>&2 ; exit 1
    fi

  elif [  "${OPT}" = "-rda" ] ; then
    PASSARGS="${PASSARGS} -rda" ; RDARELEASE="true"
    if [ "${IRISRELEASE}" = "true" ] ; then 
	echo "Cannot supply both -rda and -iris options" 1>&2 ; exit 1
    fi

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

done

# ------------------------------
# Assign directory names, some of which depend on the input args.
# Verify that the release directory we are about to create does not
# alredy exist.  This prevents clobbering an older release when the
# version numbers have not yet been incremented.
#
IRIS_INS="${IRIS_ROOT}/install/"
IRIS_INC="${IRIS_ROOT}/include/"

if [ "${RDARELEASE}" = "true" ] ; then
    IRIS_REL="${IRIS_ROOT}/releases/rda-${VERSION}"
    IRIS_CUR="${IRIS_ROOT}/releases/rda-current"
else 
    IRIS_REL="${IRIS_ROOT}/releases/iris-${VERSION}"
    IRIS_CUR="${IRIS_ROOT}/releases/iris-current"
fi

if [ -d "${IRIS_REL}" ] ; then
  echo "Sorry, ${IRIS_REL} already exists.  If you really"               1>&2
  echo "  intend to recreate the ${VERSION} release then please confirm" 1>&2
  echo "  this by manually deleting the existing version."               1>&2
  exit 1
fi

if [ "${DEBUG}" = "true" ] ; then echo mkdir ${IRIS_REL} ; echo
                             else      mkdir ${IRIS_REL}
fi

# ------------------------------
# Run make_iris_tape with option to send file to stdout instead of to
# tape.  Then redirect the stdout to a release file and compress.  Do
# for each record.
#
echo "Preparing install files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 1 ${VERBOSE} ${PASSARGS} -debug
else
  ${IRIS_INS}make_iris_tape -stdout 1 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/install
  gzip -S .tgz --best ${IRIS_REL}/install
fi

echo "Preparing application files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 2 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 2 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/app
  gzip -S .tgz --best ${IRIS_REL}/app
fi

echo "Preparing configuration files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 3 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 3 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/config
  gzip -S .tgz --best ${IRIS_REL}/config
fi

if [ "${NOMANUALS}" = "true" ] ; then
  echo "Skipping manual files..."
else
  echo "Preparing manual files..."
  if [ "${DEBUG}" = "true" ] ; then
    ${IRIS_INS}make_iris_tape -stdout 4 ${VERBOSE} ${PASSARGS} -debug
    echo
  else
    ${IRIS_INS}make_iris_tape -stdout 4 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/man
    gzip -S .tgz --best ${IRIS_REL}/man
  fi
  echo "Preparing acroread files..."
  if [ "${DEBUG}" = "true" ] ; then
    ${IRIS_INS}make_iris_tape -stdout 5 ${VERBOSE} ${PASSARGS} -debug
    echo
  else
    ${IRIS_INS}make_iris_tape -stdout 5 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/acroread
    gzip -S .tgz --best ${IRIS_REL}/acroread
  fi
fi

echo "Preparing emacs files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 6 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 6 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/emacs
  gzip -S .tgz --best ${IRIS_REL}/emacs
fi

echo "Preparing header files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 7 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 7 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/headers
  gzip -S .tgz --best ${IRIS_REL}/headers
fi

echo "Preparing source files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 8 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 8 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/source
  gzip -S .tgz --best ${IRIS_REL}/source
fi

echo "Preparing object files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 9 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 9 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/objects
  gzip -S .tgz --best ${IRIS_REL}/objects
fi

echo "Preparing patches and templates..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout A ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout A ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/tplates
  gzip -S .tgz --best ${IRIS_REL}/tplates
fi

if [ "${MACHINE}" = 'Linux' -a "${RDARELEASE}" != "true" -a \
     -d "${IRIS_ROOT}/web" ] ; then
  echo "Preparing Webserver..."
  if [ "${DEBUG}" = "true" ] ; then
    ${IRIS_INS}make_iris_tape -stdout B ${VERBOSE} ${PASSARGS} -debug
    echo
  else
    ${IRIS_INS}make_iris_tape -stdout B ${VERBOSE} ${PASSARGS} > ${IRIS_REL}/web
    gzip -S .tgz --best ${IRIS_REL}/web
  fi
else
  echo "Skipping Webserver..."
fi

echo "Copying install programs and version file..."
if [ "${DEBUG}" = "true" ] ; then
  echo cp ${IRIS_INS}install      ${IRIS_REL}
  echo cp ${IRIS_INS}install.rf   ${IRIS_REL}
  echo cp ${IRIS_INS}instiris     ${IRIS_REL}
  echo cp ${IRIS_INC}sigversion.h ${IRIS_REL}/version
  echo
else
  cp ${IRIS_INS}install      ${IRIS_REL}
  cp ${IRIS_INS}install.rf   ${IRIS_REL}
  cp ${IRIS_INS}instiris     ${IRIS_REL}
  cp ${IRIS_INC}sigversion.h ${IRIS_REL}/version
fi

# Make symbolic link from current
cd ${IRIS_REL%/*}
rm -f ${IRIS_CUR##*/}
ln -s ${IRIS_REL##*/} ${IRIS_CUR##*/}
