#!/bin/sh
# ------------------------------------------------------------------------------
# Programmer(s): David J. Gardner @ LLNL
# ------------------------------------------------------------------------------
# Acknowledgement(s): This script is based on the mkgithub script for the hypre
#                     library written by Rob Falgout @ LLNL
# ------------------------------------------------------------------------------
# LLNS Copyright Start
# Copyright (c) 2014, Lawrence Livermore National Security
# This work was performed under the auspices of the U.S. Department
# of Energy by Lawrence Livermore National Laboratory in part under
# Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
# Produced at the Lawrence Livermore National Laboratory.
# All rights reserved.
# For details, see the LICENSE file.
# LLNS Copyright End
# ------------------------------------------------------------------------------
# Update SUNDIALS GitHub distribution with a commit from sundials release tar 
# file. Run from any machine that has access to github.
# ------------------------------------------------------------------------------

help ()
{
  cat <<EOF

   $0 -help | <release tar file>

   where:

      -help        Displays this message.

   Example usage: $0 sundials-2.7.0.tar.gz
                  $0 sundials-3.0.0-beta-1.tar.gz

EOF
}

# check number of inputs
if [ $# -ne 1 ]; then
  help
  exit -1
fi

case $1 in
    -h|-help)
        help
        exit;;
esac

# move to location where tarscript places tarballs
cd ../..

# check that tar file exists
DISTFILE=$1
if [ ! -e $DISTFILE ]; then
  echo "Error: tar file not found"
  exit -1
fi

# parse tar file name and set version number
DISTDIR=`basename $DISTFILE .tar.gz`
VERSION=`echo $DISTDIR | sed 's/sundials-/v/g'`

# names of directories for working and cloning the GitHub repo
WORKDIR="sundials-github"
REPODIR="sundials-github-repo"

# create WORKDIR, exit with an error if it exists already
mkdir $WORKDIR
if [ $? -ne 0 ]; then
  echo "Error: Subdirectory sundials-github already exists"
  exit -1
fi

# untar sundials distribution into WORKDIR
\tar xzf $DISTFILE -C $WORKDIR

# change into working directory
cd $WORKDIR

# clone github repo (with ssh) into REPODIR
git clone git@github.com:LLNL/sundials.git $REPODIR
if [ $? -ne 0 ]; then
  echo "Error: GitHub clone failed! Make sure to authenticate in a web browser first."
  exit -1
fi

# remove everything from the local github repo except for '.git'
rm -fr $REPODIR/*

# add sundials distribution files to local github repo
cp -r $DISTDIR/* $REPODIR

# change into the github repo directory and commit/tag the new code
cd $REPODIR
git add .
git commit -m "SUNDIALS Release $VERSION"
git tag -a $VERSION -m "SUNDIALS Release $VERSION"

echo ""
echo "*** DO THE FOLLOWING TO COMPLETE THE GITHUB UPDATE ***"
echo ""
echo "Review the new commit in $WORKDIR/$REPODIR"
echo "Do 'git push' and 'git push --tags'"
echo ""
