#!/bin/bash
#
#******************************************************************************
# pack (Seq66)
#------------------------------------------------------------------------------
##
# \file       	pack
# \library    	Seq66
# \author     	Chris Ahlstrom
# \date       	2015-09-10
# \update     	2020-11-25
# \version    	$Revision$
# \license    	$XPC_SUITE_GPL_LICENSE$
#
#  Packs up the current project directory, ignoring some of the derived
#  junk and the version-control infrastructure.
#
#  Run "./pack --help" for more information.
#
#  Allows some artifacts generated by bootstrap to be included, so that a
#  conventional "GNU configure" tarfile can be generated. If you do
#  not want that, do a "./bootstrap --full-clean" first.
#
#  Please note that making a build from the resulting tarball will not include
#  git version information, which is a good thing for release tarballs, which
#  just need the date and version number.
#
#-----------------------------------------------------------------------------

CURRENTDATE=$(date +%Y-%m-%d)
CURRENTDIR=$(pwd)
WORKINGDIR=${CURRENTDIR##/*/}       # strip all but last part of path
PACKAGENAME="bogus"
TAGSTRING="pack"
DODRYRUN="no"
DOCLEAN="no"
DORELEASE="no"
DODIFFS="no"

BRANCH=`git symbolic-ref -q HEAD 2> /dev/null`

if [ $? == 0 ] ; then

   ISGITBRANCH="yes"
   GITBRANCH=${BRANCH##*/}

else

   ISGITBRANCH="no"
   GITBRANCH=""

fi

if [ "$1" == "--help" ] || [ "$1" == "help" ] ; then

   cat << E_O_F

pack 0.9 2020-11-25

Usage:  ./pack [--dry-run] [--clean] [--release package] [--diffs] [tag]

'tag' is information to include in the name of the tarball; it replaces the
current date.

This script packs the contents of the project directory into a tarball with the
name of the directory and other information as part of the filename. This
tarball can be suitable for unpacking, then running ./configure, and building
the default (currently rtmidi) build of Seq66 (currently the seq66
executable).  To create such a tarball, see the instructions below.
This script packs the entire current directory ('$WORKINGDIR') into
a file named like the following (using no tag as an example):

   $WORKINGDIR-master-$TAGSTRING-my-code.tar.xz
   $WORKINGDIR-$TAGSTRING-my-code.tar.xz

If the --diffs option is specified, "seqpack" is used in lieu of the
current working directory. Obviously, this option will work only in a
git repository.  It's a convenience for the developers.

For source-code releases, one first checks out the desired git branch,
then bootstraps the desired package (e.g. 'rtmidi'), then builds the code
(optional, but recommended), and then runs a command like the following:

   ./pack --release rtmidi 0.94.6

The resulting tarball can be distributed to users who just want to do
the beautiful "./configure ; make ; make install" mantra.

Excluded from the tarball are derived products.  Version-control-system
directories are ignored.  This script detects the presence of a git branch, and
incorporates the branch name into the name of the tarball.

Finally, it packs up 'configure', Makefile.in files, and other necessary files.
Do './bootstrap --full-clean' first, or './pack --clean', if you don't want
these files and want to start from scratch and build something difference from
the default build.
E_O_F

else

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

      case "$1" in

        --dry-run)
            DODRYRUN="yes"
            ;;

        --clean)
            DOCLEAN="yes"
            ;;

        --no-clean)
            DOCLEAN="no"
            ;;

        --release)
            shift
            DORELEASE="yes"
            PACKAGENAME="$1"
            ;;

        --diffs)
            DODIFFS="yes"
            ;;

        *)
            TAGSTRING="$1"
            ;;

      esac
      shift
   done

   if [ "$ISGITBRANCH" == "yes" ] ; then
      if [ "$DORELEASE" == "yes" ] ; then
         TARBALLNAME="$WORKINGDIR-$GITBRANCH-$PACKAGENAME-$TAGSTRING.tar.xz"
         make clean
      elif [ "$DODIFFS" == "yes" ] ; then
         TARBALLNAME="seqpack-$GITBRANCH-$CURRENTDATE-$TAGSTRING.tar.xz"
      else
         TARBALLNAME="$WORKINGDIR-$GITBRANCH-$CURRENTDATE-$TAGSTRING.tar.xz"
      fi
   else
      if [ "$DODIFFS" == "yes" ] ; then
         echo "The --diffs option requires a git repository, aborting!"
         exit 1
      fi
      
      if [ "$DORELEASE" == "yes" ] ; then
         TARBALLNAME="$WORKINGDIR-$PACKAGENAME-$TAGSTRING.tar.xz"
      else
         TARBALLNAME="$WORKINGDIR-$CURRENTDATE-$TAGSTRING.tar.xz"
      fi
   fi

   echo "The tar-ball to be generated is '../$TARBALLNAME'"

   if [ "$DODRYRUN" == "yes" ] ; then
      if [ "$DOCLEAN" == "yes" ] ; then
         echo "Seq66 will be cleaned by 'bootstrap --full-clean'."
      fi
      if [ "$DODIFFS" == "yes" ] ; then
         echo "Will do 'tar cJf $TARBALLNAME \$(git diff --name-only)'"
      else
         echo "Will do 'tar cJf $TARBALLNAME $WORKINGDIR'"
      fi
      exit 1
   fi

   if [ "$DOCLEAN" == "yes" ] ; then
      echo "Cleaning the project..."
      ./bootstrap --full-clean
   fi

   if [ "$DODIFFS" == "yes" ] ; then

      tar cJf $TARBALLNAME $(git diff --name-only)

   else

