#!/bin/sh

# This script updates VERSION_SUBMINOR in petscversion.h and download/index.html
#
# Usage: createpatch petscrepo
# example usage: createpatch /sandbox/petsc/petsc-dist

if [ $# = 1 ]; then
  petscrepo=$1
else
  echo "Error: petscrepo not specified. Usge: createpatch petscrepo"
  exit
fi

# check petscrepo to be valid
if [ ! -d $petscrepo ]; then
  echo "Error: dir $petscrepo does not exist"
  exit
fi
cd $petscrepo

if [ ! -f include/petscversion.h ]; then
  echo "Error: dir $petscrepo/include/petscversion.h does not exist"
  exit
fi

if [ ! -f src/docs/website/download/index.html ]; then
  echo "Error: dir $petscrepo/src/docs/website/download/index.html does not exist"
  exit
fi

# check if all files are checked in
a=`git status --untracked-files=no --short | wc -l`
if [ "${a}" != "0" ]; then
  echo "*** Git edited files exist. Cannot proceed! ****"
  git status --untracked-files=no --short
  exit
fi

#git_branch=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`
#if  [ "${git_branch}" != "maint" ]; then
#  echo "Error: Wrong branch '${git_branch}'! Patchlevel can only be updated for 'maint' branch"
#  exit
#fi

version_release=`grep '^#define PETSC_VERSION_RELEASE ' include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`
version_major=`grep '^#define PETSC_VERSION_MAJOR ' include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`
version_minor=`grep '^#define PETSC_VERSION_MINOR ' include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`
version_subminor=`grep '^#define PETSC_VERSION_SUBMINOR ' include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`


if  [ ${version_release} = 0 ]; then
  echo "Error: PETSC_VERSION_RELEASE is unset. Not using a 'maint' branch?"
  exit
fi

# crank up patchlevel
new_version_subminor=`expr $version_subminor + 1`
oldver=${version_major}.${version_minor}.${version_subminor}
newver=${version_major}.${version_minor}.${new_version_subminor}

echo #########################################################
echo ## updating patchlevel from $version_subminor to $new_version_subminor  ##
echo #########################################################


# Update patchlevel in petscversion.h
/bin/mv include/petscversion.h include/petscversion.h.bak
cat include/petscversion.h.bak | \
  sed -e "s/#define PETSC_VERSION_SUBMINOR .*/#define PETSC_VERSION_SUBMINOR   ${new_version_subminor}/" > include/petscversion.h
/bin/rm -f include/petscversion.h.bak

# Update patchlevel in download/index.html
/bin/mv src/docs/website/download/index.html src/docs/website/download/index.html.bak
cat src/docs/website/download/index.html.bak | \
  sed -e "s/-${oldver}.tar.gz/-${newver}.tar.gz/g" > src/docs/website/download/index.html
/bin/rm -f src/docs/website/download/index.html.bak

# now create a changeset
git commit -m"Increase patchlevel to ${newver}" include/petscversion.h src/docs/website/download/index.html
echo #########################################################
echo # Created patch for the following change                #
echo #########################################################
git show HEAD
