#! /bin/sh

# Time-stamp: <2015-09-27 13:45:22 feeley>

# Utility to change the version of Gambit attached to a source file.
#
# Usage: changev 40062 40063 file1 file2

OLDVERSION=$1
NEWVERSION=$2
TEMPFILE=/tmp/changev.$$

OLDMAJORVERSION=`eval expr $OLDVERSION / 100000`
OLDMINORVERSION=`eval expr $OLDVERSION % 100000 / 1000`
OLDREVISIONVERSION=`eval expr $OLDVERSION % 1000`
OLDVERSIONSTRING="v$OLDMAJORVERSION"."$OLDMINORVERSION"."$OLDREVISIONVERSION"
OLDDISTSTRING="v$OLDMAJORVERSION"_"$OLDMINORVERSION"_"$OLDREVISIONVERSION"

NEWMAJORVERSION=`eval expr $NEWVERSION / 100000`
NEWMINORVERSION=`eval expr $NEWVERSION % 100000 / 1000`
NEWREVISIONVERSION=`eval expr $NEWVERSION % 1000`
NEWVERSIONSTRING="v$NEWMAJORVERSION"."$NEWMINORVERSION"."$NEWREVISIONVERSION"
NEWDISTSTRING="v$NEWMAJORVERSION"_"$NEWMINORVERSION"_"$NEWREVISIONVERSION"

while [ -n "$3" ]
do

  FILE=$3

  sed -e "s/#define ___VERSION $OLDVERSION/#define ___VERSION $NEWVERSION/g" -e "s/Gambit $OLDVERSIONSTRING/Gambit $NEWVERSIONSTRING/g" -e "s/Gambit Version $OLDVERSIONSTRING/Gambit Version $NEWVERSIONSTRING/g" -e "s/Gambit,$OLDVERSION/Gambit,$NEWVERSION/g" -e "s/(define (system-version) $OLDVERSION)/(define (system-version) $NEWVERSION)/g" -e "s/PACKAGE_VERSION=\'$OLDVERSION\'/PACKAGE_VERSION=\'$NEWVERSION\'/g" -e "s/PACKAGE_STRING=\'Gambit $OLDVERSION\'/PACKAGE_STRING=\'Gambit $NEWVERSION\'/g" -e "s/Gambit $OLDVERSION/Gambit $NEWVERSION/g" -e "s/Gambit configure $OLDVERSION/Gambit configure $NEWVERSION/g" -e "s/Gambit \$as_me $OLDVERSION/Gambit \$as_me $NEWVERSION/g" -e "s/Gambit config.status $OLDVERSION/Gambit config.status $NEWVERSION/g" -e "s/___VERSION != $OLDVERSION/___VERSION != $NEWVERSION/g" -e "s/gambit-not$OLDVERSION.h/gambit-not$NEWVERSION.h/g" -e "s/AC_INIT(Gambit,$OLDVERSIONSTRING,gambit@iro.umontreal.ca,gambit-$OLDDISTSTRING)/AC_INIT(Gambit,$NEWVERSIONSTRING,gambit@iro.umontreal.ca,gambit-$NEWDISTSTRING)/g" -e "s/gambit_dist_if_downloaded=\"gambit-$OLDDISTSTRING/gambit_dist_if_downloaded=\"gambit-$NEWDISTSTRING/g" $FILE > $TEMPFILE

  diff -q $FILE $TEMPFILE > /dev/null

  if [ $? -eq 1 ]
  then
    echo =================================================== $FILE
    diff $FILE $TEMPFILE
    if [ -x $FILE ]
    then
      chmod +x $TEMPFILE
    fi
    mv $TEMPFILE $FILE
    #rm -f $TEMPFILE
  else
    rm -f $TEMPFILE
  fi

  shift

done
