#!/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
# ------------------------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2019, Lawrence Livermore National Security
# and Southern Methodist University.
# All rights reserved.
#
# See the top-level LICENSE and NOTICE files for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# SUNDIALS 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-4.0.0-dev.tar.gz develop

EOF
}

# check number of inputs
if [ $# -eq 1 ]; then
    DISTFILE=$1
elif [ $# -eq 2 ]; then
    DISTFILE=$1
    BRANCH=$2
else
    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
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
if [ $? -ne 0 ]; then
  echo "Error: Failed to untar $DISTFILE"
  exit -1
fi

# 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

# if a branch is specified, checkout that branch
if [ -n "${BRANCH}" ]; then
    cd $REPODIR
    git checkout ${BRANCH}
    if [ $? -ne 0 ]; then
        echo "Error: git checkout failed"
        exit -1
    fi
    cd ..
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 -A
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 ""
