#!/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/pl-itf.h
sed "s/^#define PLVERSION.*/#define PLVERSION $numversion/" $f > $tmp
if cmp $f $tmp; then
    rm -f $tmp
else
    cp $tmp $f
    cp -p $f include/SWI-Prolog.h
    echo "Updated #define PLVERSION in $f and include/SWI-Prolog.h"
fi

rm -f $tmp

for f in pl.lsm pl.spec; do
    sed -e "s/\(Version:[ 	]*\).*/\1$version/" \
	-e "s/pl-[0-9.]*\.tar\.gz/pl-$version.tar.gz/" $f > $tmp
    if cmp $f $tmp; then
	rm -f $tmp
    else
	cp $tmp $f
	echo "Updated version in $f"
    fi
done

cvstag="V${major}_${minor}_${patch}"

if confirm "Tag the CVS repository with $cvstag? "; then
    cvs rtag -F $cvstag pl
fi
if confirm "Upload new LSM entry to lsm@execpc.com? "; then
    mail -s add lsm@execpc.com < pl.lsm
fi
