#!/bin/sh
#
#  Script to make new distributions
#  Precondition is that the version you want to distribute must be setup.
#
# Make sure this wasn't done before
if [ -r $BFDIST/tarfiles/DistLog.$BFCURRENT ]; then
  echo "The distribution for this release has already been made."
  echo "Request ignored."
  exit 2
else
  /bin/touch $BFDIST/tarfiles/DistLog.$BFCURRENT
fi
savedir=$PWD
stdfiles=" GNUmakefile ReleaseLog "
stddirs=" tmp bin lib include man doc results ups "
for var in $BFDIST/releases/$BFCURRENT/*
do
  pack=`echo $var | awk -F'/' '{print $NF}'`
  vers=`ls -ld $var | awk -F'/' '{print $NF}'`
  if (echo "$stdfiles $stddirs" | grep -v " $pack " >/dev/null) then
  # Make tar file for this package if necessary
     if [ ! -r $BFDIST/tarfiles/${pack}-${vers}.tar ]; then
        cd $BFDIST/packages/$pack
        echo "Making tar file for $pack $vers"
        /bin/tar -cf $BFDIST/tarfiles/${pack}-${vers}.tar $vers
     fi
     echo "$BFDIST/tarfiles/${pack}-${vers}.tar" >> $BFDIST/tarfiles/DistLog.$BFCURRENT
     # Add tar file to the database
     if [ "$UPS_DIR" ]; then
        $UPS_DIR/bin/ups modify -T `domainname`:$BFDIST/tarfiles/${pack}-${vers}.tar -z $RUN2_PRODUCTS $pack $vers
     fi
  fi  
done
# Now make release tar file for the current flavor:
flvr=`uname -s | cut -c1,2,3,4`
echo "Making $flvr tar file for release $BFCURRENT"
cd $BFDIST/releases/$BFCURRENT
/bin/find * \( ! -type d \) -print | egrep -v '^(tmp/|bin/|ups/comp*|results/)' | \
/bin/tar -cf $BFDIST/tarfiles/rel-V${BFCURRENT}-$flvr.tar -

# Now make release tar file for the NULL flavor:
echo "Making NULL tar file for release $BFCURRENT"
/bin/find * \( ! -type d \) -print | egrep -v '^(tmp/|bin/|lib/|ups/comp*|results/)' | \
/bin/tar -cf $BFDIST/tarfiles/rel-V${BFCURRENT}-NULL.tar -

if [ "$UPS_DIR" ]; then
   echo "Declaring $flvr tar file to ups database"
   $UPS_DIR/bin/ups modify -f $flvr -T `domainname`:$BFDIST/tarfiles/rel-V${BFCURRENT}-$flvr.tar -z $RUN2_PRODUCTS cdfsoft2 $BFCURRENT
fi
# Make new ups database if needed
if [ -d $BFROOT/ups_database ]; then
   echo "Re-making the ups database tarfile..."
   mv $BFDIST/tarfiles/database.tar $BFDIST/tarfiles/database.tar_old
   cd $BFROOT/ups_database/declared
   /bin/tar -cf $BFDIST/tarfiles/database.tar *
fi
cd $savedir
exit 0

