#!/bin/sh

# ==================================================================== #
# TITLE                                                                #
# AMR: An R Package for Working with Antimicrobial Resistance Data     #
#                                                                      #
# SOURCE                                                               #
# https://github.com/msberends/AMR                                     #
#                                                                      #
# CITE AS                                                              #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C    #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance  #
# Data. Journal of Statistical Software, 104(3), 1-31.                 #
# doi:10.18637/jss.v104.i03                                            #
#                                                                      #
# Developed at the University of Groningen and the University Medical  #
# Center Groningen in The Netherlands, in collaboration with many      #
# colleagues from around the world, see our website.                   #
#                                                                      #
# This R package is free software; you can freely use and distribute   #
# it for both personal and commercial purposes under the terms of the  #
# GNU General Public License version 2.0 (GNU GPL-2), as published by  #
# the Free Software Foundation.                                        #
# We created this package for both routine data analysis and academic  #
# research and it was publicly released in the hope that it will be    #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY.              #
#                                                                      #
# Visit our website for the full manual and a complete tutorial about  #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/   #
# ==================================================================== #

echo "Running pre-commit hook..."

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if command -v Rscript > /dev/null; then
  if [ "$(Rscript -e 'cat(all(c('"'pkgload'"', '"'devtools'"', '"'dplyr'"', '"'styler'"') %in% rownames(installed.packages())))')" = "TRUE" ]; then
    Rscript -e "source('data-raw/_pre_commit_hook.R')"
    currentpkg=`Rscript -e "cat(pkgload::pkg_name())"`
    echo "-> Adding all files in 'data-raw' to this commit"
    git add data-raw/*
    git add man/*
    git add R/sysdata.rda
    git add NAMESPACE
  else
    echo "- R package 'pkgload', 'devtools', 'dplyr', or 'styler' not installed!"
    currentpkg="your"
  fi
else
  echo "- R is not available on your system!"
  currentpkg="your"
fi
echo ""


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo "Updating semantic versioning and date..."

# get tags from remote, and remove tags not on remote:
git fetch origin --prune --prune-tags --quiet
currenttagfull=`git describe --tags --abbrev=0`
currenttag=`git describe --tags --abbrev=0 | sed 's/v//'`
# assume main branch to be 'main' or 'master', pick the right name:
defaultbranch=`git branch | cut -c 3- | grep -E '^master$|^main$'`
if [ "$currenttag" = "" ]; then
  # there is no tag, so set tag to 0.0.1 and commit index to current count
  currenttag="0.0.1"
  currentcommit=`git rev-list --count ${defaultbranch}`
  echo "- no git tags found, create one in format 'v(x).(y).(z)' - curently ${currentcommit} previous commits in ${defaultbranch}"
else
  # there is a tag, so base version number on that
  currentcommit=`git rev-list --count ${currenttagfull}..${defaultbranch}`
  if (( "$currentcommit" == 0 )); then
    # tag is new, so this must become the version number
    currentversion="$currenttag"
  fi
  echo "- latest tag is '${currenttagfull}', with ${currentcommit} previous commits in ${defaultbranch}"
fi
if [ "$currentversion" = "" ]; then
  # combine tag (e.g. 1.2.3) and commit number (like 5) increased by 9000 to indicate beta version
  currentversion="$currenttag.$((currentcommit + 9001))" # results in e.g. 1.2.3.9005
fi
echo "- ${currentpkg} pkg version set to ${currentversion}"

# set version number and date to DESCRIPTION file
sed -i -- "s/^Version: .*/Version: ${currentversion}/" DESCRIPTION
sed -i -- "s/^Date: .*/Date: $(date '+%Y-%m-%d')/" DESCRIPTION
echo "- updated DESCRIPTION"
# remove leftover on macOS
rm -f DESCRIPTION--
# add to commit
git add DESCRIPTION

# set version number to NEWS file
if [ -e "NEWS.md" ]; then
  if [ "$currentpkg" = "your" ]; then
    currentpkg=""
  fi
  sed -i -- "1s/.*/# ${currentpkg} ${currentversion}/" NEWS.md
  echo "- updated NEWS.md"
  # remove leftover on macOS
  rm -f NEWS.md--
  # add to commit
  git add NEWS.md
else
  echo "- no NEWS.md found!"
fi
echo ""