# Others that mostly, but not always, are not needed, and so aren't excluded
# at this time:
#
# Makefile.in
# Linux executable files
# --exclude="aux-files"
# --exclude="Makefile" (currently the ones in the "doc" directory
# --exclude="latex"
#
# Removed the slash after WORKINGDIR, and moved it to the end, which
# now allows tar to not include .git in the archive, saving a lot of time
# and space.

      pushd ..
      if [ -d $WORKINGDIR ] ; then

      tar cJf $TARBALLNAME \
 --exclude-vcs \
 --exclude=".git" \
 --exclude="Seq66/seq66" \
 --exclude="Seq66qt5/qseq66" \
 --exclude="Seq66cli/seq66cli" \
 --exclude="config.h" \
 --exclude="config.status" \
 --exclude="libtool" \
 --exclude="Debug" \
 --exclude="Release" \
 --exclude="debug" \
 --exclude="release" \
 --exclude="autom4te.cache" \
 --exclude="core" \
 --exclude="moc" \
 --exclude="obj" \
 --exclude="html" \
 --exclude="ipch" \
 --exclude="*stamp*" \
 --exclude="tests" \
 --exclude="out.*" \
 --exclude=".deps" \
 --exclude=".exes" \
 --exclude=".libs" \
 --exclude="*.a" \
 --exclude="*.BAK" \
 --exclude="*.bak" \
 --exclude="*.bpi" \
 --exclude="*.bz" \
 --exclude="*.bz2" \
 --exclude="*.deps" \
 --exclude="*.gz" \
 --exclude="*.o" \
 --exclude="*.lo" \
 --exclude="*.la" \
 --exclude="*.lib" \
 --exclude="*.log" \
 --exclude="*.obj" \
 --exclude="*.pch" \
 --exclude="*.pdb" \
 --exclude="*.Po" \
 --exclude="*.sdf" \
 --exclude="*.so" \
 --exclude="*.stash" \
 --exclude="*.swp" \
 --exclude=".*.swp" \
 --exclude="*.t" \
 --exclude="*.tds" \
 --exclude="*.tmp" \
 --exclude="*.user" \
 --exclude="*.xz" \
 --exclude="*.testsettings" \
 $WORKINGDIR
         echo
      else
         echo "? Working directory $WORKINGDIR does not exist."
         echo "  Are you running pack from the proper directory?"
         echo "  That is what you must do.  See './pack --help'."
      fi
      popd

   fi

fi

#******************************************************************************
# pack (Seq66)
#------------------------------------------------------------------------------
# vim: ts=3 sw=3 et ft=sh
#------------------------------------------------------------------------------
