#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# sbin/$RCSfile$  $Revision$

# script to cut the encp (client) and enstore (server) products from the enstore cvs sources

# based on copCut which is based almost entirely on Ron R's dscCut

productCut=`basename $0`
echo "$productCut starting at `date`"
thepwd=`pwd`
INSTALLDIR=

##############################################################################
##############################################################################
# utility functions that do what their names imply

# this function would be better if it actually did the command too, this
# would allow you to say "check_status command" rather than "command; check_status $? command.."
# Oh, well... next time
check_status() { jstatus=${1:-0}
                 jname=${2:-unknown}
                 jstep=${Step:-unknown}
                 if [ $jstatus != 0 ] ; then
                   echo ""
                   echo "$productCut Fatal Error=$1 \"$jname\", Step \"$jstep\"" >&2
                   echo "$productCut ABORTED at `date`"
                   cd $thepwd
                   exit $jstatus
                 else
                   if [ -z "${3-}" ] ; then
                      echo "$productCut success for \"$jname\", Step \"$jstep\" at `date`" >&2
                   fi
                 fi ; }

echo_timed_read() { jtime=${1:-10}
                    /bin/sh -c '( sleep '$jtime' ; kill $$ ) >/dev/null 2>&1 &
                    read ans ; echo "$ans"' ;}

starting() { if [ $try != "list" ] ; then
                /bin/echo -n "starting at `date`"
             fi; }

copyit()   { dname=`dirname $2`
             if [ ! -d $dname ]; then
               mkdir -p $dname
               check_status $? "mkdir -p $dname"
             fi
             if [ -f $1 ]; then
               cp $1 $2;
               check_status $? "cp $1 $2"
             fi ;}

plint() { echo $1
          python $GADFLY_GRAMMAR/kjpylint.py $1 | egrep -v "^setup$|^loading$|^now parsing$|ref at .* before assign at|defined before .* not used"
          echo
        }
# we need a place to install the cut product
check_installdir() {
    installdir=$1
    product=$2
    rev=$3
    #setrev=1
    puntUPS=0
    if [ -z "${installdir-}" ] ; then
	Step="installdir"
	case `uname -n` in
	    #airedale*) INSTALLDIR=/home/products/$product;;
	    #pcfarm9*)  INSTALLDIR=/products/$product;;
	    #hppc*)     INSTALLDIR=/usr/products/IRIX+6/$product;;
	    #ripsgi*)   INSTALLDIR=/usr/products/$product;;
	    #rip8*)     INSTALLDIR=/fnal/ups/prd/$product/${REV}/${FLAVOR}
	    #           setrev=0;;
	    #bld*)      INSTALLDIR=/afs/.fnal.gov/ups/encp/${REV}/${FLAVOR}
	    #           setrev=0;;
	    #d0ola*)    INSTALLDIR=$ENSTORE_DIR/$product #this stinks! but I don't know what else to do
	    #           puntUPS=1;;
	    *)         check_status 1 "Don't know where to install ups product $product on `uname -n`";;
	esac
	echo "Install directory not specified - Defaulting to install directory of $installdir"
    fi

#if [ $setrev -eq 1 ]; then
#  INSTALLDIR=${INSTALLDIR}/${REV}
#fi
    if [ -n $product -a -n $rev ]; then
	base_filename=`basename $installdir`
	if [ $base_filename = $rev ]; then
	    #echo $installdir
	    INSTALLDIR=$installdir
	elif [ $base_filename = $product ]; then
	    #echo ${installdir}/${rev}
	    INSTALLDIR=$installdir/${rev}
	else
	    #echo ${installdir}/${product}/${rev}
	    INSTALLDIR=$installdir/${product}/${rev}
	fi
    fi
}

#If the installdir does not have the product and revision appended, tack
# that on the end.
append_installdir() {
    installdir=$1
    product=$2
    rev=$3

    base_filename=`basename installdir`
    if [ $base_filename = $rev ]; then
        echo $installdir
    elif [ $base_filename = $product ]; then
	echo ${installdir}/${rev}
    else
	echo ${installdir}/${product}/${rev}
    fi
}

# we need ups and upd to be setup
check_upsupd() {
if [ -z "${UPS_DIR-}" ] ; then
  Step="ups check"
  check_status 1 "You need ups to be setup before using $productCut"
fi

if [ -z "${UPD_DIR-}" ] ; then
  Step="upd check"
  check_status 1 "You need upd to be setup before using $productCut"
fi
}

##############################################################################

USAGE="

   $productCut -options

-r version   next release version
-i directory install directory (without version number and product) for the new revision (i.e. /p/Linux)
-c chain     to set chain to be <new|old|test|development|current|none>
-s steps     to control which steps get executed,  zB 1,2,4-13
-x           to set -xv
-?           to list this

