#!/bin/sh
###########################################################
#
#  UNIT NAME:  label
#
#  PURPOSE:  New releases and/or patches are best defined
#            by version labels for efficient CM.  This
#            script is used to define the new release or
#            patch in terms of a baseline release or patch.
#            Then units can be added, modified, or deleted
#            without having to specifically indicate which
#            units are unchanged.  The unchanged units will
#            simply keep the version labels it already has.
#
#  SYNTAX:  label
#
#  NOTES:  An instance of the ISTP LZP directory tree
#          (with links) must exist on the archive host.
#          This script must be run on the archive host.
#
###########################################################

# default options
top=`pwd`
oldver=ISTP_B1
newver=ISTP_B2

# initialize ad subsystem tree
for location in		ad/.. \
			ad/common \
			ad/wind \
			ad/polar \
			ad/geotail \
			ad/config \
			ad/help
do
ad_tree="$ad_tree $location"
done

# initialize fs subsystem tree
for location in		fs/sync/etc
do
fs_tree="$fs_tree $location"
done

# initialize recon subsystem tree
for location in		recon/common/procedures \
			recon/common/tables \
			recon/common/source \
			recon/common/include \
			recon/common/docs \
			recon/common/help \
			recon/wind/procedures \
			recon/wind/tables \
			recon/polar/procedures \
			recon/polar/tables \
			recon/pwiw/procedures \
			recon/pwiw/tables \
			recon/geotail/procedures \
			recon/geotail/tables
do
recon_tree="$recon_tree $location"
done

# initialize trend subsystem tree
for location in		trend/common/src \
			trend/wind/src \
			trend/polar/src \
			trend/geotail/src \
			trend/config \
			trend/help
do
trend_tree="$trend_tree $location"
done

echo ""
echo "Enter full path of target tree ($top): "
read in_top
if [ $in_top ]
then
   top=$in_top
fi

cd $top
if [ $? -ne 0 ]
then
   echo "ERROR: $top does not exist"
   exit 1
fi

echo ""
echo "Enter subsystem (ad, dps, fs, recon, trend): "
read subsys
case $subsys in
   ad)      istp_tree="$ad_tree" ;;
   dps)     istp_tree="" ;;
   fs)      istp_tree="$fs_tree" ;;
   recon)   istp_tree="$recon_tree" ;;
   trend)   istp_tree="$trend_tree" ;;
   *)       echo "ERROR: invalid subsystem"; exit 1 ;;
esac

echo ""
echo "Enter old version label ($oldver): "
read in_oldver
if [ $in_oldver ]
then
   oldver=$in_oldver
fi

echo ""
echo "Enter new version label ($newver): "
read in_newver
if [ $in_newver ]
then
   newver=$in_newver
fi

echo ""
logfile=$HOME/$oldver.$newver.$subsys.vlog
echo "Enter log file ($logfile): "
read in_logfile
if [ $in_logfile ]
then
   logfile=$in_logfile
fi

echo ""
echo "Target tree path   --> $top"
echo "Subsystem          --> $subsys"
echo ""
echo "Old version label  --> $oldver"
echo "New version label  --> $newver"
echo "Log file           --> $logfile"
echo ""
echo "Add new version labels (Y/N)? "
read answer
if [ "$answer" != "Y" ]
then
   echo "ERROR: script aborted"
   exit 1
fi

touch $logfile
echo "Target tree path   --> $top"     >> $logfile
echo "Subsystem          --> $subsys"  >> $logfile
echo ""                                >> $logfile
echo "Old version label  --> $oldver"  >> $logfile
echo "New version label  --> $newver"  >> $logfile
echo "Log file           --> $logfile" >> $logfile
echo ""                                >> $logfile

