#!/bin/bash
#
#******************************************************************************
# pack (Sequencer64)
#------------------------------------------------------------------------------
##
# \file       	pack
# \library    	Sequencer64
# \author     	Chris Ahlstrom
# \date       	2015-09-10
# \update     	2018-09-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.
#
#  Now updated to allow 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

seq64 pack 0.5 2018-09-25

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

where tag is alternate information you want to include in the
name of the tarball; it replaces the current date.

This script packs the contents of the current directory into a
tarball that has the name of the directory and other information
as part of the filename. This tarball should be suitable for unpacking,
then running ./configure, and building the default (currently rtmidi)
build of Sequencer64 (currently the seq64 executable).  To create such a
tarball, bootstrap the project for release and rtmidi (or some other setup),
and run "./pack".

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.

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 "./configure ; make ; make install" mantra.

Excluded from the tarball are derived products such as the 'Debug',
'Release', 'html', and 'latex' directories, as well as object
modules and the database/symbol files generated by Visual Studio
or Borland.  Version-control-system directories are also ignored.
This script also detects the presence of a git branch, and incorporates
the branch name into the name of the tarball.

Finally, it will pack up configure, the Makefile.in files, and other
necessary files if they are present.  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
      
#        Although this option is no longer needed, keep it around as an
#        undocumented feature just in case the user wants to override the
#        git discovery.

        --branch | --git)
            shift
            ISGITBRANCH="yes"
            GITBRANCH="$1"
            ;;

        --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"
      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 "Sequencer64 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
# Makefile.in
# Linux executable files
# --exclude="aux-files"
#
# 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="Midiclocker64/midiclocker64" \
 --exclude="Sequencer64/sequencer64" \
 --exclude="Seq64rtmidi/seq64" \
 --exclude="Seq64qt5/qseq64" \
 --exclude="Seq64cli/seq64cli" \
 --exclude="config.h"  \
 --exclude="config.status"  \
 --exclude="libtool"   \
 --exclude="Makefile"  \
 --exclude="Debug"     \
 --exclude="Release"   \
 --exclude="debug"     \
 --exclude="release"   \
 --exclude="autom4te.cache" \
 --exclude="core"      \
 --exclude="moc"       \
 --exclude="obj"       \
 --exclude="html"      \
 --exclude="ipch"      \
 --exclude="latex"     \
 --exclude="*stamp*"     \
 --exclude="tests"     \
 --exclude="out.*"     \
 --exclude=".deps"     \
 --exclude=".exes"     \
 --exclude=".libs"     \
 --exclude="*obsolete*" \
 --exclude="*.a"       \
 --exclude="*.BAK"     \
 --exclude="*.bak"     \
 --exclude="*.bpi"     \
 --exclude="*.bz"      \
 --exclude="*.bz2"     \
 --exclude="*.deps"    \
 --exclude="*.gz"      \
 --exclude="*.idb"     \
 --exclude="*.ilc"     \
 --exclude="*.ild"     \
 --exclude="*.ilf"     \
 --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 above this directory."
         echo "  Are you running pack.sh from the proper directory?"
         echo "  That is what you must do.  See './pack --help'."
      fi
      popd

   fi

fi

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