#!/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-2021, 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] [-no-tag] | <release tar file>

   where:

      -help        Displays this message.
      -no-tag      Doesn't tag the release commit.

   Example usage: $0 sundials-5.0.0.tar.gz
                  $0 sundials-5.0.0-dev.1.tar.gz develop
                  $0 -no-tag sundials-pre-release.tar.gz develop

EOF
}

# at least one input required
if [ $# -lt 1 ]; then
  help
  exit
fi

# tag commits by default
TAG=true

# parse inputs
POSITIONAL=() # store positional inputs
while [[ $# -gt 0 ]]
do
input="$1"

case $input in
  -help)
    help
    exit;;
  -no-tag)
    TAG=false
    shift;;
  *)
    POSITIONAL+=("$input")
    shift;;
esac
done

# reset positional inputs
set -- "${POSITIONAL[@]}"

# check positional inputs
if [ $# -lt 1 ]; then
  help
  exit
elif [ $# -eq 1 ]; then
    DISTFILE=$1
else
    DISTFILE=$1
    BRANCH=$2
fi

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

# 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"
if [ "${TAG}" == "true" ]; then
  git tag -a $VERSION -m "SUNDIALS Release $VERSION"
fi

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 ""
