#! /usr/local/bin/tcsh -f
#
# CreateNewModule.csh module_name
#
# This script creates a skeleton package under directory 'module_name'
#
# module_name
#    |
#    |--Makefile.am
#    |--autogen.sh
#    |--configure.in
#    |--src
#    |    |--Makefile.am
#    |--doc
#    |    |--Makefile.am
# /\ /\ /\ /\ mandatory files, anytime, anywhere.
#
#    |--AUTHORS and so on are GNU preference (can be remove if AUTOMAKE_OPTIONS = foreign is
#                                             used in Makefile.am)
#

if ( $#argv != 1 ) then
  echo
  echo 'Usage: CreateNewModule.csh module_name'
  echo
  exit
endif

set module = $argv 

if ( -e $module ) then
  echo
  echo 'A directory called ' $module ' already exits.'
  echo
  exit
endif

mkdir -p $module
cd $module
mkdir -p src doc
touch configure.in
touch Makefile.am
touch autogen.sh
touch src/$module.h
touch src/Makefile.am
touch doc/Makefile.am
touch AUTHORS
touch COPYING
touch ChangeLog
touch INSTALL
touch NEWS
touch README
touch THANKS
touch TODO
touch WARNING

set FILE = Makefile.am

echo 'EXTRA_DIST = reconf configure' >> $FILE
echo 'SUBDIRS = src doc' >> $FILE

set FILE = autogen.sh

echo '#! /bin/sh ' >> $FILE
echo 'srcdir=`dirname $0`' >> $FILE
echo 'test -z "$srcdir" && srcdir=.' >> $FILE
echo 'ORIGDIR=`pwd`' >> $FILE
echo 'cd $srcdir' >> $FILE
echo 'PROJECT='$module >> $FILE
echo 'TEST_TYPE=-f' >> $FILE
echo 'FILE=configure.in' >> $FILE
echo 'DIE=0' >> $FILE
echo '(libtool --version) < /dev/null > /dev/null 2>&1 || {'>>$FILE
echo '       echo'>>$FILE
echo '        echo "You must have libtool installed to compile $PROJECT."'>>$FILE
echo '        echo "Download the appropriate package for your distribution,"'>>$FILE
echo '        echo "or get the source tarball at ftp://ftp.gnu.org/pub/gnu/"'>>$FILE
echo '        DIE=1'>>$FILE
echo '}'>>$FILE
echo '(autoconf --version) < /dev/null > /dev/null 2>&1 || {'>>$FILE
echo '        echo'>>$FILE
echo '        echo "You must have autoconf installed to compile $PROJECT."'>>$FILE
echo '        echo "Download the appropriate package for your distribution,"'>>$FILE
echo '        echo "or get the source tarball at ftp://ftp.gnu.org/pub/gnu/"'>>$FILE
echo '        DIE=1'>>$FILE
echo '}'>>$FILE
echo '(automake --version) < /dev/null > /dev/null 2>&1 || {'>>$FILE
echo '        echo'>>$FILE
echo '        echo "You must have automake installed to compile $PROJECT."'>>$FILE
echo '        echo "Get ftp://sourceware.cygnus.com/pub/automake/automake-1.4.tar.gz"'>>$FILE
echo '        echo "(or a newer version if it is available)"'>>$FILE
echo '        DIE=1'>>$FILE
echo '}'>>$FILE
echo 'if test "$DIE" -eq 1; then'>>$FILE
echo '        exit 1'>>$FILE
echo 'fi'>>$FILE
echo 'if test -z "$*"; then'>>$FILE
echo '        echo "I am going to run ./configure with no arguments - if you wish "'>>$FILE
echo '        echo "to pass any to it, please specify them on the $0 command line."'>>$FILE
echo 'fi'>>$FILE
echo 'case $CC in'>>$FILE
echo '*xlc | *xlc\ * | *lcc | *lcc\ *) am_opt=--include-deps;;'>>$FILE
echo 'esac'>>$FILE
echo 'libtoolize --copy --force'>>$FILE
echo 'aclocal $ACLOCAL_FLAGS'>>$FILE
echo 'automake --copy --add-missing $am_opt'>>$FILE
echo 'autoconf'>>$FILE
echo 'cd $ORIGDIR'>>$FILE
echo '$srcdir/configure --enable-maintainer-mode "$@"'>>$FILE
echo 'echo '>>$FILE
echo 'echo "Now type 'make' to compile $PROJECT."'>>$FILE

chmod +x autogen.sh

set FILE = configure.in

echo 'dnl Process this file with autoconf to produce a configure script. '>>$FILE  
echo ''>>$FILE
echo 'AC_INIT(src/'$module'.h)'>>$FILE
echo ''>>$FILE
echo 'AC_CANONICAL_SYSTEM'>>$FILE
echo ''>>$FILE
echo 'dnl Select the compilers used'>>$FILE
echo ''>>$FILE
echo 'case "$host" in'>>$FILE
echo '*-*-linux*)'>>$FILE
echo '  CXX=g++'>>$FILE
echo '  CXXFLAGS=-g'>>$FILE
echo '  LDFLAGS=-g'>>$FILE
echo '  ;;'>>$FILE
echo '*-*-hpux*)'>>$FILE
echo '  CXX=aCC'>>$FILE
echo '  ;;'>>$FILE
echo '*)'>>$FILE
echo '  AC_MSG_ERROR([default compiler unknown for this system.])'>>$FILE
echo ' ;;'>>$FILE
echo 'esac     '>>$FILE
echo ''>>$FILE
echo 'dnl dnl --------------------------------------------------------------'>>$FILE
echo 'dnl dnl Example of checking for another package library or so...'>>$FILE
echo 'dnl dnl'>>$FILE
echo 'dnl AC_ARG_WITH(XXX,'>>$FILE
echo 'dnl	[--with-XXX=something Your help message here (printing by configure --help)],'>>$FILE
echo 'dnl	A_VARIABLE_NAME_XXX=$withval,'>>$FILE
echo 'dnl	A_VARIABLE_NAME_XXX=${some_predefined_environment_variable})'>>$FILE
echo 'dnl'>>$FILE
echo 'dnl AC_CHECK_FILE(${A_VARIABLE_NAME_XXX}/lib/libXXX.so,,'>>$FILE
echo 'dnl	AC_MSG_ERROR([can not find libXXX.so ! Use --with-XXX=]))'>>$FILE
echo 'dnl dnl The A_VARIABLE_NAME_XXX might then be used in the Makefile.am'>>$FILE
echo 'dnl AC_SUBST(A_VARIABLE_NAME_XXX)'>>$FILE
echo 'dnl '>>$FILE
echo 'dnl --------------------------------------------------------------'>>$FILE
echo 'dnl We need ROOT for our GUI'>>$FILE
echo 'dnl'>>$FILE
echo ''>>$FILE
echo 'AC_ARG_WITH(rootsys,'>>$FILE
echo '	[--with-rootsys=ROOTSYS Defines the ROOTSYS for ROOT],'>>$FILE
echo '	ROOTSYS=$withval, )'>>$FILE
echo ''>>$FILE
echo 'AC_CHECK_FILE(${ROOTSYS}/bin/root-config,'>>$FILE
echo '	,'>>$FILE
echo '        AC_MSG_ERROR([can not find ROOT! Check that ROOTSYS is set or use --with-rootsys])) '>>$FILE
echo 'ROOTLIBS="`$ROOTSYS/bin/root-config --libs`"'>>$FILE
echo 'ROOTGLIBS="`$ROOTSYS/bin/root-config  --glibs`"  '>>$FILE
echo 'ROOTFLAGS="`$ROOTSYS/bin/root-config --cflags`"'>>$FILE
echo ''>>$FILE
echo 'CXXFLAGS="$ROOTFLAGS $CXXFLAGS"'>>$FILE
echo 'AC_SUBST(CXXFLAGS)'>>$FILE
echo 'AC_SUBST(ROOTSYS) '>>$FILE
echo 'AC_SUBST(ROOTLIBS)'>>$FILE
echo 'AC_SUBST(ROOTGLIBS)'>>$FILE
echo 'AC_SUBST(ROOTFLAGS) '>>$FILE
echo ''>>$FILE
echo 'dnl Check for C++'>>$FILE
echo '	'>>$FILE
echo 'AC_PROG_CXX'>>$FILE
echo ''>>$FILE
echo 'AM_INIT_AUTOMAKE(' $module ', 0.1.0)'>>$FILE
echo 'AM_DISABLE_STATIC'>>$FILE
echo 'AM_PROG_LIBTOOL'>>$FILE
echo ''>>$FILE
echo 'dnl AM_CONFIG_HEADER(config.h)'>>$FILE
echo ''>>$FILE
echo 'dnl Checks for header files.'>>$FILE
echo 'AC_HEADER_STDC'>>$FILE
echo 'AC_CHECK_HEADERS(unistd.h)'>>$FILE
echo ''>>$FILE
echo 'dnl Checks for typedefs, structures, and compiler characteristics.'>>$FILE
echo 'AC_C_CONST'>>$FILE
echo 'AC_C_INLINE'>>$FILE
echo ''>>$FILE
echo 'dnl Checks for libraries.'>>$FILE
echo 'AC_PATH_X'>>$FILE
echo 'AC_SUBST(x_libraries) '>>$FILE
echo ''>>$FILE
echo 'AC_OUTPUT(Makefile \'>>$FILE
echo ' src/Makefile \'>>$FILE
echo ' doc/Makefile)'>>$FILE


echo ----------------------
echo
echo 'We have created a '$module'.h file in '$module'/src directory.'
echo 'If you remove it, modify accordingly the '$module'/configure.in file'
echo
