#!/bin/csh -f
# Copyright(c) 2011 LinBox
# Copyright(c)'1994-2009 by The Givaro group
# Imported and modified from Givaro's by Brice Boyer (briceboyer) <boyer.brice@gmail.com>
# see the COPYING file for more details.


set conf = configure.ac
set ver  = Makefile.am
set autoinst = linbox-auto-install.sh

#verbatim second argument of AC_INIT
set verb = `grep ^AC_INIT $conf | cut -d',' -f2`
#removes spaces and brackets
set vern = `echo "$verb" | sed 's/ //g;s/\[//;s/\]//'`
echo "Current version is $vern."

echo -n "Increment library version ? (y/n)"
set answ = $<
if ("$answ" == "y") then
	set line = `fgrep -n  ^AC_INIT $conf  | cut -d':' -f1`  #gets the line
    set macro = `echo "$vern" | cut -d'.' -f1` #a version number is macro.minor.micro
    set minor = `echo "$vern" | cut -d'.' -f2`
	set micro = `echo "$vern" | cut -d'.' -f3`
	set tmpfile = `mktemp` #tempfile
	set sedfile = `mktemp` #temp sed file
	set pmicro = `echo $micro`
    @ pmicro ++
	set pminor = `echo $minor`
    @ pminor ++
	set pmacro = `echo $macro`
    @ pmacro ++
	echo "Increment micro revision number ($vern -> $macro.$minor.$pmicro) ? press '0' "
	echo "Increment minor revision number ($vern -> $macro.$pminor.0) ? press '1' "
	echo -n "Increment macro revision number ($vern -> $pmacro.0.0) ? press '2' "
	set increm = $<
	switch ($increm)
		case 0:
			set newv = "[$macro.$minor.$pmicro]"
			breaksw
		case 1:
			set newv = "[$macro.$pminor.0]"
			breaksw
		case 2:
			set newv = "[$pmacro.0.0]"
			breaksw
		default:
			set newv = "$verb"
			echo "'$increm' read. Not incrementing anything."
			breaksw
	endsw

	#replacing [ ] and . with escaped version for sed would understand them as 'operators'
	echo "$line s/$verb/$newv/" | sed 's/\./\\\./g;s/\[/\\\[/g;s/\]/\\\]/g' > $sedfile
	sed -f $sedfile $conf > $tmpfile
	#clean up
	\rm -f $sedfile
	#diff for changes
	diff -u0 $conf $tmpfile
	#if something was changed, confirm incrementation :
    if ("$newv" != "$verb") then
		echo -n "Confirmation of incrementation ? (yes/no)"
		set answ = $<
            set backupconf = $conf.back$$
		if ("$answ" == "yes") then
			\cp -p $conf $backupconf
            echo "Back-up of $conf made in $backupconf. Now overwriting $conf."
			\mv -f $tmpfile $conf
		else
			echo "'$answ' read. Not incrementing anything."
			\rm -f $tmpfile
			exit 0 ;
		endif
		#now change Makefile accordingly
		echo -n "Incrementing Makefile revision accordingly"
		set tmpfile = `mktemp` #tempfile
		set sedfile = `mktemp` #tempfile
		switch ($increm)
			case 0:
				echo -n "s/VERSION.*/VERSION=$macro.$minor.$pmicro/"  >> $sedfile
				breaksw
			case 1:
				echo "s/VERSION.*/VERSION=$macro.$pminor.0/"  > $sedfile
				breaksw
			case 2:
				echo "s/VERSION.*/VERSION=$pmacro.0.0/"  > $sedfile
				breaksw
			default:
				echo "Something abnormal happened"
				exit 1
				breaksw
		endsw
		sed -f $sedfile $ver > $tmpfile
		\rm -f $sedfile
		diff -u0 $ver $tmpfile
		echo -n "Confirmation of incrementation ? (yes/no) "
		set answ = $<
                set backupmake = $ver.back$$
		if ("$answ" == "yes") then
			\cp -p $ver $backupmake
			\mv -f $tmpfile $ver
		else
			echo "'$answ' read. Not incrementing anything."
			echo " your old $conf is restored..."
			\rm -f $tmpfile
			\mv -f $backupconf $conf
			exit 0
		endif
	#now change linbox-auto-install accordingly
		echo -n "Incrementing linbox-auto-install.sh revision accordingly"
		set tmpfile = `mktemp` #tempfile
		set sedfile = `mktemp` #tempfile
		switch ($increm)
			case 0:
				echo -n "s/STABLE_LB=.*/STABLE_LB=$macro.$minor.$pmicro/"  >> $sedfile
				breaksw
			case 1:
				echo "s/STABLE_LB=.*/STABLE_LB=$macro.$pminor.0/"  > $sedfile
				breaksw
			case 2:
				echo "s/STABLE_LB=.*/STABLE_LB=$pmacro.0.0/"  > $sedfile
				breaksw
			default:
				echo "Something abnormal happened"
				exit 1
				breaksw
		endsw
		sed -f $sedfile $autoinst > $tmpfile
		\rm -f $sedfile
		diff -u0 $autoinst $tmpfile
		echo -n "Confirmation of incrementation ? (yes/no) "
		set answ = $<
		if ("$answ" == "yes") then
			\mv -f $tmpfile $autoinst
			echo " your old $conf and $ver are destroyed..."
			\rm -f $backupconf $backupmake
		else
			echo "'$answ' read. Not incrementing anything."
			echo " your old $conf and $ver are restored..."
			\rm -f $tmpfile
			\mv -f $backupconf $conf
			\mv -f $backupmake $ver
			exit 0
		endif
	endif
else
	echo "'$answ' read. Not doing anything."
endif

exit 0

