#!/usr/bin/env bash

. "$(git rev-parse --show-toplevel)/gitTools/functions"

GAWK=`gitTool gawk`
WGET=`gitTool wget`

current_major=4
current_minor=25
current_build=207
current_comment=Source
current_commit=117075e61eab32f5f4ff2b8fcf9550219f905682

# We only register a build.
if [ "_$1" == "_--registerBuild" ]; then
  latestBuild=`git ls-remote --tags | ${GAWK} -- '$0 ~ /Build-/ {printf "%s %s\n", gensub(/.*Build-([0-9]+).*/, "\\\\1", "g", $0), $1}' | sort -k1rn | head -n 1`
  echo latest build: \'${latestBuild%% *}\' \'${latestBuild##* }\'

  if [ "`git log -1 --pretty=format:%H`" != "${latestBuild##* }" ]; then
    newBuild=Build-$((${latestBuild%% *}+1))
    git tag --force ${newBuild}
    git push origin ${newBuild}
  fi

  exit
fi

# Load information about the last run.
if [ -e ${GitTools}/.UpdateCopasiVersion ]; then
  . ${GitTools}/.UpdateCopasiVersion
else
  lastCommit=
fi

localCommit=`git log -1 ${branch} --pretty=%H`
localChanges=`git status --untracked-files=no --short | wc -l`

# If nothing has changed we stop here.
if [ "_$lastCommit" == "_$localCommit" -a $localChanges == 0 -a "_$1" != "_--force" -a -e ${GitTools}/../copasi/CopasiVersion.h ]; then
  echo No changes detected, skipping update.
  exit
fi

# Save information about current run
echo lastCommit=$localCommit > ${GitTools}/.UpdateCopasiVersion

echo Updating ${GitTools}/../copasi/CopasiVersion.h

version=`git log --pretty="%H %d" ${current_commit}..@{upstream} | ${GAWK} -- '$0 ~ /Version-/ {printf "%s\n", gensub(/.*Version-([0-9]+).([0-9]+).*/, "\\\\1 \\\\2", "g", $0)}' | sort -k1rn -k2rn | head -n 1`

if [ "_${version}" != _ ]; then
  current_major=${version%% *}
  current_minor=${version##* }
fi

# git log --pretty=%H $current_commit..HEAD
commitList=`git log --pretty=%H $current_commit..HEAD`
# git log --pretty="%H %d" ${current_commit}..@{upstream}
buildList=`git log --pretty="%H %d" ${current_commit}..@{upstream} | ${GAWK} -- '$0 ~ /Build-/ {printf "%s %s,", gensub(/.*Build-([0-9]+).*/, "\\\\1", "g", $0), $1}'`

SAVEIFS=$IFS
IFS=','

echo $current_build > $$

for b in $buildList; do
  build=${b%% *}
  commit=${b##* }
  
  if [ `echo $commitList | grep ${commit}` ]; then
    if [ $current_build == 207 ]; then 
      current_build=${build}
      current_commit=${commit}
    fi
    
    echo $build >> $$
  fi
done 

IFS=$SAVEIFS

compatible=all
previous=206
next=207

compatibility={

for b in `sort -g $$`; do
  # Remove Windows line ending
  b=$((${b//$'\r'}))

  if [ $next != $b ]; then
    compatibility="${compatibility}${previous}, "
    compatible=list
  fi
  
  previous=$((b))
  next=$((b+1))
  
  if [ $compatible == list -a $b != $current_build ]; then
    compatibility="${compatibility}${b}, "
  fi
done;  

compatibility="${compatibility}${current_build}}"

rm $$

RELEASE=`echo $buildList | grep -q ${localCommit} && echo true`

if [ _${RELEASE} == _true -a $localChanges == 0 ]; then
  creator=copasi.org
  modified=false
else
  creator=`git config user.email`
  modified=true
fi


sed -e 's/COPASI_VERSION_MAJOR.*/COPASI_VERSION_MAJOR '$current_major'/' \
    -e 's/COPASI_VERSION_MINOR.*/COPASI_VERSION_MINOR '$current_minor'/' \
    -e 's/COPASI_VERSION_BUILD.*/COPASI_VERSION_BUILD '$current_build'/' \
    -e 's/COPASI_VERSION_MODIFIED.*/COPASI_VERSION_MODIFIED '$modified'/' \
    -e 's/COPASI_VERSION_COMPATIBILITY.*/COPASI_VERSION_COMPATIBILITY '"$compatibility"'/' \
    -e 's/COPASI_VERSION_COMMENT.*/COPASI_VERSION_COMMENT "'$current_comment'"/' \
    -e 's/COPASI_VERSION_CREATOR.*/COPASI_VERSION_CREATOR "'$creator'"/' \
    ${GitTools}/../copasi/CopasiVersion.h.in > ${GitTools}/../copasi/CopasiVersion.h
    
IFS=$SAVEIFS
