Cleanup autotools files

General cleanup and enhancement of the autotools build files.

Signed-off-by: Geoff Levand <geoff@infradead.org>
diff --git a/Makefile.am b/Makefile.am
index 04c1edc..4e41fc0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -15,14 +15,19 @@
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
-if COND_DEBUG
- extra_cppflags = -DDEBUG
-else
- extra_cppflags = -DNDEBUG
-endif
-
 SUBDIRS = lib
 
+ACLOCAL_AMFLAGS = -I m4
+
+AM_CPPFLAGS = -I$(srcdir)/lib $(DEFAULT_CPPFLAGS)
+AM_CFLAGS = $(DEFAULT_CFLAGS)
+AM_LDFLAGS = -Llib
+
+LIBTOOL_DEPS = @LIBTOOL_DEPS@
+
+libtool: $(LIBTOOL_DEPS)
+	$(SHELL) ./config.status libtool
+
 composed_bin_scripts =
 
 bin_SCRIPTS = $(composed_bin_scripts)
@@ -44,21 +49,6 @@
 
 man_MANS = $(composed_mans)
 
-AM_CPPFLAGS = -I$(srcdir)/lib
-
-AM_CFLAGS = $(extra_cppflags) \
-	-g \
-	-Wall \
-	-W \
-	-Wunused \
-	-Wstrict-prototypes \
-	-Wmissing-prototypes \
-	-Wmissing-declarations \
-	-Wredundant-decls \
-	-Winline
-
-AM_LDFLAGS = -Llib
-
 bin_PROGRAMS = ps3-video-mode
 sbin_PROGRAMS = ps3-flash-util
 
@@ -135,9 +125,14 @@
 composed_files = $(composed_mans) $(composed_bin_scripts) \
 	$(composed_sbin_scripts)
 
-EXTRA_DIST = bootstrap configure.ac.in $(addsuffix .in, $(composed_files))
+EXTRA_DIST = bootstrap configure.ac.in $(srcdir)/m4 \
+	$(addsuffix .in, $(composed_files))
 
-MAINTAINERCLEANFILES = aclocal.m4 config.* configure configure.ac depcomp \
+MAINTAINERCLEANFILES = config.* configure configure.ac depcomp \
 	install-sh ltmain.sh 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 3323f9d..1218e9f 100755
--- a/bootstrap
+++ b/bootstrap
@@ -23,26 +23,17 @@
 
 	# If available, use the git commit revision for the package version.
 
-	months="Jan:01 Feb:02 Mar:03 Apr:04 May:05 Jun:06 Jul:07 Aug:08 \
-		Sep:09 Oct:10 Nov:11 Dec:12"
-
 	# Add a date prefix for easy reading.
+	# date='2010-11-30 16:36:09 -0800'
 