###########################################################
#   ISTP archives
###########################################################
if [ "$istp_tree" ]
then
for location in $istp_tree
do

   workarg=$location
   while [ "$workarg" != "." ]
   do
      SUBSYSTEM=$workarg
      workarg=`dirname $workarg`
   done

   PROJECT=istp
   export PROJECT SUBSYSTEM
   cd $top/$location

   echo "[$PROJECT/$SUBSYSTEM]$top/$location=> vcs -V$newver:$oldver"
   echo "[$PROJECT/$SUBSYSTEM]$top/$location=> vcs -V$newver:$oldver" >> $logfile
   vcsfile=vcs_"$SUBSYSTEM"_file

   rm -f $vcsfile.*
   if [ $? = 0 ]
   then
      vlog -bv$oldver "*,v" 2>>$logfile | awk '{ print $1 }' >> $vcsfile.1
      if [ $? = 0 ]
      then
         echo "#!/bin/sh" > $vcsfile.2
         command="awk '{ printf"' "vcs -V'$newver:$oldver" %s >> $logfile\\"n'", $1 }'"'"
         cat $vcsfile.1 2>/dev/null | eval "$command" 1>> $vcsfile.2
         if [ $? = 0 ]
         then
            chmod u+x $vcsfile.2
            $vcsfile.2
            if [ $? = 0 ]
            then
               echo "***** SUCCESS ***** executing $vcsfile.2" >> $logfile
            else
               echo "***** ERROR ***** executing $vcsfile.2" >> $logfile
               echo "***** ERROR ***** executing $vcsfile.2"
            fi
         else
            echo "***** ERROR ***** parsing $vcsfile.1" >> $logfile
            echo "***** ERROR ***** parsing $vcsfile.1"
         fi
      else
         echo "***** ERROR ***** executing vlog -bv$oldver" >> $logfile
         echo "***** ERROR ***** executing vlog -bv$oldver"
      fi
   else
      echo "***** ERROR ***** removing $vcsfile" >> $logfile
      echo "***** ERROR ***** removing $vcsfile"
   fi

done
fi

###########################################################
#   DPS archives
###########################################################
if [ "$subsys" = "dps" ]
then
for SUBSYSTEM in	ISTP \
			ISTPDB \
			ISTPXXX \
			ISTPPDP \
			ISTPTOOLS \
			ISTPPICS
do

   PROJECT=pacor2
   export PROJECT SUBSYSTEM
   cd $top/dps

   echo "[$PROJECT/$SUBSYSTEM]$top/dps=> vcs -V$newver:$oldver"
   echo "[$PROJECT/$SUBSYSTEM]$top/dps=> vcs -V$newver:$oldver" >> $logfile
   vcsfile=vcs_"$SUBSYSTEM"_file

   rm -f $vcsfile.*
   if [ $? = 0 ]
   then
      vlog -bv$oldver "*,v" 2>>$logfile | awk '{ print $1 }' >> $vcsfile.1
      if [ $? = 0 ]
      then
         echo "#!/bin/sh" > $vcsfile.2
         command="awk '{ printf"' "vcs -V'$newver:$oldver" %s >> $logfile\\"n'", $1 }'"'"
         cat $vcsfile.1 2>/dev/null | eval "$command" 1>> $vcsfile.2
         if [ $? = 0 ]
         then
            chmod u+x $vcsfile.2
            $vcsfile.2
            if [ $? = 0 ]
            then
               echo "***** SUCCESS ***** executing $vcsfile.2" >> $logfile
            else
               echo "***** ERROR ***** executing $vcsfile.2" >> $logfile
               echo "***** ERROR ***** executing $vcsfile.2"
            fi
         else
            echo "***** ERROR ***** parsing $vcsfile.1" >> $logfile
            echo "***** ERROR ***** parsing $vcsfile.1"
         fi
      else
         echo "***** ERROR ***** executing vlog -bv$oldver" >> $logfile
         echo "***** ERROR ***** executing vlog -bv$oldver"
      fi
   else
      echo "***** ERROR ***** removing $vcsfile" >> $logfile
      echo "***** ERROR ***** removing $vcsfile"
   fi

done
fi

###########################################################
#   EXIT success
###########################################################
exit 0
