#!/bin/bash
#
# Determine the gap version from the git reported last changed date
# of file (or the file GAP_VERSION if it exists). The gapversion
# is the UNIX timestap of the most recently changed file.

GAP_FILES="descriptors.f95 descriptors_wrapper.f95 \
  gp_predict.f95 \
  clustering.f95 gp_fit.f95 \
  gap_fit_module.f95 gap_fit.f95"

GAP_ROOT=$(dirname "$0")

# By default only show the date
VERBOSE=false

# Options for interactive use
while getopts 'v' opt; do
  case $opt in
    v)
      VERBOSE=true
      ;;
    \?)
      echo "Usage: $0"
      echo "'-v' to output name of newest file to stderr"
      exit 1
      ;;
  esac
done

function git_date
{
  if [ -e "$1" ]
  then
    DATE=$(git log -n1 --format="%at" "$1")
    [[ $? -eq 0 && ! -z $DATE ]] && echo "$DATE" || echo 0
  else
    echo 0
  fi
}

if [ -s "${GAP_ROOT}/GAP_VERSION" ]; then
  echo -ne "$(cat "${GAP_ROOT}/GAP_VERSION")"
  exit 0
elif [ -d "${GAP_ROOT}/.git" ] || [ -s "${GAP_ROOT}/.git" ]; then
  # Sort everything as "DATE filename" list and take the last one
  GAP_VERSION=$(
    for I in $GAP_FILES
    do
      if [ -d "${GAP_ROOT}/$(dirname "$I")" ]
      then
        cd "${GAP_ROOT}/$(dirname "$I")"
        echo $(git_date $(basename "$I")) "$I"
        cd - >/dev/null
      fi
    done | sort -n | tail -1 )
  # filename to stderr if requested;
  # split string with parameter expansion
  if [ $VERBOSE == "true" ]; then
    echo "${GAP_VERSION##* }" >&2
  fi
  echo "${GAP_VERSION%% *}"
else
   echo "0"
   exit 0
fi
