#!/bin/bash
#
# Create a GNU autoconf/automake based directory, capable
# of generating rpms, and maintainable under subversion

if [ $# -ne 3 ]
then
  echo usage: `basename $0` your-domain package-name start-version
  exit 1
fi

DIRNAME=$2

if [ -d $DIRNAME ]
then
  echo $DIRNAME already exists! Bailing out..
  exit 1
fi

mkdir $DIRNAME
cd $DIRNAME

# first make the bootstrap script
mkdir cfg
cat > cfg/bootstrap << _EOF_
#! /bin/sh
set -x

aclocal \
&& autoheader \
&& libtoolize \
&& autoconf \
&& automake --gnu --add-missing
_EOF_
chmod +x cfg/bootstrap

# Now generate configure.ac
cat > configure.ac << _EOF_
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_INIT($DIRNAME, $3, bugs@$1)
AC_CONFIG_SRCDIR(src/Makefile.am)
VERSION=\`cat VERSION\`
RELEASE=\`svnversion .\`
PACKAGE=$DIRNAME
AM_INIT_AUTOMAKE(\$PACKAGE,\$VERSION)
AC_SUBST(VERSION)
AC_DEFINE_UNQUOTED([RELEASE], ["\${RELEASE}"], [Package release string])
AC_CONFIG_HEADERS([config.h:config-h.in])

# Checks for programs.
#AC_PROG_CC
#AC_PROG_CPP
AC_PROG_INSTALL
#AC_PROG_LIBTOOL
#AC_EXEEXT

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([src/Makefile])

AC_OUTPUT
_EOF_

mkdir redhat

# Generate specfile
cat > redhat/$DIRNAME.spec.in << _EOF_
# for RH9 stop doing build debug stuff
%define debug_package %{nil}

Summary: .........................
Name: $DIRNAME
Version: $3
Release: 0
License: Proprietary
Group: XXXXX/XXXXX
Provides: %{name}
Source0: http://ftp.$1/src/$DIRNAME/%{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%(id -u -n)-buildroot
Packager: $LOGNAME@$1
Vendor: XXXXX <info@$1>

%description
..................................

%prep

%setup

%build

./configure
make

%install
rm -rf %{buildroot}
mkdir %{buildroot}
make DESTDIR=%{buildroot} install

%clean
rm -rf %{buildroot}

%files
%defattr(-, root, root)
%doc AUTHORS ChangeLog COPYING INSTALL NEWS README
_EOF_

cat > BuildRpm << _EOF_
#!/bin/sh

cfg/bootstrap
./configure
set -x

FEDVERSION=\`rpm -q fedora-release | cut -d- -f3\`
BASEDIR=\`basename \$PWD\`
SVNVERSION=\`svnversion .\`
RPMVERSION=\`head -1 VERSION\`

sed -e "s/^Version:.*/Version: \$RPMVERSION/g" \
    -e "s/^Release:.*/Release: \$SVNVERSION/g" redhat/$DIRNAME.spec.in > $DIRNAME.spec ;
cd ..
ln -sf \$BASEDIR $DIRNAME-\$RPMVERSION
tar czvf $DIRNAME-\$RPMVERSION.tar.gz --exclude=.svn $DIRNAME-\$RPMVERSION/.
rm -f $DIRNAME-\$RPMVERSION
rpmbuild -ta $DIRNAME-\$RPMVERSION.tar.gz
rm -f $DIRNAME-\$RPMVERSION.tar.gz
_EOF_
chmod +x BuildRpm

echo $3 > VERSION
touch NEWS README AUTHORS ChangeLog

cat > Makefile.am << _EOF_
SUBDIRS = src

EXTRA_DIST = $DIRNAME.spec cfg VERSION

dist-hook:
	rm -rf \`find \$(distdir) -name CVS -o -name .arch-ids -o name .svn\`
_EOF_

echo $LOGNAME@$1 > AUTHORS
echo "Unauthorized copying prohibited. All Rights Reserved" > COPYING
mkdir src
echo "#bin_PROGRAMS =" > src/Makefile.am