Steps to the cutting process:
  1.  cvs directory check
  2.  lint check (continues if errors)
  3.  bump encp version number in encp.py
  4.  cd modules - make clean all
  5.  cd src - make clean encp
  6.  cd volume_import - make clean enstore_tape
  7.  cd doc/WWW - make all
  8.  compileall python source
  9.  cvs tag
 10.  install into product area
 11.  ups undeclare
 12.  ups declare
 13.  upd delproduct
 14.  upd addproduct
 15.  ups setup; build config rpm / pgkmk
 16.  rpmbuild / pgkmk
"

STEPSDEFINED=16

echo "$USAGE"

##############################################################################
#Determine the name of the product we are building.

product=`echo $productCut | sed -e 's/Cut//'`

if [ $product = "encp" ]; then
  productmate=enstore
  productUps=""
  productName=encp
elif [ $product = "enstore" ]; then
  productmate=encp
  productUps="/ups"
  productName=enstore
elif [ $product = "source" ]; then
  productmate=encp
  productUps=""
  productName=enstore
else
  check_status 1 "Do not know anything about $productCut"
fi

##############################################################################
# we need enstore to be setup and we need to be in a cvs working directory

if [ -z "${ENSTORE_DIR-}" ] ; then
  Step="enstore check"
  check_status 1 "You need enstore to be setup before using $productCut"
fi
cd $ENSTORE_DIR
check_status $? "cd $ENSTORE_DIR"

##############################################################################
# 
if [ $product = "encp" ]; then
    build_dir=$ENSTORE_DIR/src/ENCPBIN
elif [ $product = "enstore" ]; then
    build_dir=$ENSTORE_DIR/src/ENSTORE_BIN
elif [ $product = "source" ]; then
    build_dir=$ENSTORE_DIR/src/ENSTORE_BIN
else
    check_status 1 "Not defined for $product."
fi
prefix=/opt/$product

##############################################################################
#FLAVOR=`echo $SETUP_ENSTORE | sed -e 's/.*-f //' -e 's/ .*//'`
#FLAVOR=`$UPS_DIR/bin/ups flavor -2`

# Take care of IRIX+6.5, Linux+2.2 and Linux+2.4

FLAVOR=`$UPS_DIR/bin/ups flavor -3`

case ${FLAVOR} in
        IRIX+6.5)
                ;;
	SunOS+5.7)
		;;
	SunOS+5.8)
		;;
	Linux+2.4)
		if [ "`ups flavor -4`" = "Linux+2.4-2.3.2" ]; then
			# Take care of Linux 9.0 and LTS.
			FLAVOR=`$UPS_DIR/bin/ups flavor -4`
                fi
		;;
	Linux+2.6)
		if [ "`ups flavor -4`" = "Linux+2.6-2.5" ]; then
			#Take care of SLF5.
			FLAVOR=`$UPS_DIR/bin/ups flavor -4`
		fi
		;;
	Linux64bit+2.6)
		if [ "`ups flavor -4`" = "Linux+2.6-2.5" ]; then
			# Take care of SLF5.
			FLAVOR=`$UPS_DIR/bin/ups flavor -4`
		fi
		;;
        *)
                FLAVOR=`$UPS_DIR/bin/ups flavor -2`
esac

if [ -d CVS ] ; then
   # who cares any way?
   # Cvs_Module=`expr "\`cat CVS/Repository\`" : ".*/\([^/]*\)"`
   Cvs_Module=`cat CVS/Repository`
fi
if [ "${Cvs_Module:-}" = "" ];then
   check_status 1 "You need to be in a cvs working directory to use $productCut"
fi

##############################################################################
# parse the options on the command line
#### dadburned getopts does not work on OSF1, so parse opts manually############

gotc=
QUALIFIERS=""
opt=""
if [ $# -gt 0 ] ; then
   for optarg in $@ ; do

      case $opt in
         -i) INSTALLDIR=$optarg
             opt=""
             ;;
         -r) REV=$optarg
             opt=""
             ;;
         -c) case $optarg in
               "")  CHAIN="";             ;;
               no*) CHAIN="";             ;;  # no or none
               c*)  CHAIN="${CHAIN-} -c"; ;;  # you can have multiple chains
               d*)  CHAIN="${CHAIN-} -d"; ;;
               n*)  CHAIN="${CHAIN-} -n"; ;;
               o*)  CHAIN="${CHAIN-} -o"; ;;
               t*)  CHAIN="${CHAIN-} -t"; ;;
                *)   Step="chain getopts"; check_status 1 "You have specified an illegal chain";  ;;
             esac
             gotc=1
             opt=""
             ;;
         -q) QUALIFIERS="-q $optarg"
             opt=""
             ;;
         -s) STEPS=`for i in \`echo $optarg | sed -e 's/,/ /g'\` ; do
                if x=\`expr $i : '\(.*\)-'\` ; then
                   y=\`expr $i : '.*-\(.*\)'\`
                   while [ $x -le $y -a $x -le $STEPSDEFINED ] ; do
                     echo $x
                     x=\`expr $x + 1\`
                   done
                else
                   echo $i
                fi
              done | sort -nu`        # do not sort if we want to specify an order or repeating steps (could use uniq instead of sort)
              STEPS="`echo $STEPS`"   # remove lfs (b/c STEPS isnt quoted inside)
              opt=""
              ;;
         "") opt=$optarg;;
         -?) echo "$USAGE"; Step="getopts"; check_status 1 "" ;;
         *)  echo "$USAGE"; Step="getopts"; check_status 1 "Unknown option" ;;
         esac
   done
