#!/bin/sh -
#
#   Copyright (C) 2005-2010 Oliver Fromme, Munich. All rights reserved.
#   Standard 2-clause BSD license and disclaimer apply, please refer to
#   the file /usr/share/examples/etc/bsd-style-copyright on FreeBSD
#   or ask the author for a copy:  olli@fromme.com or olli@secnetix.de
#
#   The purpose of this script is to modify the version string of the
#   FreeBSD kernel by appending a date accoding to the age of your
#   source tree, making it easier to tell exactly what sources your
#   kernel was built from, which is especially useful when tracking
#   changes or hunting bugs.  For example:  8.0-STABLE-20100122 or
#   9.0-CURRENT-20100217.
#
#   This script is intended to be used instead of "make kernel" in the
#   /usr/src directory, as part of a FreeBSD update.   Please refer to
#   the /usr/src/UPDATING file for the full procedure.
#
#   Use the -h option to get usage information.
#

ME="${0##*/}"

Usage()
{
	cat <<-tac >&2
		Usage:  $ME  [-t | -y] [-n] [foo=bar ...] [-- <make options>]
		Options:
		    -t   Use datestamp of today.
		    -y   Use datestamp of yesterday.
		    -n   Only print the datestamp, then exit.
		By default (neither -t nor -y is given), the datestamp is deduced from the
		mtimes of the source files.  Because that might give the wrong result,
		confirmation is requested from the user if stdin and stderr are terminals.
	tac
	exit 1
}

EXTRA=""
DATE_STAMP=""
PRINT_ONLY=false
DEBUG=false

while [ $# -gt 0 ]; do
	case "$1" in [!-]*=?*)	EXTRA="$EXTRA $1"; shift; continue ;; esac
	case "$1" in --)	shift; break ;; esac
	case "$1" in -*[!tyn]*|[!-]*)	Usage ;; esac
	case "$1" in -*t*)	DATE_STAMP=$(date +%Y%m%d) ;; esac
	case "$1" in -*y*)	DATE_STAMP=$(date -v -1d +%Y%m%d) ;; esac
	case "$1" in -*n*)	PRINT_ONLY=true ;; esac
	shift
done

NV="sys/conf/newvers.sh"
if [ ! -f $NV ]; then
	if [ -f /usr/src/$NV ]; then
		NV="/usr/src/$NV"
	else
		echo "${ME}: Can't find $NV in '.' nor in '/usr/src'!" >&2
		exit 1
	fi
fi
if $DEBUG ; then
	echo "Using $NV" >&2
fi

if [ -z "$DATE_STAMP" ]; then
	DATE_STAMP=$(date -r $(stat -f%m */* | sort -n | tail -1) +%Y%m%d)
	if [ -z "$DATE_STAMP" ]; then
		echo "${ME}: Unexpected error generating date stamp!" >&2
		exit 1
	fi
	if $DEBUG ; then
		echo "Using date stamp:  $DATE_STAMP" >&2
	fi
fi

eval $(awk '/^[	 ]*BRANCH=/ {print; exit}' $NV)

if [ -z "$BRANCH" ]; then
	echo "${ME}: Cannot locate BRANCH in $NV" >&2
	exit 1
fi

case "$BRANCH" in
	CURRENT|STABLE|RELEASE|PRERELEASE)
		;;
	ALPHA|ALPHA[1-9]|BETA|BETA[1-9]|GAMMA|GAMMA[1-9]|RC|RC[1-9])
		;;
	*)
		echo "${ME}: Unknown BRANCH name \"$BRANCH\"" >&2
		if ! $PRINT_ONLY; then
			while :; do
				echo -n 'Continue [y/n]? ' >&2
				read answer < /dev/tty
				case "$answer" in
					[Yy]|[Yy][Ee][Ss])
						break
						;;
					[Nn]|[Nn][Oo])
						echo "${ME}: Cancelled" >&2
						exit 1
						;;
				esac
				echo "Please enter 'y' or 'n'!" >&2
			done
		fi
		;;
esac

export BRANCH_OVERRIDE="${BRANCH}-${DATE_STAMP}"
if $DEBUG ; then
	echo "BRANCH_OVERRIDE=\"$BRANCH_OVERRIDE\"" >&2
fi

case $(pwd) in
	/usr/src)
		set -- "$@" BRANCH_OVERRIDE="$BRANCH_OVERRIDE" $EXTRA kernel
		;;
	*/compile/*)
		set -- "$@" BRANCH_OVERRIDE="$BRANCH_OVERRIDE" $EXTRA
		;;
	*)
		set -- "$@" BRANCH_OVERRIDE="$BRANCH_OVERRIDE" $EXTRA kernel
esac

echo "Executing the following command:"
echo "    make $*"

if $PRINT_ONLY; then
	exit 0
fi

if [ -t 0 -a -t 2 ]; then
	echo -n 'Please enter "y" to confirm: ' >&2
	read ANSWER
	case "$ANSWER" in
		[Yy]|[Yy][Ee][Ss])
			# OK.
			;;
		*)
			echo "Cancelled." >&2
			exit 1
			;;
	esac
fi

TT_S=$(date +%s)

make "$@"

TT_S=$(( $(date +%s) - $TT_S ))
TT_H=$(( $TT_S / 3600 ))
TT_S=$(( $TT_S % 3600 ))
TT_M=$(( $TT_S / 60 ))
TT_S=$(( $TT_S % 60 ))
if [ $TT_M -lt 10 ]; then
	TT_M=0$TT_M
fi
if [ $TT_S -lt 10 ]; then
	TT_S=0$TT_S
fi
echo "Time elapsed:  ${TT_H}:${TT_M}:${TT_S}" >&2

#-- 
