#! /bin/sh

# possible usage: $0 [-f] [version.m4] [version.def]
#
# -f would be 'force the update'

force=0
outputs=
for i in $*
do
    case "$i" in
     -f) force=1 ;;
     version.m4)
	outputs="version.m4 $outputs"
	;;
     *version.def)
	outputs="include/version.def $outputs"
	;;
     *version.texi)
	outputs="include/version.texi $outputs"
	;;
     *) echo "Unrecognized option: $i"
	exit 1
	;;
    esac
done

case "$outputs" in
 '') outputs="version.m4 include/version.def include/version.texi" ;;
esac

set -e

. ./packageinfo.sh

dversion=`scripts/VersionName`

set +e

case "$outputs" in
 *version.m4*)
    echo "m4_define([VERSION_NUMBER],[${dversion}])" > /tmp/version.m4+
    cmp -s /tmp/version.m4+ version.m4
    rc=$?
    case "$force$rc" in
     00)
	rm -f /tmp/version.m4+
	;;
     *)
	mv /tmp/version.m4+ version.m4
	;;
    esac
    ;;
esac

case "$outputs" in
 *version.def*)
    echo "version = '${dversion}';" > /tmp/version.def+
    cmp -s /tmp/version.def+ include/version.def
    rc=$?
    case "$force$rc" in
     00)
	rm -f /tmp/version.def+
	;;
     *)
	mv /tmp/version.def+ include/version.def
	;;
    esac
    ;;
esac

case "$outputs" in
 *version.texi*)
    echo "@set UPDATED `date +'%d %B %Y'`"	>  /tmp/version.texi+
    echo "@set EDITION $dversion"		>> /tmp/version.texi+
    echo "@set VERSION $dversion"		>> /tmp/version.texi+
    cmp -s /tmp/version.texi+ include/version.texi
    rc=$?
    case "$force$rc" in
     00)
	rm -f /tmp/version.texi+
	;;
     *)
	mv /tmp/version.texi+ include/version.texi
	;;
    esac
    ;;
esac