fi

if [ -n "$opt" ] ; then echo "$USAGE"; Step="getopts"; check_status 1 "Can not parse command line correctly"; fi

echo ""
echo "options have been processed... checking"

##############################################################################
# we need a revision - if not specified, get old one from a cvs tag

if [ -z "${REV-}" ] ; then
   Step="next revision"
   last=`cvs status -v Makefile | grep \(revision: | egrep "$product|$productmate" | head -1  | sed -e 's/^[ ]*//'`
   check_status $? "cvs status -v Makefile"
   if [ -z "${last-}" ] ; then
     echo "revision info not available via cvs - defaulting to v0_0 as last known revision"
     last=${last:-v0_0_Linux}
   fi
   major=`echo $last | awk -F_ '{print $1}'`
   minor=`echo $last | awk -F_ '{print $2}'`
   REV=${major}_`expr $minor + 1`
   check_status $? "determining next rev from $last"
   echo "Revision not specified - Defaulting to revison $REV"
fi
TAG=${REV}_${product}_${FLAVOR}
TAG=`echo ${TAG} | tr "." "_"`

##############################################################################
# we need a place to install the cut product
#check_installdir $INSTALLDIR

##############################################################################
# we need to know what steps we are supposed to perform during the cut

if [ -z "${STEPS-}" ] ; then
   STEPS=1
   s=2
   while [ $s -le $STEPSDEFINED ] ; do STEPS="${STEPS-} $s"; s=`expr $s + 1`; done
   echo "Cut steps not specified - Defaulting to all steps: $STEPS"
fi
STEPSOK=""
for step in $STEPS; do
   if [ $step -le $STEPSDEFINED -a $step -ge 1 ] ; then
      if [ -z "${STEPSOK-}" ] ; then
        STEPSOK=$step
      else
        STEPSOK="$STEPSOK $step"
      fi
   fi
done
if [ -z "${STEPSOK-}" ] ; then
  Step="checksteps"
  check_status 1 "No steps defined, how odd"
fi
STEPS=$STEPSOK
STEPS="$STEPS done" # done is flag that we are finished
save_STEPS=$STEPS

##############################################################################
# we need to know what chain we are cutting

if [ -z "${gotc-}" ] ; then
   CHAIN=" -c"
   echo "UPS/UPD chain not specified - Defaulting to chain $CHAIN"
fi

##############################################################################

#For printing output to the terminal only...
if [ -z $INSTALLDIR ]; then
   use_INSTALLDIR="<not specified>"
else
   use_INSTALLDIR=$INSTALLDIR
fi

echo ""
echo "Using files in CVS $Cvs_Module working directory `pwd` to cut $product"
echo "  $REV and ups/upd declaring it as ($CHAIN) into directory"
echo "  $use_INSTALLDIR and using steps $save_STEPS."

##############################################################################

