From 46676a4268f0cf8096cf67c3c9e9fe081a8d2cbf Mon Sep 17 00:00:00 2001
From: Steven Michalske <smichalske@gmail.com>
Date: Wed, 24 Jun 2009 18:26:18 -0700
Subject: [PATCH] Build info script

This patch adds a script that updates the build information in the about
dialog box.

If the file releaseInfo.txt exists in base source directory it's
defines are echoed into the build_info.h file this way a release tag can
be made.  We might want to extend this to include git tags, but I feel
the sha sum is enough.
---
 scripts/buildInfo.sh |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/.gitignore       |    1 +
 src/Makefile.am      |    7 ++++
 src/misc.c           |    9 +++++
 4 files changed, 100 insertions(+), 0 deletions(-)
 create mode 100755 scripts/buildInfo.sh

diff --git a/scripts/buildInfo.sh b/scripts/buildInfo.sh
new file mode 100755
index 0000000..aae8fc8
--- /dev/null
+++ b/scripts/buildInfo.sh
@@ -0,0 +1,83 @@
+#!/bin/bash
+
+if [ "x$1" == "x" ]
+then
+	echo "Must supply a filename for the build information header." >&2
+	exit -1
+fi
+
+FNAME=$1
+FNAMEDEF=${1/\./_}
+
+echo "#ifndef __${FNAMEDEF}__" > ${FNAME}.tmp
+echo "#define __${FNAMEDEF}__" >> ${FNAME}.tmp
+
+echo "//Automatically generated by $0" >> ${FNAME}.tmp
+
+if [ -e ../releaseInfo.txt ]
+then
+	grep "#define" ../releaseInfo.txt | grep -v USERNAME | grep -v __ >> ${FNAME}.tmp
+else
+	if [ -e `which git` -a x`git rev-parse --is-inside-work-tree 2>/dev/null` == "xtrue" ]
+	then  # Use info from git if avilable
+		USERNAME=`git config --get user.name`
+		GIT_SHA=`git rev-parse HEAD`
+		GIT_SHORT=`git rev-parse --short HEAD`
+		UNTRACKED=`git ls-files --others --exclude-standard | wc -l`
+
+		DIRTY=""
+		if [ $UNTRACKED -gt 0 ]
+		then
+			DIRTY="-dirty"
+		fi
+
+		git update-index -q --refresh;
+		git diff-index --quiet --cached HEAD --ignore-submodules -- 	&&
+			git diff-files --quiet --ignore-submodules 					||
+			DIRTY="-dirty"
+
+		echo "#define GIT_SHORT \"${GIT_SHORT}\"" >> ${FNAME}.tmp
+		echo "#define GIT_SHORT_DIRTY \"${GIT_SHORT}$DIRTY\"" >> ${FNAME}.tmp
+		echo "#define GIT_SHA \"${GIT_SHA}\"" >> ${FNAME}.tmp
+		echo "#define GIT_SHA_DIRTY \"${GIT_SHA}$DIRTY\"" >> ${FNAME}.tmp
+		echo "#define WC_DIRTY \"$DIRTY\"" >> ${FNAME}.tmp
+	else
+		echo "#define GIT_SHORT \"UNKNOWN\"" >> ${FNAME}.tmp
+		echo "#define GIT_SHORT_DIRTY \"UNKNOWN\"" >> ${FNAME}.tmp
+		echo "#define GIT_SHA \"UNKNOWN\"" >> ${FNAME}.tmp
+		echo "#define GIT_SHA_DIRTY \"UNKNOWN\"" >> ${FNAME}.tmp
+		echo "#define WC_DIRTY \"UNKNOWN\"" >> ${FNAME}.tmp
+
+	fi
+fi
+
+if [ -z "$USERNAME" ]
+then
+	# Fall back to OS methods for determining username.
+	case `uname` in
+		Darwin )
+			USERNAME=`/usr/bin/dscl . -read ~ RealName | tr -d "\n" | sed -e "s/RealName: //" -e "s/^ *//" -e 's/ *$$//'`
+		;;
+		* )
+			echo "Add the ability to detect your operating systems username here." >&2
+			exit -1
+		;;
+	esac
+fi
+
+if [ "x$USERNAME" != "x" ]
+then
+	echo "#define USERNAME \"$USERNAME\"" >> ${FNAME}.tmp
+fi
+
+echo "#endif" >> ${FNAME}.tmp
+
+if [ -e ${FNAME} ]
+then
+	diff -wq ${FNAME} ${FNAME}.tmp || cat ${FNAME}.tmp > ${FNAME}
+else
+	 cat ${FNAME}.tmp > ${FNAME}
+fi
+
+rm -rf ${FNAME}.tmp
+
diff --git a/src/.gitignore b/src/.gitignore
index ed7ee9f..b23928a 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -9,3 +9,4 @@ res_parse.[ch]
 *pcb-menu.h
 pcbtest.sh
 pcb
+build_info.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 1c97b60..8f1d597 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -158,6 +158,7 @@ DBUS_SRCS= \
 
 BUILT_SOURCES = \
 	core_lists.h \
+	build_info.h \
 	gpcb-menu.h \
 	hid/gtk/gtk_lists.h \
 	hid/lesstif/lesstif_lists.h \
@@ -188,6 +189,12 @@ core_lists.h : ${PCB_SRCS} Makefile
 	(for f in ${PCB_SRCS} ; do cat $(srcdir)/$$f ; done) | grep "^REGISTER" > $@.tmp
 	mv $@.tmp $@
 
+#Always run this target. The script only updates the file if there is a change.
+.PHONY : build_info
+build_info.h : build_info
+build_info : Makefile
+	$(top_srcdir)/scripts/buildInfo.sh build_info.h
+
 # for globalconst.h
 INCLUDES=	-I$(top_srcdir) -I$(srcdir)/icons -I$(srcdir)/gts
 
diff --git a/src/misc.c b/src/misc.c
index e36b0ce..fe15ed5 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -79,6 +79,8 @@
 #include <dmalloc.h>
 #endif
 
+#include "build_info.h"
+
 RCSID ("$Id$");
 
 
@@ -2007,9 +2009,16 @@ GetInfoString (void)
       DSAddString (&info, "printed circuit board editor\n");
       DSAddString (&info, "version ");
       DSAddString (&info, VERSION);
+#ifdef GIT_SHA_DIRTY
+      DSAddString (&info, "\n\nGit SHA " GIT_SHA_DIRTY );
+#endif
       DSAddString (&info, "\n\n");
       DSAddString (&info, "Compiled on " __DATE__ " at " __TIME__);
+#ifdef USERNAME
+      DSAddString (&info, "\n\n" "by " USERNAME "\n\n");
+#else
       DSAddString (&info, "\n\n" "by harry eaton\n\n");
+#endif
       DSAddString (&info,
                    "Copyright (C) Thomas Nau 1994, 1995, 1996, 1997\n");
       DSAddString (&info, "Copyright (C) harry eaton 1998-2007\n");
-- 
1.6.3.2

