#!/bin/sh

tag=INSERT-NEW-ADAPTORS-BEFORE-THIS-LINE 

curdir="`pwd`"
curdir=`basename "$curdir"`

if [ "$curdir" != db -o \! \( -w aclocal.m4 -a -w config.h.in -a -w config.mak.in \) ] ; then
    echo "${pname} should be run from the db directory, where "
    echo "aclocal.m4, config.h.in, config.mak.in, and configure.in lie."
    exit 1
fi


if [ $# -ne 1 ] ; then
    pname=`basename "$0"`
    echo "${pname} usage:"
    echo "     ${pname}   NEW_ADAPTOR_NAME"
    echo "NEW_ADAPTOR_NAME should be the name of the new adaptor as used"
    echo "to build the makefile/cpp variable NEW_ADAPTOR_NAME ## _DATABASE."
    echo "${pname} should be run from the db directory, where "
    echo "aclocal.m4, config.h.in, config.mak.in, and configure.in lie."
    exit 2
fi


name="$1"


if [ -n "`echo $name|sed -e 's/^[A-Za-z][A-Za-z0-9_]*$//'`" ] ; then
    echo "Invalid name for an adaptor: please use only alphanumerics and "
    echo "underline, and start with a letter."
    exit 3
fi


name_low=`echo $name|sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
name_cap="`echo $name|sed -e 's/^\(.\).*/\1/'``echo $name_low|sed -e 's/^.\(.*\)/\1/'`"


#
#   1-   Update 'aclocal.m4'.
#

echo 'Updating aclocal.m4...'
mv aclocal.m4 aclocal.m4~
cat << EOF > /tmp/$$.sed
/${tag}/i\\
dnl\\
dnl\\
dnl ${name}\\
dnl\\
AC_SUBST(${name}_DATABASE)dnl\\
AC_SUBST(${name}_CFLAGS)dnl\\
AC_SUBST(${name}_LDFLAGS)dnl\\
dnl\\
dnl\\
AC_DEFUN(AC_CHECK_${name},\\
[AC_ARG_WITH(${name_low}, [--with-${name_low}\\
    Build the ${name_cap} adaptor.\\
],\\
[if test \$withval = yes; then\\
    ${name}_DATABASE="yes"\\
else\\
    ${name}_DATABASE="no"\\
fi\\
],\\
[${name}_DATABASE=no\\
])\\
AC_SUBST(${name}_DATABASE)\\
if test \$${name}_DATABASE = yes; then\\
    AC_DEFINE(${name}_DATABASE)\\
    if test "\$${name}_CFLAGS" = ""; then\\
	${name}_CFLAGS="" dnl Please edit this.\\
    fi\\
    if test "\$${name}_LDFLAGS" = ""; then\\
	${name}_LDFLAGS="" dnl Please edit this.\\
    fi\\
fi\\
])dnl\\
dnl\\
dnl\\

EOF
sed -f /tmp/$$.sed  < aclocal.m4~ > aclocal.m4




#
#   2-   Update 'config.h.in'.
#

echo 'Updating config.h.in...'
mv config.h.in config.h.in~
cat << EOF > /tmp/$$.sed
/${tag}/i\\
#undef ${name}
EOF
sed -f /tmp/$$.sed  < config.h.in~ > config.h.in 



#
#   3-   Update 'config.mak.in'.
#

echo 'Updating config.mak.in...'
mv config.mak.in config.mak.in~
cat << EOF > /tmp/$$.sed
/${tag}/i\\
# $name adaptor\\
${name}_DATABASE = @${name}_DATABASE@\\
${name}_CFLAGS = @${name}_CFLAGS@\\
${name}_LDFLAGS = @${name}_LDFLAGS@\\

EOF
sed -f /tmp/$$.sed  < config.mak.in~ > config.mak.in



#
#   4-   Update 'configure.in'.
#

echo 'Updating configure.in...'
mv configure.in configure.in~
cat << EOF > /tmp/$$.sed
/${tag}/i\\
AC_CHECK_${name}
EOF
sed -f /tmp/$$.sed  < configure.in~ > configure.in 


#
#   5-   Regenerate 'configure'.
#

autoconf



#
#   6-   I'm lazy... 
#

echo 'Done.'
echo ''
echo "Please update 'eoadaptors/GNUmakefile' with the new subproject."
echo ''
echo 'Remember to insert something like:'
cat << EOF
# ----------------------------------------------------------------------
ifeq (\$(${name}_DATABASE),yes)
include \$(GNUSTEP_MAKEFILES)/bundle.make
include \$(GNUSTEP_MAKEFILES)/library.make
else
include \$(GNUSTEP_MAKEFILES)/rules.make
all::	
	@echo 
	@echo "Nothing will be done if you don't define ${name}_DATABASE=yes,"
	@echo "  either:    make ${name}_DATABASE=yes"
	@echo "  or:        ${name}_DATABASE=yes make"
	@echo "  or:        export ${name}_DATABASE=yes ; make"
	@echo "  or:        setenv ${name}_DATABASE yes ; make"
endif
# ----------------------------------------------------------------------
EOF
echo 'to the GNUmakefile of your adaptor subproject.'

exit 0