for try in list doit; do
   echo ""
   STEPS=$save_STEPS
   while [ "$STEPS" != "done" ] ; do
      Step="`expr \"$STEPS\" : '\([^ ]*\) .*'`"
      STEPS="`expr \"$STEPS\" : '[^ ]* \(.*\)'`"
      case $Step in

        1) echo "Step $Step:   cvs directory check for `pwd`    `starting`"
           if [ "$try" = "doit" ] ; then
              cd $ENSTORE_DIR
              check_status $? "cd $ENSTORE_DIR"
              cvs_nq_update=`cvs -nq update 2>&1`
              check_status $? "cvs -nq update: $cvs_nq_update"
              if echo "$cvs_nq_update" | fgrep '[server aborted]' >/dev/null ; then
                check_status 1 "server aborted problems with cvs: $cvs_nq_update"
              elif x="`echo \"$cvs_nq_update\"|grep '^M'`"   ; then
               check_status 1 "$x: Modified or unknown files ($cvs_nq_update) in working directory - update/commit needed first"
              elif x="`echo \"$cvs_nq_update\"|grep '^[PU]'`"; then
               check_status 1 "$x: Update/Patched files ($cvs_nq_update) in repository - update/commit needed first"
              fi
           fi
           ;;

        2) echo "Step $Step:   lint the python source   `starting`"
           if [ "$try" = "doit" ] ; then
              pyfiles=/tmp/$productCut.pyfiles
              find $ENSTORE_DIR/src -name "*.py" >$pyfiles
              cat $pyfiles | while read pfile; do
                 $ENSTORE_DIR/tools/mylint.py $pfile
                 check_status $? "$ENSTORE_DIR/tools/mylint.py $pfile"
		 $ENSTORE_DIR/tools/check_pct.py -w $pfile
                 check_status $? "$ENSTORE_DIR/tools/check_pct.py -w $pfile"
              done
              rm $pyfiles
           fi
           ;;

        3) echo "Step $Step:   bump encp version number to $REV in encp.py   `starting`"
           if [ "$try" = "doit" ] ; then
              cd $ENSTORE_DIR/src
              check_status $? "cd $ENSTORE_DIR/src"
              cvs update -A encp.py
              check_status $? "cvs update -A encp.py"
              encpnew=/tmp/$productCut.encpnew
              sed -e "s+version_string = \".*  CVS +version_string = \"$REV  CVS +" $ENSTORE_DIR/src/encp.py > $encpnew
              check_status $? "sed bump version to $REV"
              diff $ENSTORE_DIR/src/encp.py $encpnew
              cp $encpnew $ENSTORE_DIR/src/encp.py
              check_status $? "mv $encpnew $ENSTORE_DIR/src/encp.py"
              rm -f $encpnew
              cvs commit -m "bumping version to $REV because of $productCut" encp.py
              check_status $? "cvs commit of encp.py"
              cvs tag -F production encp.py
              check_status $? "cvs tag -F production encp.py"
           fi
           ;;

        4) echo "Step $Step:   cd modules and make clean all   `starting`"
           if [ "$try" = "doit" ] ; then
              cd $ENSTORE_DIR/modules
              check_status $? "cd $ENSTORE_DIR/modules"
              make clean all
              check_status $? "make clean all"
           fi
           ;;

        5) echo "Step $Step:   cd src and make clean $product    `starting`"
           if [ "$try" = "doit" ] ; then
              cd $ENSTORE_DIR/src
              check_status $? "cd $ENSTORE_DIR/src"
	      #If the 'dcache' qualifier was specified do a slightly
	      # different build.
	      echo "$QUALIFIERS" | tr ": " "\n" | grep dcache > /dev/null
	      if [ $? -eq 0 ]; then
		make clean ${product}_for_dcache
		check_status $? "make clean ${product}_for_dcache"
	      else
		make clean $product
		check_status $? "make clean $product"
	      fi
           fi
           ;;

        6) echo "Step $Step:   cd volume_import and make clean enstore_tape    `starting`"
           if [ "$try" = "doit" ] ; then
              cd $ENSTORE_DIR/volume_import
              check_status $? "cd $ENSTORE_DIR/volume_import"
              make clean enstore_tape
              check_status $? "make clean enstore_tape"
           fi
           ;;

        7) echo "Step $Step:   cd doc/WWW - make all    `starting`"
           if [ "$try" = "doit" ] ; then
              if [ $product = "enstore" ]; then
                 cd $ENSTORE_DIR/doc
		 check_status $? "cd $ENSTORE_DIR/doc"
                 make clean all
                 check_status $? "docs make clean all"
              else
                 echo "No make needed for documentation of encp"
              fi
           fi
           ;;

        8) echo "Step $Step:   compileall python source   `starting`"
           if [ "$try" = "doit" ] ; then
             find $ENSTORE_DIR -name "*\.pyc" | xargs rm
             check_status $? "rm of old pyc files"
             compileall_output=/tmp/$productCut.compileall_output
             python -c '
