#!/bin/sh

#
# $Id$
#
# Copyright (c) 2006, 2011, Raphael Manfredi
#
# Computes git current revision number, if possible, emitting it in
# the form of a #define for C perusal.
#
#----------------------------------------------------------------------
# This file is part of gtk-gnutella.
#
#  gtk-gnutella is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  gtk-gnutella is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with gtk-gnutella; if not, write to the Free Software
#  Foundation, Inc.:
#      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#----------------------------------------------------------------------
#

LC_ALL=C
export LC_ALL

TOP="$1"
FILE="$2"

if [ "x$FILE" = x ]; then
	FILE="&1"
	oldrev=''
else
	oldrev=`grep Revision "$FILE" 2>/dev/null | head -n1 | cut -d' ' -f4`
fi

. $TOP/scripts/git-version.sh >/dev/null
revnum="$VRev"
date="$DATE"

if [ "x$revnum" = "x$oldrev" -a -f $FILE ]; then
	exit 0
fi

{
stamp=`date +"%Y-%m-%d %H:%M:%d %z"`
cat <<EOF
/*
 * THIS FILE IS AUTOMATICALLY GENERATED -- DO NOT EDIT
 *
 * Generated by $0.
 */

EOF

if [ "x$revnum" = x ]; then
	echo '/* No change since last git tag */'
else
	XRev='$Revision'
	cat <<EOF
#undef GTA_BUILD
#define GTA_BUILD "$XRev: $revnum \$"
EOF
	if [ "x$DATE" != x ]; then
		cat <<EOF

#undef GTA_BUILD_DATE
#define GTA_BUILD_DATE "$DATE"
EOF
	fi
fi

} > "$FILE"

