#!/bin/sh
#
#
# NAME
#   makeDist: make distribution for this software project
#
#
# USAGE
#   MD/bin/makeDist [dist_name_suffix]
#
#   (Must run in root directory of distribution source.)
#
#
# ARGUMENTS
#   dist_name_suffix: suffix of distribution name.
#     Prefix is "PST".
#
#
# DESCRIPTION
#   Instructions:
#
#     () Make your working directory the root directory of the
#        source heirarchy.  e.g. "cd $HOME/src/PST"
#
#     () Make sure no distribution or backup already exist for the
#        current day.  If it does, then this script will fail and tell
#        you to remove those directories.  This is intentional because
#        it requires some thought before overwriting an existing
#        distribution or backup directory or file.
#
#     () Type "MD/bin/makeDist".
#
#        This will create a directory called PREFIX-YYYYmmmDD where
#        YYYYmmmDD is a timestamp string using obvious notation, and
#        PREFIX is "PST".  This directory will not contain any of the
#        CVS directories.
#
#        This will also create a ".tar.gz" file of that directory.




# TAR_FLAGS: list of flags for creating a distribution
#
# GNU: h follows symbolic links
#
# IRIX: L follows symbolic links.
#       K tars files greater than 2 gigabytes.

TAR_FLAGS=cf

# SOURCE_LIST: list of entries to put in this distribution.
SOURCE_LIST="MD SET TUSC README.txt"




if [ $# = 0 ]; then
  DIST_SUFFIX=`date +%Y%b%d | tr '[A-Z]' '[a-z]'`
elif [ $# = 1 ]; then
  DIST_SUFFIX=$1
else
  echo "`basename $0`: ERROR: insufficient arguments."
  echo "usage: `basename $0` dist_name"
  echo "  dist_name: name of distribution"
  exit 1
fi

PREFIX=PST
DIST_DIR=${PREFIX}-${DIST_SUFFIX}
DIST_FILE=${DIST_DIR}.tar




# makeSums: create checksum files to determine integrity of files
makeSums ()
{
  if [ -f sums ]; then
    # Remove old sums file.  This avoids applying sums to itself.
    rm sums
  fi
  echo "makeSums: generating checksums..."
  find . -type f -exec sum {} \; \
    | sort -n                    \
    > sums
}




# makeTarfileByDate: create tar file named by current date
makeTarfileByDate ()
{
  for item in $SOURCE_LIST ; do
    if [ ! -e $item ]; then
      echo "makeTarfileByDate: ERROR: could not find '$item' here."
      echo "  You probably ran makeDist from the wrong directory."
      echo "  Run from the root directory of this distribution."
      exit 2
    fi
  done

  if [ -d "$DIST_SUFFIX" ]; then
    echo "makeTarfileByDate: ERROR: directory '$DIST_SUFFIX' already exists."
    echo "  Remove that directory to make a new tarfile."
    exit 3
  else
    mkdir $DIST_SUFFIX
  fi

  makeSums

  # Append sums file to list of sources to put in distribution.
  SOURCE_LIST="$SOURCE_LIST sums"

  echo "makeTarfileByDate: Creating tar file..."
  tar ${TAR_FLAGS} ${DIST_SUFFIX}/${DIST_FILE} ${SOURCE_LIST}
}




# makeBackupByDate: make directory, named by date, containing backup
makeBackupByDate ()
{
  makeTarfileByDate

  if [ ! -d $DIST_SUFFIX ]; then
    echo "makeBackupByDate: PANIC: '$DIST_SUFFIX' does not exist."
    echo "  Something is horribly wrong."
    exit 4
  fi

  ( cd $DIST_SUFFIX                             ;
    tar xf $DIST_FILE && rm $DIST_FILE          ;
  )

  omit_list_1=`( cd $DIST_SUFFIX ; find . -type d -name 'CVS' -print )`
  omit_list_2=`( cd $DIST_SUFFIX ; find . -type d -name '200[0-9]???[0-9][0-9]' -print )`
  omit_list="$omit_list_1 $omit_list_2"

  echo "makeBackupByDate: removing CVS files and backups from distribution..."
  ( cd $DIST_SUFFIX          ;
    for subdir in $omit_list ; do
      rm -r $subdir
    done
  )
}



# makeDistByDate: make distribution file, named by date
makeDistByDate ()
{
  if [ -d "$DIST_DIR" ]; then
    echo "makeDistByDate: ERROR: directory '$DIST_DIR' already exists."
    echo "  Remove that directory to make a new tarfile."
    if [ -d "$DIST_SUFFIX" ]; then
      echo "  Remove '$DIST_SUFFIX' too."
    fi

    exit 5
  fi

  makeBackupByDate

  mv $DIST_SUFFIX $DIST_DIR
  tar $TAR_FLAGS $DIST_FILE $DIST_DIR
  gzip $DIST_FILE
}




makeDistByDate