import os,compileall
compileall.compile_dir(os.environ["ENSTORE_DIR"])
' 2>&1 | tee $compileall_output
             check_status $? "compileall python source"
             if [ `grep -c "SyntaxError" $compileall_output` -ne 0 ]; then
               check_status 1 "compile error - see $compileall_output"
             else
               rm -f $compileall_output
             fi
           fi
           ;;

        9) echo "Step $Step:   cvs tag -F $TAG    `starting`"
           if [ "$try" = "doit" ] ; then
              cd $ENSTORE_DIR
              check_status $? "cd $ENSTORE_DIR"
              cvs tag -F $TAG
              check_status $? "cvs tag -F $TAG"
           fi
           ;;

       10) echo "Step $Step:   install $product into $INSTALLDIR    `starting`"
           if [ "$try" = "doit" ] ; then

	      check_installdir "$INSTALLDIR" $product $REV

              # check if directory doesn't exist
              if [ ! -d $INSTALLDIR ] ; then
                 echo "$INSTALLDIR does not exist... making it"
                 mkdir -p $INSTALLDIR
                 check_status $? "mkdir -p $INSTALLDIR"
              else
		 chmod -R u+wx $INSTALLDIR
		 check_status $? "chmod -R u+xw $INSTALLDIR"
                 rm -fr $INSTALLDIR
                 check_status $? "protections bad??  rm -fr $INSTALLDIR"
		 mkdir -p $INSTALLDIR
		 check_status $? "mkdir -p $INSTALLDIR"
              fi

	      if [ $product = "encp" ]; then
		cp -r $build_dir/$prefix/* $INSTALLDIR/
		check_status $? "cp -r $ENSTORE_DIR/src/ENCPBIN/* $INSTALLDIR/"
	      elif [ $product = "enstore" ]; then
	        cp -r $build_dir/$prefix/* $INSTALLDIR/
		check_status $? " cp -r $ENSTORE_DIR/src/ENSTORE_BIN/* $INSTALLDIR/"
              elif [ $product = "encp" -o $product = 'encp_for_dcache' ]; then

                mkdir -m 755 $INSTALLDIR/volume_import
		chmod u+rwx $INSTALLDIR/volume_import
                copyit $ENSTORE_DIR/src/ENCPBIN/encp            $INSTALLDIR/encp
                copyit $ENSTORE_DIR/src/ENCPBIN/ensync          $INSTALLDIR/ensync
                copyit $ENSTORE_DIR/src/ENCPBIN/enstore         $INSTALLDIR/enstore
                copyit $ENSTORE_DIR/src/ENCPBIN/enroute2        $INSTALLDIR/enroute2
                copyit $ENSTORE_DIR/src/ENCPBIN/ddencp          $INSTALLDIR/ddencp
		copyit $ENSTORE_DIR/src/ENCPBIN/ecrc		$INSTALLDIR/ecrc
		copyit $ENSTORE_DIR/src/ENCPBIN/en_check	$INSTALLDIR/en_check
# So far, make setuid installation manual
#		chmod u+s $INSTALLDIR/enroute2
#               copyit $ENSTORE_DIR/src/ENCPBIN/enmonitor       $INSTALLDIR/enmonitor
                copyit $ENSTORE_DIR/volume_import/enstore_tape  $INSTALLDIR/enstore_tape
		cp     $ENSTORE_DIR/volume_import/*.c           $INSTALLDIR/volume_import/
		cp     $ENSTORE_DIR/volume_import/*.h           $INSTALLDIR/volume_import/
		copyit $ENSTORE_DIR/volume_import/Makefile      $INSTALLDIR/volume_import/
                copyit $ENSTORE_DIR/sbin/EPS                    $INSTALLDIR/EPS
                copyit $ENSTORE_DIR/ups/encp.table              $INSTALLDIR/encp.table
                copyit $ENSTORE_DIR/ups/encp_mni_install.sh     $INSTALLDIR/encp_mni_install.sh
                copyit $ENSTORE_DIR/ups/chooseConfig            $INSTALLDIR/chooseConfig
		copyit $ENSTORE_DIR/src/e_errors.py             $INSTALLDIR/e_errors.py
                manifest=$INSTALLDIR/.encpCut.manifest

              elif [ $product = "enstore" ]; then

                copylist=/tmp/$productCut.copylist
                find $ENSTORE_DIR/ -type f| egrep -v "/CVS|/templates|\.o$|\~$|\.#|.cvsignore|/test/[0-9]+|/test/data|core|enstore_variables.table">$copylist
                check_status $? "find files > $copylist"
                if [ -z "${cvs_nq_update-}"]; then
                  cvs_nq_update=`cvs -nq update 2>&1`
                  check_status $? "cvs -nq update"
                fi
                cat $copylist | while read from; do
                    to=`echo $from | sed -e "s#^$ENSTORE_DIR/##"`
                    if [ "$to" = "$from" ]; then
                       check_status 1 "can't get to from from $to $from"
                    fi
                    if x="`echo \"$cvs_nq_update\"|grep \"^? $to\"`"; then
                        echo "Skipping $to because it is not in the repository"
                    else
                      copyit $from $INSTALLDIR/$to
                    fi
                done
                rm -f $copylist
                cd $INSTALLDIR/ups
                check_status $? "cd $INSTALLDIR/ups"
                ln -s sam.table enstore_variables.table
                check_status $? "ln -s sam.table enstore_variables.table"
                cd $INSTALLDIR/bin
                check_status $? "cd $INSTALLDIR/bin"
                ln -s ../src/encp.py encp
                check_status $? "ln -s ../src/encp.py encp"
                ln -s ../src/enstore_admin.py enstore
                check_status $? "ln -s ../src/enstore_admin.py enstore"
                cd $INSTALLDIR/sbin
                check_status $? "cd $INSTALLDIR/sbin"
                ln -s encpCut enstoreCut
                check_status $? "ln -s encpCut enstoreCut"
                manifest=$INSTALLDIR/ups/.encpCut.manifest
             fi

	     manifest=$INSTALLDIR/.encpCut.manifest

             filelist=/tmp/$productCut.filelist
             find $INSTALLDIR -type f > $filelist
             check_status $? "find $INSTALLDIR -type f > $filelist"
             cat $filelist | while read f; do
               s=`sum $f`
               check_status $? "s=`sum $f`" quiet
               echo $f $s >>$manifest
               check_status $? "echo $f $s >>$manifest" quiet
             done
             rm -f $filelist
             set | grep _DIR= | egrep -v "export|EMACS|ISPELL"| sort >>$manifest

             #Make the directories group writeable.  This allows UPS to
             # be able to remove the directory(ies).
             find $INSTALLDIR -type d | xargs chmod 775
             check_status $? "find $INSTALLDIR -type d | xargs chmod 775"
             #Remove any write permissions for files.
             find $INSTALLDIR -type f | xargs chmod a-w
             check_status $? "find $INSTALLDIR -type f | xargs chmod a-w"

             #if [ $puntUPS -eq 1 ]; then
             #   rm -f $HOME/${product}_$REV.tar
             #   cd $INSTALLDIR/..
             #   check_status $? "cd $INSTALLDIR"
             #   basen=`basename $INSTALLDIR`
             #   tar -cf $HOME/${product}_$REV.tar $basen
             #   check_status $? "tar -cf $HOME/${product}_$REV.tar $INSTALLDIR"
             #fi
           fi
           ;;

       11) echo "Step $Step:  ups undeclare -f $FLAVOR $product $REV    `starting`"
           if [ "$try" = "doit" ] ; then

	      check_upsupd
	      check_installdir "$INSTALLDIR" $product $REV
	      #$INSTALLDIR=`append_installdir "$INSTALLDIR" $product $REV`

              if [ $puntUPS -ne 1 ]; then
                a=`$UPS_DIR/bin/ups undeclare $QUALIFIERS -f $FLAVOR $product $REV 2>&1`
                stat=$?
                if [ `echo "$a" | grep -c "No matching product"` -eq 0 ]; then
                  check_status $stat $a
                fi
              else
                echo "PUNTING: ups undeclare  $QUALIFIERS -f $FLAVOR $product $REV"
              fi
           fi
           ;;

       12) echo "Step $Step:  ups declare $CHAIN $QUALIFIERS -f $FLAVOR -r $INSTALLDIR -M ${INSTALLDIR}${productUps} -m $product.table $product $REV    `starting`"
           if [ "$try" = "doit" ] ; then

	      check_upsupd
	      check_installdir "$INSTALLDIR" $product $REV
	      #$INSTALLDIR=`append_installdir "$INSTALLDIR" $product $REV`

              if [ $puntUPS -ne 1 ]; then
                 $UPS_DIR/bin/ups declare $CHAIN $QUALIFIERS -f $FLAVOR -r $INSTALLDIR -M ${INSTALLDIR}${productUps} -m $product.table $product $REV
                 check_status $? "ups declare"
              else
                 echo "PUNTING: ups declare $CHAIN $QUALIFIERS -f $FLAVOR -r \`pwd\` -M \`pwd\`${productUps} -m $product.table $product $REV"
              fi
           fi
           ;;

       13) echo "Step $Step:  upd delproduct $QUALIFIERS -H $FLAVOR -f $FLAVOR  $product $REV   `starting`"
           if [ "$try" = "doit" ] ; then

	      check_upsupd
	      check_installdir "$INSTALLDIR" $product $REV
	      #$INSTALLDIR=`append_installdir "$INSTALLDIR" $product $REV`

              if [ $puntUPS -ne 1 ]; then
                a=`$UPD_DIR/bin/upd delproduct $QUALIFIERS -H $FLAVOR -f $FLAVOR $product $REV 2>&1`
                stat=$?
                if [ `echo "$a" | grep -c "No matching product"` -eq 0 ]; then
                  check_status $stat $a
                fi
              else
                echo "PUNTING: upd delproduct $QUALIFIERS -H $FLAVOR -f $FLAVOR $product $REV"
              fi
           fi
           ;;

       14) echo "Step $Step:  upd addproduct $CHAIN $QUALIFIERS -H $FLAVOR -f $FLAVOR -r $INSTALLDIR -M ${INSTALLDIR}${productUps} -m $product.table $product $REV   `starting`"
           if [ "$try" = "doit" ] ; then

	      check_upsupd
	      check_installdir "$INSTALLDIR" $product $REV
	      #$INSTALLDIR=`append_installdir "$INSTALLDIR" $product $REV`

              if [ $puntUPS -ne 1 ]; then
                $UPD_DIR/bin/upd addproduct $CHAIN $QUALIFIERS -H $FLAVOR -f $FLAVOR -r $INSTALLDIR -M ${INSTALLDIR}${productUps} -m $product.table $product $REV
                check_status $? "upd addproduct"
              else
                echo "PUNTING: upd addproduct $CHAIN $QUALIFIERS -H $FLAVOR -f $FLAVOR -r \`pwd\` -M \`pwd\`${productUps} -m $product.table $product $REV"
              fi
           fi
           ;;
	15)
	   echo -n "Step $Step: "
	   echo -n "ups setup -r $build_dir -M $ENSTORE_DIR/ups -m ${productName}.table $productName; "
	   if [ `uname` = "Linux" ]; then
		echo rpmbuild -bb -D "rpmversion $REV" -D "product $productName" -D "upsflags $QUALIFIERS" $ENSTORE_DIR/spec/encp_conf.spec `starting`
	   elif [ `uname` = "SunOS" ]; then
		echo "pgkmk -o -d /tmp -f $ENSTORE_DIR/spec/prototype-conf -a `uname -p` -v $REV) `starting`"
	   else
		check_status 1 "Not defined for `uname`."
	   fi


	   if [ "$try" = "doit" ] ; then

		if [ ! -d $build_dir/etc/profile.d ]; then
		    mkdir -p $build_dir/etc/profile.d
		    check_status $? "mkdir -p $build_dir/etc/"
		fi

		if [ "${SETUP_UPS:-x}" != "x" ]; then
		    echo product $product
		    echo productName $productName

		    #If a previous version exists, make it go away.
		    #unsetup $product
		    #Draconian way of doing it.
		    unset SETUP_`echo $productName | tr '[a-z]' '[A-Z]'`
	   
		    ups_dir=$ENSTORE_DIR/ups

		    # make .sh setup
		    UPS_SHELL=sh; export UPS_SHELL
		    tf=`ups setup -r $build_dir -M $ups_dir -m ${productName}.table $product`
		    #We need to set the paths to those for the rpm, and
		    # not those locally avalible on the build node.
		    sed -e "s:$build_dir:/opt/$product:" \
			-e "s:$ups_dir:/opt/$product:" $tf > $tf.1
		    cp $tf.1 $build_dir/etc/profile.d/${productName}.$UPS_SHELL
		    rm -f $tf $tf.1

		    # make .csh setup
		    UPS_SHELL=csh; export UPS_SHELL
		    tf=`ups setup -r $build_dir -M $ENSTORE_DIR/ups -m ${productName}.table $product`
		    #We need to set the paths to those for the rpm, and
		    # not those locally avalible on the build node.
		    sed -e "s:$build_dir:/opt/$product:" \
			-e "s:$ups_dir:/opt/$product:" $tf > $tf.1
		    cp $tf.1 $build_dir/etc/profile.d/${productName}.$UPS_SHELL
		    rm -f $tf $tf.1

		    chown -R `id -u`.`id -g` $build_dir/etc/
		else
		    check_status 1 "No UPS?"
		fi

		if [ $product = encp -o $product = entv ]; then
		    chooseConfig_loc=$build_dir/opt/$product/chooseConfig
		else
		    chooseConfig_loc=$build_dir/opt/$product/ups/chooseConfig
		fi

		mkdir -p `dirname $chooseConfig_loc`
		cp -p $ENSTORE_DIR/ups/chooseConfig $chooseConfig_loc

                if [ `uname` = "Linux" ]; then

		     if [ ${ENSTORE_DEVEL_DIR:-x} = "x" ]; then
                          OUT_DIR=$ENSTORE_DIR/rpmbuild
                     else
                          OUT_DIR=$ENSTORE_DEVEL_DIR/rpmbuild
                     fi
                     mkdir -p $OUT_DIR/noarch

		    #Take the UPS version and convert it.
		    version=`echo $REV | sed -e 's/^[vx]//' -e 's/_/\./g'`
		    echo version $version
		
		    #For those wondering why encp_conf.spec is used for
		    # both encp and enstore... One file is simpler; and
		    # the '-D "product $productName"' part controls which
		    # product is actually being made.
env | grep rpmbuild
set -xv
		    rpmbuild -bb -D "rpmdir $OUT_DIR" -D "rpmversion $version" -D "product $productName" $ENSTORE_DIR/spec/encp_conf.spec
set +xv
		elif [ `uname` = "SunOS" ]; then
		     #Get some key values.
		     #prefix=/opt/$product
		     ARCH=`uname -p`
		     PKG=$product
		     if [ ${ENSTORE_DEVEL_DIR:-x} = "x" ]; then
			  OUT_DIR=$ENSTORE_DIR/pkgmk
		     else
			  OUT_DIR=$ENSTORE_DEVEL_DIR/pkgmk
		     fi
		     PKGFILE=$OUT_DIR/$PKG-conf-FNAL-$REV-$ARCH.pkg

		     #Make the package file.
		     mkdir -p `dirname $PKGFILE`
		     pkgmk -o -d /tmp -f $ENSTORE_DIR/spec/prototype-conf -a $ARCH -v $REV -r $build_dir/$prefix
		     touch $PKGFILE
		     pkgtrans -s /tmp $PKGFILE $PKG
		     rtn_save=$?
		     rm -r /tmp/$PKG
		     check_status $rtn_save "pkgmk"
		else
		     check_status 1 "Step 15 does not support `uname`."
		fi
	   fi 
	   ;;
	16)
	   echo -n "Step $Step: "
	   if [ `uname` = "Linux" ]; then
		echo rpmbuild -bb -D "rpmversion $REV" -D "upsflags $QUALIFIERS" $ENSTORE_DIR/spec/encp.spec `starting`
	   elif [ `uname` = "SunOS" ]; then
		echo "pgkmk -o -d /tmp -f $ENSTORE_DIR/spec/prototype -a `uname -p` -v $REV) `starting`"
	   else
		check_status 1 "Not defined for `uname`."
	   fi

	   

           if [ "$try" = "doit" ] ; then

		if [ `uname` = "Linux" ]; then
		     if [ ${ENSTORE_DEVEL_DIR:-x} = "x" ]; then
			  OUT_DIR=$ENSTORE_DIR/rpmbuild
		     else
			  OUT_DIR=$ENSTORE_DEVEL_DIR/rpmbuild
		     fi
		     mkdir -p $OUT_DIR/`uname -i`
		      
		     #Take the UPS version and convert it.
		     version=`echo $REV | sed -e 's/^[vx]//' -e 's/_/\./g'`
		     echo version $version
		     echo build_dir $build_dir
		     echo product $product
                     echo productName $productName
                     echo OUT_DIR $OUT_DIR

		     #For those wondering why encp.spec is used for
		     # both encp and enstore... One file is simpler; and
		     # the '-D "product $productName"' part controls which
		     # product is actually being made.
		     rpmbuild -bb -D "rpmversion $version"  -D "product $productName" --buildroot $build_dir -D "rpmdir $OUT_DIR" -D "upsflags $QUALIFIERS" $ENSTORE_DIR/spec/encp.spec
		     check_status $? "rpmbuild"
		     
		elif [ `uname` = "SunOS" ]; then
		     #Get some key values.
		     #prefix=/opt/$product
		     ARCH=`uname -p`
		     PKG=$product
		     if [ ${ENSTORE_DEVEL_DIR:-x} = "x" ]; then
			  OUT_DIR=$ENSTORE_DIR/pkgmk
		     else
			  OUT_DIR=$ENSTORE_DEVEL_DIR/pkgmk
		     fi
		     echo "$QUALIFIERS" | tr ": " "\n" | grep dcache > /dev/null
		     if [ $? -ne 0 ]; then
			PKGFILE=$OUT_DIR/$PKG-$REV-$ARCH.pkg
		     else
			PKGFILE=$OUT_DIR/$PKG-dcache-$REV-$ARCH.pkg
		     fi

		     #Make the package file.
		     mkdir -p `dirname $PKGFILE`
		     #pkgmk -o -d /tmp -f $ENSTORE_DIR/spec/prototype -a $ARCH -v $REV -r $install_base/$prefix
		     pkgmk -o -d /tmp -f $ENSTORE_DIR/spec/prototype -a $ARCH -v $REV -r $build_dir/$prefix
		     touch $PKGFILE
		     pkgtrans -s /tmp $PKGFILE $PKG
		     rtn_save=$?
		     rm -r /tmp/$PKG
		     check_status $rtn_save "pkgmk"
		else
		     check_status 1 "Step 16 does not support `uname`."
		fi
	   fi	
           ;;
        *) check_status 1 "There is no step \"$Step\". Why did you ask for it?"
           ;;

       esac
   done

   if [ "$try" = "list" ] ; then
      /bin/echo -n "\nThere are a total of $STEPSDEFINED steps defined. Is this the set that you want to do? "
      ans=`echo_timed_read 10`
      if [ $? != 0 ] || expr "$ans" : '[Yy]' >/dev/null ; then
        echo "Proceeding with $productCut..."
        echo ""
    else
         cd $thepwd
         check_status $? "cd $thepwd"
         exit 0
      fi
  fi
done

cd $thepwd
echo "$productCut $REV finished at `date`"
