Configure cleanup

Signed-off-by: Geoff Levand <geoff@infradead.org>
diff --git a/.gitignore b/.gitignore
index 13e51c1..d740089 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,7 +27,6 @@
 config.h.in
 config.sub
 config.sub.orig
-configure.ac
 configure
 depcomp
 install-sh
diff --git a/Makefile.am b/Makefile.am
index 4e41fc0..6b70b7c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,19 +1,8 @@
 #
 #  Copyright 2007 Sony Corp.
 #
-#  This program 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; version 2 of the License.
-#
-#  This program 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 this program; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-#
+
+AUTOMAKE_OPTIONS = foreign
 
 SUBDIRS = lib
 
@@ -125,14 +114,10 @@
 composed_files = $(composed_mans) $(composed_bin_scripts) \
 	$(composed_sbin_scripts)
 
-EXTRA_DIST = bootstrap configure.ac.in $(srcdir)/m4 \
+EXTRA_DIST = bootstrap version.sh $(srcdir)/m4 \
 	$(addsuffix .in, $(composed_files))
 
-MAINTAINERCLEANFILES = config.* configure configure.ac depcomp \
-	install-sh ltmain.sh Makefile.in missing $(PACKAGE)-*.gz
+MAINTAINERCLEANFILES = aclocal.m4 compile config.* configure depcomp \
+	install-sh ltmain.sh m4/*.m4 Makefile.in missing $(PACKAGE)-*.gz
 
 CLEANFILES = $(composed_files) $(addsuffix .tmp, $(composed_files))
-
-maintainer-clean-local:
-	rm -rf m4
-
diff --git a/bootstrap b/bootstrap
index 1218e9f..e72dad6 100755
--- a/bootstrap
+++ b/bootstrap
@@ -1,59 +1,3 @@
-#! /bin/bash
-# bootstrap -- Use this script to create generated files from a VCS checkout
-#
-# This program 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, or (at your option)
-# any later version.
-#
-# This program 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 this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#!/bin/sh
 
-version=
-
-if [ "$1" ]; then
-	version=$1
-elif head=$(git rev-parse --short --verify HEAD 2>/dev/null); then
-
-	# If available, use the git commit revision for the package version.
-
-	# Add a date prefix for easy reading.
-	# date='2010-11-30 16:36:09 -0800'
-
-	date=$(git log --pretty=format:"%ci" -1 HEAD)
-	date=${date##20}
-	date=${date%%:[0-9][0-9] *}
-	date=${date//-/.}
-	date=${date// /.}
-	date=${date//:/.}
-
-	version=$(printf '%s-%s%s' ${date} g ${head})
-
-	# Add a '-dirty' postfix for uncommitted changes.
-
-	if git diff-index HEAD | read dummy; then
-		version=`printf '%s%s' ${version} -dirty`
-	fi
-else
-	# Default to current date and time.
-
-	version="dev-$(date +%y.%m.%d-%H.%M.%S)"
-fi
-
-set -x
-
-sed -e s,@version@,${version},g configure.ac.in > configure.ac
-
-aclocal
-libtoolize --force --copy
-autoheader
-automake --foreign --add-missing --copy
-autoconf
-
-pushd lib> /dev/null && ./bootstrap ${version} ; popd > /dev/null
+exec autoreconf -f -i
diff --git a/configure.ac.in b/configure.ac
similarity index 91%
rename from configure.ac.in
rename to configure.ac
index a927611..f6fa250 100644
--- a/configure.ac.in
+++ b/configure.ac
@@ -15,9 +15,11 @@
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
-AC_INIT([ps3-utils], [@version@], [Geoff Levand <geoff@infradead.org>])
+AC_INIT([ps3-utils],
+    [m4_esyscmd_s([./version.sh])],
+    [Geoff Levand <geoff@infradead.org>])
 
-AC_CONFIG_MACRO_DIR([m4])
+AC_CONFIG_MACRO_DIRS([m4])
 
 AC_PREFIX_DEFAULT([/usr/local])
 
@@ -30,6 +32,8 @@
 AM_INIT_AUTOMAKE
 LT_INIT
 
+AM_SILENT_RULES([yes])
+
 default_cflags="--std=gnu99 -g \
 	-Wall -W -Wunused -Wstrict-prototypes -Wmissing-prototypes \
 	-Wmissing-declarations -Wredundant-decls"
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4aa8e90..6d61d7d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2,6 +2,8 @@
 #  Copyright 2007 Sony Corp.
 #
 
+AUTOMAKE_OPTIONS = foreign
+
 ACLOCAL_AMFLAGS = -I m4
 
 AM_CPPFLAGS = $(DEFAULT_CPPFLAGS)
@@ -30,9 +32,7 @@
 libps3_utils_la_SOURCES = flash.c flash-db.c
 libps3_utils_la_LDFLAGS = -version-info 3:0:1 ${EXTRA_LDFLAGS}
 
-EXTRA_DIST = bootstrap configure.ac.in  $(srcdir)/m4
+EXTRA_DIST = bootstrap version.sh $(srcdir)/m4
 
-MAINTAINERCLEANFILES = config.* configure configure.ac Makefile.in
-
-maintainer-clean-local:
-	rm -rf m4
+MAINTAINERCLEANFILES = aclocal.m4 compile config.* configure depcomp \
+	install-sh ltmain.sh m4/*.m4 Makefile.in missing
diff --git a/lib/bootstrap b/lib/bootstrap
index 9f28c42..e72dad6 100755
--- a/lib/bootstrap
+++ b/lib/bootstrap
@@ -1,44 +1,3 @@
-#! /bin/bash
-# bootstrap -- Use this script to create generated files from a VCS checkout
-#
+#!/bin/sh
 
-version=
-
-if [ "$1" ]; then
-	version=$1
-elif head=$(git rev-parse --short --verify HEAD 2>/dev/null); then
-
-	# If available, use the git commit revision for the package version.
-
-	# Add a date prefix for easy reading.
-	# date='2010-11-30 16:36:09 -0800'
-
-	date=$(git log --pretty=format:"%ci" -1 HEAD)
-	date=${date##20}
-	date=${date%%:[0-9][0-9] *}
-	date=${date//-/.}
-	date=${date// /.}
-	date=${date//:/.}
-
-	version=$(printf '%s-%s%s' ${date} g ${head})
-
-	# Add a '-dirty' postfix for uncommitted changes.
-
-	if git diff-index HEAD | read dummy; then
-		version=`printf '%s%s' ${version} -dirty`
-	fi
-else
-	# Default to current date and time.
-
-	version="dev-$(date +%y.%m.%d-%H.%M.%S)"
-fi
-
-set -x
-
-sed -e s,@version@,${version},g configure.ac.in > configure.ac
-
-aclocal
-libtoolize --force --copy
-autoheader
-automake --foreign --add-missing --copy
-autoconf
+exec autoreconf -f -i
diff --git a/lib/configure.ac.in b/lib/configure.ac
similarity index 89%
rename from lib/configure.ac.in
rename to lib/configure.ac
index bd8704a..e332c57 100644
--- a/lib/configure.ac.in
+++ b/lib/configure.ac
@@ -2,9 +2,11 @@
 #  Copyright 2007 Sony Corp.
 #
 
-AC_INIT([libps3-utils], [@version@], [Geoff Levand <geoff@infradead.org>])
+AC_INIT([libps3-utils],
+    [m4_esyscmd_s([./version.sh])],
+    [Geoff Levand <geoff@infradead.org>])
 
-AC_CONFIG_MACRO_DIR([m4])
+AC_CONFIG_MACRO_DIRS([m4])
 
 AC_PREFIX_DEFAULT([/usr/local])
 
@@ -62,6 +64,5 @@
 
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([Makefile])
-AC_CONFIG_SUBDIRS([lib])
 
 AC_OUTPUT
diff --git a/lib/m4/README b/lib/m4/README
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/m4/README
diff --git a/lib/version.sh b/lib/version.sh
new file mode 100755
index 0000000..32fd6ec
--- /dev/null
+++ b/lib/version.sh
@@ -0,0 +1,46 @@
+#! /bin/bash
+#
+# version.sh: create a version string for use by configure.ac
+
+version=
+datefmt='%Y%m%d'
+
+export GIT_DIR=$(dirname $0)/.git/
+
+if head=$(git rev-parse --short=8 --verify HEAD 2>/dev/null); then
+
+	suffix=''
+	# Add a '-dirty' suffix for uncommitted changes.
+	if git diff-index HEAD | read dummy; then
+		suffix=-dirty
+	fi
+
+	if tag=$(git describe --tags --exact-match 2>/dev/null); then
+		# use a tag; remove any 'v' prefix from v<VERSION> tags
+		tag=${tag#v}
+		version=$(printf "%s%s" ${tag} ${suffix})
+	else
+		# Use the git commit revision for the package version, and add
+		# a date prefix for easy comparisons.
+		date=$(git log --pretty=format:"%ct" -1 HEAD)
+		version=$(printf "%($datefmt)T.g%s%s" ${date} ${head} ${suffix})
+	fi
+else
+	# Check if a specific version is set, eg: by buildroot
+	if [ ! -z "$PACKAGE_VERSION" ];
+	then
+		# Full git hash
+		len=$(echo -n "${PACKAGE_VERSION}" | wc -c)
+		if [[ ${len} == 40 ]]; then
+			version=`echo -n ${PACKAGE_VERSION} | \
+				sed "s/^\([0-9a-f]\{7\}\).*/\1/;"`
+		else
+			version="$PACKAGE_VERSION"
+		fi
+	else
+		# Default to current date and time.
+		version="$(date +dev.$datefmt)"
+	fi
+fi
+
+echo $version
diff --git a/m4/README b/m4/README
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/m4/README
diff --git a/version.sh b/version.sh
new file mode 100755
index 0000000..32fd6ec
--- /dev/null
+++ b/version.sh
@@ -0,0 +1,46 @@
+#! /bin/bash
+#
+# version.sh: create a version string for use by configure.ac
+
+version=
+datefmt='%Y%m%d'
+
+export GIT_DIR=$(dirname $0)/.git/
+
+if head=$(git rev-parse --short=8 --verify HEAD 2>/dev/null); then
+
+	suffix=''
+	# Add a '-dirty' suffix for uncommitted changes.
+	if git diff-index HEAD | read dummy; then
+		suffix=-dirty
+	fi
+
+	if tag=$(git describe --tags --exact-match 2>/dev/null); then
+		# use a tag; remove any 'v' prefix from v<VERSION> tags
+		tag=${tag#v}
+		version=$(printf "%s%s" ${tag} ${suffix})
+	else
+		# Use the git commit revision for the package version, and add
+		# a date prefix for easy comparisons.
+		date=$(git log --pretty=format:"%ct" -1 HEAD)
+		version=$(printf "%($datefmt)T.g%s%s" ${date} ${head} ${suffix})
+	fi
+else
+	# Check if a specific version is set, eg: by buildroot
+	if [ ! -z "$PACKAGE_VERSION" ];
+	then
+		# Full git hash
+		len=$(echo -n "${PACKAGE_VERSION}" | wc -c)
+		if [[ ${len} == 40 ]]; then
+			version=`echo -n ${PACKAGE_VERSION} | \
+				sed "s/^\([0-9a-f]\{7\}\).*/\1/;"`
+		else
+			version="$PACKAGE_VERSION"
+		fi
+	else
+		# Default to current date and time.
+		version="$(date +dev.$datefmt)"
+	fi
+fi
+
+echo $version