#!/bin/bash
# Automate the release process.
# Author: Jan wielemaker
#
# Usage: first update VERSION, then run this script.

function confirm ()
{ while true; do
    echo -n "$1 "
    read answer
    case "$answer" in
          y*)   return 0
                ;;
          n*)   return 1
                ;;
          *)
                echo "Please answer yes or no"
                ;;
    esac
  done
}

version=`cat VERSION`
versiondate=`date +"%B %Y"`

major=`echo $version | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1/'`
minor=`echo $version | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\2/'`
patch=`echo $version | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\3/'`

numversion=$(($major * 10000 + $minor * 100 + $patch))

tmp=.tmp$$

f=src/SWI-Prolog.h
sed "s/^#define PLVERSION.*/#define PLVERSION $numversion/" $f > $tmp
if cmp $f $tmp; then
    rm -f $tmp
else
    cat $tmp > $f
    rm $tmp
    cp -p $f include/SWI-Prolog.h
    echo "Updated #define PLVERSION in $f and include/SWI-Prolog.h"
fi

rm -f $tmp

month=`LANG=POSIX date "+%B"`
year=`date "+%Y"`
f=man/main.doc
sed -e "s@{\\\\vmajor}{[0-9]*}@{\\\\vmajor}{$major}@" \
    -e "s@{\\\\vminor}{[0-9]*}@{\\\\vminor}{$minor}@" \
    -e "s@{\\\\vpatch}{[0-9]*}@{\\\\vpatch}{$patch}@" \
    -e "s@{\\\\vmonth}{[A-Za-z]*}@{\\\\vmonth}{$month}@" \
    -e "s@{\\\\vyear}{[0-9]*}@{\\\\vyear}{$year}@" \
	$f > $tmp
if cmp $f $tmp; then
    rm -f $tmp
else
    cat $tmp > $f
    rm $tmp
    echo "Updated $f"
fi

gittag="V${major}.${minor}.${patch}"

if confirm "Tag the GIT repository with $gittag? "; then
    git tag -s -f -m "Release tag for ${major}.${minor}.${patch}" $gittag
fi


if confirm "Tag all packages with $gittag? "; then
    tmp=/tmp/msg$$
    echo "Release tag for ${major}.${minor}.${patch}" > $tmp

    git submodule foreach git tag -s -f -F $tmp  $gittag

    rm -f $tmp
fi

