#!/bin/sh

# Get date (two formats)
if [ -n "$2" ]; then
    LONGDATE=`date -d "$2" "+%B %d, %Y"`
    SHORTDATE=`date -d "$2" "+%Y-%m-%d"`
else
    LONGDATE=`date "+%B %d, %Y"`
    SHORTDATE=`date "+%Y-%m-%d"`
fi

# Current version number
CURRENTVERSION=2.1.4

# Last date
LASTDATE=2024-08-01

if [ "x$1" = "x" ]
then
	echo "Usage: $0 <version number | \"newdate\"> [revision date]"
	echo ""
	echo "Run this script with the name of the new version (i.e \"1.4\") to"
	echo "update version number and modification date in files."
	echo "Use the \"newdate\" argument if you want to keep the current version"
	echo "number and just update the modification date."
	echo ""
	echo "Current version=$CURRENTVERSION"
	echo "Current Modification date=$LASTDATE"
	echo ""
	exit 1
fi

newversion=$1
if [ "x$newversion" = "xnewdate" ]
then
    newversion=$CURRENTVERSION
fi

# Update version number and release date in source code
perl -i -p -e "s/NDOMOD_VERSION \"[0-9].*\"/NDOMOD_VERSION \"$newversion\"/;" src/ndomod.c
perl -i -p -e "s/NDOMOD_DATE \"[0-9].*\"/NDOMOD_DATE \"$SHORTDATE\"/;" src/ndomod.c
perl -i -p -e "s/Last Modified: [0-9].*/Last Modified: $SHORTDATE/;" src/ndomod.c

perl -i -p -e "s/NDO2DB_VERSION \"[0-9].*\"/NDO2DB_VERSION \"$newversion\"/;" src/ndo2db.c
perl -i -p -e "s/NDO2DB_DATE \"[0-9].*\"/NDO2DB_DATE \"$SHORTDATE\"/;" src/ndo2db.c
perl -i -p -e "s/Last Modified: [0-9].*/Last Modified: $SHORTDATE/;" src/ndo2db.c

perl -i -p -e "s/LOG2NDO_VERSION \"[0-9].*\"/LOG2NDO_VERSION \"$newversion\"/;" src/log2ndo.c
perl -i -p -e "s/LOG2NDO_DATE \"[0-9].*\"/LOG2NDO_DATE \"$SHORTDATE\"/;" src/log2ndo.c
perl -i -p -e "s/Last Modified: [0-9].*/Last Modified: $SHORTDATE/;" src/log2ndo.c

perl -i -p -e "s/FILE2SOCK_VERSION \"[0-9].*\"/FILE2SOCK_VERSION \"$newversion\"/;" src/file2sock.c
perl -i -p -e "s/FILE2SOCK_DATE \"[0-9].*\"/FILE2SOCK_DATE \"$SHORTDATE\"/;" src/file2sock.c
perl -i -p -e "s/Last Modified: [0-9].*/Last Modified: $SHORTDATE/;" src/file2sock.c

perl -i -p -e "s/SOCKDEBUG_VERSION \"[0-9].*\"/SOCKDEBUG_VERSION \"$newversion\"/;" src/sockdebug.c
perl -i -p -e "s/SOCKDEBUG_DATE \"[0-9].*\"/SOCKDEBUG_DATE \"$SHORTDATE\"/;" src/sockdebug.c
perl -i -p -e "s/Last Modified: [0-9].*/Last Modified: $SHORTDATE/;" src/sockdebug.c

# Update version number and release date in configure script and configure.ac
perl -i -p -e "s/PKG_VERSION=.*/PKG_VERSION=\"$newversion\"/;" configure
perl -i -p -e "s/PKG_REL_DATE=.*\"/PKG_REL_DATE=\"$SHORTDATE\"/;" configure
perl -i -p -e "s/PKG_VERSION=.*/PKG_VERSION=\"$newversion\"/;" configure.ac
perl -i -p -e "s/PKG_REL_DATE=.*\"/PKG_REL_DATE=\"$SHORTDATE\"/;" configure.ac

# Update this file with version number and last date
perl -i -p -e "s/^CURRENTVERSION=.*/CURRENTVERSION=$newversion/;" update-version
perl -i -p -e "s/^LASTDATE=.*/LASTDATE=$SHORTDATE/;" update-version