-	date=$(git log --pretty=format:"%cD" -1 HEAD)
+	date=$(git log --pretty=format:"%ci" -1 HEAD)
+	date=${date##20}
+	date=${date%%:[0-9][0-9] *}
+	date=${date//-/.}
+	date=${date// /.}
+	date=${date//:/.}
 
-	# date='Fri, 2 Aug 2007 19:36:06 -0700'
-
-	date=${date##*, }
-	date=${date% [0-9][0-9]:*}
-	day=${date%* [A-Z][a-z][a-z] *}
-	year=${date##* [A-Z][a-z][a-z] 20}
-	month=${date##${day} }
-	month=${month%% 20${year}}
-	month=${months##*${month}:}
-	month=${month%% *}
-	[ ! ${day#[0-9][0-9]} ] || day="0$day"
-
-	version=$(printf '%s.%s.%s-%s%s' ${year} ${month} ${day} g ${head})
+	version=$(printf '%s-%s%s' ${date} g ${head})
 
 	# Add a '-dirty' postfix for uncommitted changes.
 
@@ -50,7 +41,7 @@
 		version=`printf '%s%s' ${version} -dirty`
 	fi
 else
-	# Default to date and time.
+	# Default to current date and time.
 
 	version="dev-$(date +%y.%m.%d-%H.%M.%S)"
 fi
diff --git a/configure.ac.in b/configure.ac.in
index 8dabe01..a927611 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -15,17 +15,51 @@
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
-AC_INIT([ps3-utils], [@version@], [Geoff Levand <geoffrey.levand@am.sony.com>])
+AC_INIT([ps3-utils], [@version@], [Geoff Levand <geoff@infradead.org>])
+
+AC_CONFIG_MACRO_DIR([m4])
 
 AC_PREFIX_DEFAULT([/usr/local])
 
-AC_ARG_ENABLE([debug],
-	[AS_HELP_STRING([--enable-debug], [enable debugging support])])
-AM_CONDITIONAL([COND_DEBUG], [test "x${enable_debug}" = "xyes"])
+AS_IF([test "x$CFLAGS" = "x"], [AC_SUBST([CFLAGS], [""])])
 
+AC_PROG_CC
+AC_PROG_INSTALL
 AC_PROG_LIBTOOL
+
 AM_INIT_AUTOMAKE
+LT_INIT
+
+default_cflags="--std=gnu99 -g \
+	-Wall -W -Wunused -Wstrict-prototypes -Wmissing-prototypes \
+	-Wmissing-declarations -Wredundant-decls"
+
+AC_ARG_ENABLE(
+	[werror],
+	[AS_HELP_STRING([--enable-werror],
+		[build programs with -Werror]
+	)],
+	[default_cflags="$default_cflags -Werror"]
+)
+
+AC_ARG_ENABLE(
+	[debug],
+	[AS_HELP_STRING([--enable-debug],
+		[build programs with extra debug info [default=no]]
+	)],
+	[],
+	[enable_debug=check]
+)
+
+AS_IF(
+	[test "x$enable_debug" = "xyes"],
+	[AC_SUBST([DEFAULT_CFLAGS], ["$default_cflags -O0 -DDEBUG"])],
+	[AC_SUBST([DEFAULT_CFLAGS], ["$default_cflags -O2 -DNDEBUG"])]
+)
+
+AC_SUBST([LIBTOOL_DEPS])
 AC_SUBST([DESTDIR])
+
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([Makefile])
 AC_CONFIG_SUBDIRS([lib])
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 0119bf4..252bd00 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -15,25 +15,18 @@
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
-if COND_DEBUG
- extra_cppflags = -DDEBUG
-else
- extra_cppflags = -DNDEBUG
-endif
+ACLOCAL_AMFLAGS = -I m4
+
+AM_CPPFLAGS = $(DEFAULT_CPPFLAGS)
+AM_CFLAGS = $(DEFAULT_CFLAGS)
+
+LIBTOOL_DEPS = @LIBTOOL_DEPS@
+
+libtool: $(LIBTOOL_DEPS)
+	$(SHELL) ./config.status libtool
 
 include_HEADERS = ps3-flash.h ps3-av.h
 
-AM_CFLAGS = $(extra_cppflags) \
-	-g \
-	-Wall \
-	-W \
-	-Wunused \
-	-Wstrict-prototypes \
-	-Wmissing-prototypes \
-	-Wmissing-declarations \
-	-Wredundant-decls \
-	-Winline
-
 lib_LTLIBRARIES = libps3-utils.la
 
 # version-info rules (current:revision:age):
@@ -50,6 +43,9 @@
 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
+EXTRA_DIST = bootstrap configure.ac.in  $(srcdir)/m4
 
-MAINTAINERCLEANFILES = aclocal.m4 config.* configure configure.ac Makefile.in
+MAINTAINERCLEANFILES = config.* configure configure.ac Makefile.in
+
+maintainer-clean-local:
+	rm -rf m4
diff --git a/lib/bootstrap b/lib/bootstrap
index 68037f1..d4aa15a 100755
--- a/lib/bootstrap
+++ b/lib/bootstrap
@@ -23,26 +23,17 @@
 
 	# If available, use the git commit revision for the package version.
 
-	months="Jan:01 Feb:02 Mar:03 Apr:04 May:05 Jun:06 Jul:07 Aug:08 \
-		Sep:09 Oct:10 Nov:11 Dec:12"
-
 	# Add a date prefix for easy reading.
+	# date='2010-11-30 16:36:09 -0800'
 
-	date=$(git-log --pretty=format:"%cD" -1 HEAD)
+	date=$(git log --pretty=format:"%ci" -1 HEAD)
+	date=${date##20}
+	date=${date%%:[0-9][0-9] *}
+	date=${date//-/.}
+	date=${date// /.}
+	date=${date//:/.}
 
-	# date='Fri, 2 Aug 2007 19:36:06 -0700'
-
-	date=${date##*, }
-	date=${date% [0-9][0-9]:*}
-	day=${date%* [A-Z][a-z][a-z] *}
-	year=${date##* [A-Z][a-z][a-z] 20}
-	month=${date##${day} }
-	month=${month%% 20${year}}
-	month=${months##*${month}:}
-	month=${month%% *}
-	[ ! ${day#[0-9][0-9]} ] || day="0$day"
-
-	version=$(printf '%s.%s.%s-%s%s' ${year} ${month} ${day} g ${head})
+	version=$(printf '%s-%s%s' ${date} g ${head})
 
 	# Add a '-dirty' postfix for uncommitted changes.
 
@@ -50,7 +41,7 @@
 		version=`printf '%s%s' ${version} -dirty`
 	fi
 else
-	# Default to date and time.
+	# Default to current date and time.
 
 	version="dev-$(date +%y.%m.%d-%H.%M.%S)"
 fi
diff --git a/lib/configure.ac.in b/lib/configure.ac.in
index dd25151..f5cf4e8 100644
--- a/lib/configure.ac.in
+++ b/lib/configure.ac.in
@@ -15,24 +15,66 @@
 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
-AC_INIT([libps3-utils], [@version@], [Geoff Levand <geoffrey.levand@am.sony.com>])
+AC_INIT([libps3-utils], [@version@], [Geoff Levand <geoff@infradead.org>])
+
+AC_CONFIG_MACRO_DIR([m4])
 
 AC_PREFIX_DEFAULT([/usr/local])
 
-AC_ARG_ENABLE([debug],
-	[AS_HELP_STRING([--enable-debug], [enable debugging support])])
-AM_CONDITIONAL([COND_DEBUG], [test "x${enable_debug}" = "xyes"])
+AS_IF([test "x$CFLAGS" = "x"], [AC_SUBST([CFLAGS], [""])])
 
+AC_PROG_CC
+AC_PROG_INSTALL
 AC_PROG_LIBTOOL
+
 AM_INIT_AUTOMAKE
-AC_SUBST([DESTDIR])
+LT_INIT
+
+default_cflags="--std=gnu99 -g \
+	-Wall -W -Wunused -Wstrict-prototypes -Wmissing-prototypes \
+	-Wmissing-declarations -Wredundant-decls"
+
+AC_ARG_ENABLE(
+	[werror],
+	[AS_HELP_STRING([--enable-werror],
+		[build programs with -Werror]
+	)],
+	[default_cflags="$default_cflags -Werror"]
+)
+
+AC_ARG_ENABLE(
+	[debug],
+	[AS_HELP_STRING([--enable-debug],
+		[build programs with extra debug info [default=no]]
+	)],
+	[],
+	[enable_debug=check]
+)
+
+AS_IF(
+	[test "x$enable_debug" = "xyes"],
+	[AC_SUBST([DEFAULT_CFLAGS], ["$default_cflags -O0 -DDEBUG"])],
+	[AC_SUBST([DEFAULT_CFLAGS], ["$default_cflags -O2 -DNDEBUG"])]
+)
 
 AC_CHECK_HEADERS([asm/ps3av.h])
-AC_CHECK_DECLS([PS3AV_MODE_MASK, PS3AV_MODE_RGB, PS3AV_MODE_FULL, \
-PS3AV_MODE_WHITE, PS3AV_MODE_COLOR, PS3AV_MODE_DITHER, PS3AV_MODE_HDCP_OFF],
- [], [], [#include <asm/ps3av.h>])
+AC_CHECK_DECLS([PS3AV_MODE_MASK, \
+	PS3AV_MODE_RGB, \
+	PS3AV_MODE_FULL, \
+	PS3AV_MODE_WHITE, \
+	PS3AV_MODE_COLOR, \
+	PS3AV_MODE_DITHER, \
+	PS3AV_MODE_HDCP_OFF],
+	[],
+	[],
+	[#include <asm/ps3av.h>]
+)
+
+AC_SUBST([LIBTOOL_DEPS])
+AC_SUBST([DESTDIR])
 
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([Makefile])
+AC_CONFIG_SUBDIRS([lib])
 
 AC_OUTPUT