#!/bin/sh
#
# gptg: general-project-template-generator
# Copyright (C) 2000,2001 karsten reincke <reincke@karubik.de>
# 
# a script to generate 
# (a) the file and directory-structure
#     which is nescessary to use the gnu automake / autoconfigure tools
# while
# (b) inserting the wished company-license (not only the gnu-gpl) and
# (c) including a c resp. c++ template-sourcefile-kit which
#     is already compilable.
#
# 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 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#  
# file <gptg> version <#1.3.2#> of project <GTGT>

VERSION="<#1.3.2#>"

# =============================
# == Usage related functions ==
# =============================

function toCapitalized
{
  local WORD=$1
  local FL
  local RL
  FL=`perl -e '$STRING=$ARGV[0];$STRING=~/^(.)/;print $1;exit' $WORD`
  FL=`echo "$FL" | tr [:lower:] [:upper:]`
  RL=`perl -e '$STRING=$ARGV[0];$STRING=~/^(.)(.*)/;print $2;exit' $WORD`
  echo "$FL$RL"
}

function toLower
{
  local VAR=`echo "$1" | tr [:upper:] [:lower:]`
  echo $VAR
}

function toUpper
{
  local VAR=`echo "$1" | tr [:lower:] [:upper:]`
  echo $VAR
}

function usage
{
 cat << EOF
Creates directory tree for new software package.
Usage: gptg OPTIONS [PROJECT] [RELEASE] [REVISION]

OPTIONS  :-
   -h, --help       This message
   -v, --version    Version information
   -cpp             Language C++, uses the LF macros
   -c               Language C, uses traditional autoconf macros

PROJECT  :-         projectname [myproject]
RELEASE  :-         start-release-number [0]
REVISION :-         start-release-branch-number [1]
     
repsect the following conditions ...
(a) PROJECT should be a small identifier without blanks or any other seperators.
(b) use numbers (integers) for RELEASE and REVISION think release-number as
    main-version-number and revision-number as programming-step-number 
    or simply follow cvs-terminology. do not use rational numbers 
    (whished number "1.4" must be: "release 1 revision 4")
... or your work might fail. there is (still) no input-check.

Bug reports: karsten@reincke.org
EOF

}

function version
{
 cat << EOF
gptg $VERSION - general packet-template generator -
generates directory tree for new software package

Copyright (C) 2000,2001 karsten reincke <reincke@karubik.de>

This is free software in the sense of GNU GPL, and you are 
welcome to redistribute it and modify it under certain conditions. 
There is ABSOLUTELY NO WARRANTY for this software.
For legal details see the GNU General Public License.

EOF

}

function get_copyright_data
{
  gcng -ecl
  
  AUT_NAME="$(cat gcng.conf | head -1 | tail -1)"
  AUT_EMAIL="$(cat gcng.conf | head -2 | tail -1)"
  AUT_YEAR="$(cat gcng.conf | head -3 | tail -1)"
  COMPANY="$(cat gcng.conf | head -4 | tail -1)"
  COMP_SHORT_COPYRIGHT_NOTE="$(cat gcng.conf | head -5 | tail -1)"
  COMP_LONG_COPYRIGHT_NOTE="$(cat gcng.conf | head -6 | tail -1)"
  COMP_LONG_COPYRIGHT_NOTE=`eval echo $COMP_LONG_COPYRIGHT_NOTE`
  COMP_SHORT_COPYRIGHT_NOTE=`eval echo $COMP_SHORT_COPYRIGHT_NOTE`

}

# =========================================
# == Make the standard information files ==
# =========================================

# ---------------------------------------------------
# This function creates a template NEWS file in the 
# present working directory.
# ---------------------------------------------------

function make_NEWS
{
  
 cat >> NEWS <<EOF
Project ${PROJECT} / release <#${RELEASE}#> -- History of visible changes.
`gcng -ecl`
Please send ${PROJECT} bug reports to ${AUT_EMAIL}.
-------------------------------------------------------
`gcng -ech`
Please see the file COPYING for details.
-------------------------------------------------------
`date -I` : generating the default project-development-template

EOF

}

# ----------------------------------------------------
# This function creates a template README file in the
# present working directory
# ----------------------------------------------------

function make_README
{
 cat >> README <<EOF
Welcome to the project ${PROJECT} / release <#${RELEASE}#>
`gcng -ecl`
-------------------------------------------------------
`gcng -ech`
Please see the file COPYING for details.
------------------------------------------------------- 
For documentation, please see the files in the doc subdirectory.
For building and installation instructions please see the INSTALL file.
EOF
}

# -------------------------------------------------------
# This function creates a template AUTHORS file in the
# present working directory
# -------------------------------------------------------

function make_AUTHORS
{
 cat >> AUTHORS <<EOF
Welcome to the project ${PROJECT} / release <#${RELEASE}#>
`gcng -ecl`
-------------------------------------------------------
`gcng -ech`
Please see the file COPYING for details.
------------------------------------------------------- 

Authors of the project ${PROJECT}.
(See also the files THANKS and ChangeLog.)

${AUT_NAME} <${AUT_EMAIL}>:
    --> initiator of ${PROJECT}.
EOF
}

# --------------------------------------------------------
# This function creates the template THANKS file in the
# present working directory
# --------------------------------------------------------

function make_THANKS
{
 cat >> THANKS <<EOF
Welcome to the project ${PROJECT} / release <#${RELEASE}#>
`gcng -ecl`
-------------------------------------------------------
`gcng -ech`
Please see the file COPYING for details.
------------------------------------------------------- 

${PROJECT} THANKS file

The project ${PROJECT} has originally been initiated by ${AUT_NAME}.
Many people have further contributed to ${PROJECT} by reporting problems, 
suggesting various improvements, or submitting actual code. Here is
a list of these people. Help me keep it complete and exempt of errors.
EOF
}


function make_ChangeLog
{
 
 cat >> ChangeLog <<EOF
Project ${PROJECT} / release <#${RELEASE}#> -- ChangeLog
`gcng -ecl`
-------------------------------------------------------
`gcng -ech`
Please see the file COPYING for details.
-------------------------------------------------------
`date -I` : generating the default project-development-template

EOF

}

function make_doclevel_makefile
{
    echo "updating Makefile.am for directory doc"
    cat >>Makefile.am << EOF
${PROJECT}_helpdir = \$(datadir)/doc/${project}/
${PROJECT}_help_DATA = ${project}_index.html
EXTRA_DIST = ${project}_index.html
EOF

}

function make_htmldoc
{
    cat >> ${project}_index.html << EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<!--
${PROJECT} release <#${RELEASE}#> -- index.html
`gcng -ech`
Please see the file COPYING for details.
-->
<head>
<title> ${PROJECT} - documentation </title>
</head>
<body bgcolor="#FFc545" text="#000000" link="#0000FF" vlink="READ" alink="#008000">
<h3><b><i>${PROJECT} - documentation</i></b></h3>
<h3><i>(0) content</i></h3>
<p>
	<ol>
	<li><a href="#gp">general purpose</a></li>
	<li><a href="#ia">involved applications</a></li>
	<li><a href="#ic">installation-conditions</a></li>
	<li><a href="#gr">general remarks</a></li>
	<li><a href="#sh">special hints for the use (handbook)</a></li>
	</ol>
</p>
<p>
copyright (c) ${AUT_YEAR} ${AUTNAME} <a href="mailto: ${AUT_EMAIL}">${AUT_EMAIL}</a>
</p>
<h3><i>(1) <a name="gp">general purpose</a></i></h3>
<h3><i>(2) <a name="ia">involved applications</a></i></h3>
	<dl>
		<dt><b><i>???</i></b></dt>
		<dd>... does ...</dd>
	</dl>

<h3><i>(3) <a name="ic">installation-conditions</a></i></h3>
<h3><i>(4) <a name="gr">general remarks</a></i></h3>
<h3><i>(5) <a name="sh">special hints for the use (handbook)</a></i></h3>
</body>
</html>

EOF
}

function make_scriptlevel_makefile
{
    echo "updating Makefile.am for directory scripts"
    cat >>Makefile.am << EOF

bin_SCRIPTS = ${project}.sh
#CLEANFILES = \$(bin_SCRIPTS)
EXTRA_DIST = ${project}.sh

${project}.sh:
	chmod ugo+x ${project}.sh  
EOF
}

function make_bashscript
{
    cat >> ${project}.sh << EOF
`gcng -sh ${PROJECT} ${project}.sh ${RELEASE}`    

echo "hello from ${project}.sh release <#${RELEASE}#>"

EOF
}

# ---------------------------------------------------------------
# This function creates a Makefile for an m4 subdirectory
# and provides also a copy of mkm4.pl in case the user wants it
# FIXME: Do I want a better way of dealing with this?
# ---------------------------------------------------------------

function make_m4_makefile
{
 cat >> Makefile.am <<EOF
# Install m4 macros in this directory
m4datadir = \$(datadir)/aclocal

# List your m4 macros here
m4macros = 

# The following is boilerplate
m4data_DATA = \$(m4macros) 
EXTRA_DIST = \$(m4data_DATA) 

EOF
}


# ==================================================
# == Routines for making LANGUAGE dependent files ==
# ==================================================

# --------------------------------------------------------------------
# These functions make the default configure.in and Makefile.am files
# The default configure.in file assumes that you are programming
# only in C++ and uses the LF macros instead of the standard
# Autoconf macros for configuring the compiler.
# It also includes a call to LF_CPP_PORTABILITY
# --------------------------------------------------------------------

# (1.A) toplevel configure for c++-program
function make_toplevel_configure_cpp
{
    echo "creating configure.in at toplevel (mode cpp)"
OUTL="Makefile"
for i in $WORK_SDL
do
    OUTL="$OUTL ${i}/Makefile"
done

    cat >> configure.in <<EOF
dnl process this file with autoconf to produce a configure script
dnl (1) -------- general startmacros
AC_INIT(reconf)
AM_INIT_AUTOMAKE(${project},${RELEASE})
AM_CONFIG_HEADER(config.h)
AC_PREFIX_DEFAULT(/usr/local/)
AC_PROG_MAKE_SET

dnl (2) -------- set of available languages

dnl (3) -------- checks for programs

AC_PROG_CC
AC_PROG_CPP
AC_AIX
AC_ISC_POSIX
AC_MINIX
AC_HEADER_STDC
AC_PROG_CXX
AC_PROG_CXXCPP

dnl (4) -------- checks for libraries
AC_PROG_LIBTOOL

dnl (5) -------- checks for header files
dnl (6) -------- checks for library-functions

dnl (7) -------- les autres petis choses
AC_PATH_PROGS(BASH, bash sh)

dnl (8) -------- determine whole directory-structure
AC_OUTPUT(${OUTL})
EOF
}

# (1.B) toplevel make-file for c++-program
function make_toplevel_makefile_cpp
{
    echo "updating Makefile.am at toplevel (mode cpp)"
    
    cat >> Makefile.am <<EOF
EXTRA_DIST = reconf configure ${ADD_TOPLEVEL_DIST}
SUBDIRS = m4 scripts doc lib src 
EOF
}

# (1.C) sub-make-file for doc + m4
function make_blank_makefile_cpp
{
    echo "creating blank-Makefile.am (mode cpp)"

    cat >> Makefile.am <<EOF
#EXTRA_DIST
#SUBDIRS
EOF
}

# (1.D.1) src-level-makefile for main + sister-module
function make_simolevel_makefile_cpp
{
    echo "updating Makefile.am (mode cpp)"

    cat >> Makefile.am <<EOF

CXXFLAGS = -DLinux -Wall -ansi -pedantic
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -DXOPEN_SOURCE=500
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -g
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -O3
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -g -pg

SUBDIRS=daughter-module
    
INCLUDES = -I\$(top_srcdir)/lib -I\$(top_srcdir)/src/daughter-module

bin_PROGRAMS = ${project}
${project}_SOURCES = \\
    ${project}.cc ${simoname}.cc ${simoname}.h
${project}_DEPENDENCIES = \\
    ../lib/lib${librname}.a daughter-module/${damoname}.o
${project}_LDADD = daughter-module/${damoname}.o -l${librname} 
${project}_LDFLAGS = -L\$(top_builddir)/lib -L\$(top_builddir)/src/daughter-module

EOF

}

# (1.D.2) src-level-main-source-file for c++
function make_simolevel_main_cpp
{
  echo "creating main-program (mode cpp)"
        
    cat > ${project}.cc <<EOF
`gcng -cc ${PROJECT} ${project}.cc ${RELEASE}`

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include "${simoname}.h"
#include <${damoname}.h>
#include <${librname}.h>

using namespace gtgt;

int main (int argn, char * argv[])
{

  ${Simoname} sisterModuleObject1;
  ${Simoname} sisterModuleObject2(1,2);

  ${Damoname} daughterModuleObject1;
  ${Damoname} daughterModuleObject2(1,2);

  ${Librname} libraryModuleObject1;
  ${Librname} libraryModuleObject2(1,2);

  cout << "hello from main of package ${PROJECT}" << endl;
  return 0;
} 

EOF
}

# (1.D.3) src-level-sister-module-file for c++
function make_simolevel_module_cpp
{
    echo "creating sister-module (mode cpp)"
    
# (1.D.3.1) fill header-file
    
    cat >> ${simoname}.h << EOF
`gscg -cpp -i ${Simoname} ${PROJECT} ${RELEASE}`

EOF

# (1.D.3.2) fill source-file
    cat > ${simoname}.cc << EOF
`gscg -cpp -s ${Simoname} ${PROJECT} ${RELEASE}`
EOF

}

# (1.D.4) dmod-level-makefile for daughter-module
function make_damolevel_makefile_cpp
{
    echo "updating Makefile.am for daughtermodule (mode cpp)"

    cat >> Makefile.am <<EOF

CXXFLAGS = -DLinux -Wall -ansi -pedantic
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -DXOPEN_SOURCE=500
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -g
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -O3
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -g -pg
    
noinst_LIBRARIES =lib${damoname}.a
lib${damoname}_a_SOURCES = \\
    ${damoname}.cc ${damoname}.h

EOF

}

# (1.D.5) src-level-daughter-module-file for c++
function make_damolevel_module_cpp
{
    echo "creating daughter-module (mode cpp)"
    
# (1.D.5.1) fill header-file
    
    cat >> ${damoname}.h << EOF
`gscg -cpp -i ${Damoname} ${PROJECT} ${RELEASE}`

EOF

# (1.D.5.2) fill source-file
    cat > ${damoname}.cc << EOF
`gscg -cpp -s ${Damoname} ${PROJECT} ${RELEASE}`
EOF

}


# (1.E.1) lib-level-makefile for libraries
function make_liblevel_makefile_cpp
{
    echo "updating lib-Makefile.am for directory lib (mode cpp)"

    cat >> Makefile.am << EOF
#EXTRA_DIST 
#SUBDIRS

CXXFLAGS = -DLinux -Wall -ansi -pedantic
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -DXOPEN_SOURCE=500
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -g
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -O3
#CXXFLAGS = -DLinux -Wall -ansi -pedantic -g -pg

include_HEADERS = ${librname}.h

lib_LIBRARIES = lib${librname}.a
lib${librname}_a_SOURCES = ${librname}.cc ${librname}.h

lib_LTLIBRARIES = lib${librname}.la
lib${librname}_la_SOURCES = ${librname}.cc ${librname}.h

lib${librname}_la_LDFLAGS = -version-info ${LIBRELEASE}

EOF

}

#(1.E.2.a) statical linkable library c++
function make_liblevel_statlib_cpp
{
    echo "creating static-library (mode cpp)"
    
# (1.E.2.a.I) fill header-file
    
    cat > ${librname}.h << EOF
`gscg -cpp -i ${Librname}  ${PROJECT} ${RELEASE}`
EOF

# (1.E.2.a.II) fill source-file
    cat > ${librname}.cc << EOF
`gscg -cpp -c ${Librname}  ${PROJECT} ${RELEASE}`
EOF

}


#(1.F.1) write reconfiguration-file for c++
function make_reconf_cpp
{
    echo "creating reconfiguration-file"
    
     cat >> reconf <<EOF
#!/bin/sh


TESTDIRS=\
"  /usr/local/gnu/share/aclocal \
   /usr/local/share/aclocal \
   /opt/gnome/share/aclocal \
   /usr/share/aclocal"

 ACLOCAL_PATH="";
for dir in \${TESTDIRS};
do
   if [ -d \${dir} ];
   then
     ACLOCAL_PATH="-I \${dir} \${ACLOCAL_PATH}"
   fi
done

DELCONF_FILES=\`ls config.*\`
DELETE_FILES=\
" config.cache \
   acconfig.h \
   Makefile \
   Makefile.in \
   aclocal.m4 \
   configure \
   \${DELCONF_FILES}"
for DF in \${DELETE_FILES}; 
do
  echo "deleting \${DF}"
  find . -name \${DF} -exec rm \{\} \;
done

find . -type l -exec rm \{\} \;

echo "- libtoolize"
libtoolize

echo "- aclocal."
aclocal \$ACLOCAL_PATH 
echo "- autoconf."
autoconf
echo "- autoheader."
autoheader
echo "- automake."
automake -a
exit

EOF
 chmod u+x reconf
}


#(1.F.2) ???
function make_local_cpp
{
 gcng -cS ${PROJECT} acconfig.h ${RELEASE} > acconfig.h
 cat >> acconfig.h <<EOF

/* These are required by Automake */
#undef PROJECT
#undef VERSION

EOF
}

# --------------------------------------------------------------------
# These functions make the traditional configure.in and Makefile.am
# files. This is for people who want to follow the GNU coding
# standards a lot more closer than I do. It assumes that you are
# using C only and does not use any LF macros
# --------------------------------------------------------------------

# (2.A) toplevel configure for c-program
function make_toplevel_configure_c
{
OUTL="Makefile"
for i in $WORK_SDL
do
    OUTL="$OUTL ${i}/Makefile"
done

 cat >> configure.in <<EOF
dnl process this file with autoconf to produce a configure script
dnl (1) -------- general startmacros
AC_INIT(reconf)
AM_INIT_AUTOMAKE(${project},${RELEASE})
AM_CONFIG_HEADER(config.h)
AC_PREFIX_DEFAULT(/usr/local/)
AC_PROG_MAKE_SET

dnl (2) -------- set of available languages

dnl (3) -------- checks for programs

AC_PROG_CC
AC_PROG_CPP
AC_AIX
AC_ISC_POSIX
AC_MINIX
AC_HEADER_STDC

dnl (4) -------- checks for libraries
AC_PROG_LIBTOOL

dnl (7) -------- les autres petis choses
AC_PATH_PROGS(BASH, bash sh)

dnl (8) -------- determine whole directory-structure
AC_OUTPUT(${OUTL})
EOF
}

# (2.B) toplevel make-file for c-program
function make_toplevel_makefile_c
{
    echo "updating Makefile.am at toplevel (mode c)"
    cat >> Makefile.am <<EOF
EXTRA_DIST = reconf configure ${ADD_TOPLEVEL_DIST}
SUBDIRS = m4 scripts doc lib src 
EOF
}

# (2.C) sub-make-file for doc + m4
function make_blank_makefile_c
{
    echo "creating blank-Makefile.am (mode c)"

    cat >> Makefile.am <<EOF
#EXTRA_DIST
#SUBDIRS
EOF
}

# (2.D.1) src-level-makefile for main + sister-module
function make_simolevel_makefile_c
{
    echo "updating Makefile.am for directory src (mode c)"
    
    cat >> Makefile.am <<EOF

CFLAGS = -DLinux -Wall -ansi -pedantic
#CFLAGS = -DLinux -Wall -ansi -pedantic -DXOPEN_SOURCE=500
#CFLAGS = -DLinux -Wall -ansi -pedantic -g
#CFLAGS = -DLinux -Wall -ansi -pedantic -O3
#CFLAGS = -DLinux -Wall -ansi -pedantic -g -pg

SUBDIRS=daughter-module
INCLUDES = -I\$(top_srcdir)/lib -I\$(top_srcdir)/src/daughter-module

bin_PROGRAMS = ${project}
${project}_SOURCES = \\
    ${project}.c ${simoname}.c ${simoname}.h
${project}_DEPENDENCIES = \\
    ../lib/lib${librname}.a daughter-module/${damoname}.o
${project}_LDADD = daughter-module/${damoname}.o -l${librname} 
${project}_LDFLAGS = -L\$(top_builddir)/lib -L\$(top_builddir)/src/daughter-module

EOF

}
# (2.D.2) src-level-main-source-file for c
function make_simolevel_main_c
{
    echo "creating main-program in directory src (mode c)"
  
    cat >> ${project}.c <<EOF
`gcng -c ${PROJECT} ${project}.c ${RELEASE}`

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include "${simoname}.h"
#include <${damoname}.h>
#include <${librname}.h>

int main (int argn, char * argv[])
{
    int sister_module_object_1,
        sister_module_object_2,
        daughter_module_object_1,
        daughter_module_object_2,
        library_module_object_1,
        library_module_object_2;
    
    sister_module_object_1=init_${simoname}_object(); 
    sister_module_object_2=init_${simoname}_object(); 

    daughter_module_object_1=init_${damoname}_object(); 
    daughter_module_object_2=init_${damoname}_object(); 

    library_module_object_1=init_${librname}_object(); 
    library_module_object_2=init_${librname}_object(); 
           
    printf("hello from main of package ${PROJECT}\n");
    
    delete_${simoname}_object(sister_module_object_1);
    delete_${simoname}_object(sister_module_object_2);

    delete_${damoname}_object(daughter_module_object_1);
    delete_${damoname}_object(daughter_module_object_2);

    delete_${librname}_object(library_module_object_1);
    delete_${librname}_object(library_module_object_2);

    return 0;
} 
EOF
}

# (2.D.3) src-level-sister-module-file for c++
function make_simolevel_module_c
{
    echo "creating sister-module (mode cpp)"
    
# (2.D.3.1) fill header-file
    
    cat >> ${simoname}.h << EOF
`gscg -c -i ${Simoname} ${PROJECT} ${RELEASE}`

EOF


# (2.D.3.2) fill source-file
    cat > ${simoname}.c << EOF
`gscg -c -s ${Simoname} ${PROJECT} ${RELEASE}`
EOF
}



# (2.D.4) dmod-level-makefile for daughter-module
function make_damolevel_makefile_c
{
    echo "updating Makefile.am for daughtermodule (mode c)"

    cat >> Makefile.am <<EOF

CFLAGS = -DLinux -Wall -ansi -pedantic
#CFLAGS = -DLinux -Wall -ansi -pedantic -DXOPEN_SOURCE=500
#CFLAGS = -DLinux -Wall -ansi -pedantic -g
#CFLAGS = -DLinux -Wall -ansi -pedantic -O3
#CFLAGS = -DLinux -Wall -ansi -pedantic -g -pg
    
noinst_LIBRARIES =lib${damoname}.a
lib${damoname}_a_SOURCES = \\
    ${damoname}.c ${damoname}.h

EOF

}

# (2.D.5) src-level-daughter-module-file for c++
function make_damolevel_module_c
{
    echo "creating daughter-module (mode cpp)"
    
# (2.D.5.1) fill header-file
    
    cat >> ${damoname}.h << EOF
`gscg -c -i ${Damoname} ${PROJECT} ${RELEASE}`

EOF

# (2.D.5.2) fill source-file
    cat > ${damoname}.c << EOF
`gscg -c -s ${Damoname} ${PROJECT} ${RELEASE}`
EOF

}

# (2.E.1) lib-level-makefile for libraries
function make_liblevel_makefile_c
{
   echo "updating lib-Makefile.am for directory lib (mode c)"

    cat >> Makefile.am << EOF
#EXTRA_DIST 
#SUBDIRS

CFLAGS = -DLinux -Wall -ansi -pedantic
#CFLAGS = -DLinux -Wall -ansi -pedantic -DXOPEN_SOURCE=500
#CFLAGS = -DLinux -Wall -ansi -pedantic -g
#CFLAGS = -DLinux -Wall -ansi -pedantic -O3
#CFLAGS = -DLinux -Wall -ansi -pedantic -g -pg

include_HEADERS = ${librname}.h

lib_LIBRARIES = lib${librname}.a
lib${librname}_a_SOURCES = ${librname}.c ${librname}.h

lib_LTLIBRARIES = lib${librname}.la
lib${librname}_la_SOURCES = ${librname}.c ${librname}.h

lib${librname}_la_LDFLAGS = -version-info ${LIBRELEASE}

EOF

}

#(2.E.2.a) statical linkable library c++
function make_liblevel_statlib_c
{
    echo "creating static-library (mode c)"
    
# (2.E.2.a.I) fill header-file
    
    cat > ${librname}.h << EOF
`gscg -c -i ${Librname}  ${PROJECT} ${RELEASE}`
EOF

# (2.E.2.a.II) fill source-file
    cat > ${librname}.c << EOF
`gscg -c -s ${Librname}  ${PROJECT} ${RELEASE}`
EOF

}

#(2.F.1) write reconfiguration-file for c
function make_reconf_c
{
 cat >> reconf <<EOF
#!/bin/sh
ACLOCAL_PATH=\
" -I /usr/local/gnu/share/aclocal \
  -I /usr/local/share/aclocal \
  -I /opt/gnome/share/aclocal \
  -I/usr/share/aclocal"

DELCONF_FILES=\`ls config.*\`
DELETE_FILES=\
" config.cache \
   acconfig.h \
   Makefile \
   Makefile.in \
   aclocal.m4 \
   configure \
   \${DELCONF_FILES}"
for DF in \${DELETE_FILES}; 
do
  echo "deleting \${DF}"
  find . -name \${DF} -exec rm \{\} \;
done

find . -type l -exec rm \{\} \;

echo "- aclocal."
aclocal  

echo "- libtoolize"
libtoolize

echo "- autoconf."
autoconf

echo "- autoheader."
autoheader

echo "- automake."
automake -a

exit
EOF
 chmod u+x reconf
}

#(2.F.2) ???
function make_local_c
{
 gcng -cS ${PROJECT}  acconfig.h ${RELEASE} > acconfig.h
 cat >> acconfig.h <<EOF

/* These are required by Automake */
#undef PROJECT
#undef VERSION
EOF
}


function integrate_doxygen
{
  local DOUTPDIR="doc\/doxygen"
  local DTMPFILE=$project.tmp

  
  local SP=sed.proj
  local SR=sed.rele
  local SO=sed.outp
  local SI=sed.inpu
  local SF=sed.fipa
  local SD=sed.recu
   
  echo "writing default doxgen-config-file named $DOXYFILE"
  doxygen -g $DOXYFILE &>/dev/null
  
  echo "integrating project-specific file- and name-structure into file $DOXYFILE"
  echo "/^PROJECT_NAME / s/^.*/PROJECT_NAME = ${PROJECT}/" > $SP
  echo "/^PROJECT_NUMBER / s/^.*/PROJECT_NUMBER = ${RELEASE}/" > $SR
  echo "/^OUTPUT_DIRECTORY / s/^.*/OUTPUT_DIRECTORY = ${DOUTPDIR}/" > $SO
  echo "/^INPUT / s/^.*/INPUT = src lib /" > $SI
  echo "/^FILE_PATTERNS / s/^.*/FILE_PATTERNS = *.cc *.h *c /" > $SF  
  echo "/^RECURSIVE / s/^.*/RECURSIVE = YES /" > $SD 
  
  sed -f $SP -f $SR -f $SO -f $SI -f $SF -f $SD $DOXYFILE > $DTMPFILE
  rm $SP
  rm $SR
  rm $SO
  rm $SI
  rm $SF
  rm $SD
  mv $DTMPFILE $DOXYFILE
 
  echo calling doxgen
  doxygen $DOXYFILE
  
}

function write_prepcvsimport
{
    echo "creating prepare-cvs-import-file"
    
     cat >> $PREPARECVSIMPORTFILE <<EOF
#!/bin/sh

if [ ! -f Makefile ];
then
  ./reconf
  ./configure
fi
make distclean

DELCONF_FILES=\`ls config.*\`
DELETE_FILES=\
" config.cache \
   acconfig.h \
   Makefile \
   Makefile.in \
   aclocal.m4 \
   configure \
   \${DELCONF_FILES}"
for DF in \${DELETE_FILES}; 
do
  echo "deleting \${DF}"
  find . -name \${DF} -exec rm \{\} \;
done

find . -type l -exec rm \{\} \;

find . -type d | while read DIR; do

if [ "\$DIR" = "./autom4te.cache" ];
then
  echo "deleting \$DIR"
  rm -r \$DIR
  continue
fi

if [ "\$DIR" = "." ];
then
  continue
fi
(
  cd \$DIR
  
  echo "# Scripts which shall be ignored by cvs commit" > .cvsignore
  echo "# because they are unimportant, only" >> .cvsignore
  echo "# temporarily used or will automatically" >> .cvsignore
  echo "# be rewritten by autoconf/automake" >> .cvsignore
  echo "Makefile.in" >> .cvsignore
  echo "Makefile" >> .cvsignore
  echo "*.la" >> .cvsignore
  echo "*.lo" >> .cvsignore
  echo "*.o" >> .cvsignore
  echo ".libs" >> .cvsignore
  echo ".deps" >> .cvsignore 
)

done

echo "# Links which shall be ignored by cvs commit." > .cvsignore
echo "# They can be regenerated by automake -a" >> .cvsignore 
echo "# after having checked out the whole project" >> .cvsignore 
echo ""  >> .cvsignore
echo "COPYING" >> .cvsignore 
echo "INSTALL" >> .cvsignore 
echo "config.guess" >> .cvsignore 
echo "config.sub" >> .cvsignore 
echo "install-sh" >> .cvsignore 
echo "ltmain.sh" >> .cvsignore 
echo "ltconfig" >> .cvsignore 
echo "missing" >> .cvsignore 
echo "mkinstalldirs" >> .cvsignore 
echo ""  >> .cvsignore
echo "# Scripts which shall be ignored by cvs commit. " >> .cvsignore 
echo "# They will be regenerated each time when" >> .cvsignore 
echo "# automake and autoconf will be called" >> .cvsignore 
echo ""  >> .cvsignore
echo "aclocal.m4" >> .cvsignore 
echo "configure" >> .cvsignore 
echo "Makefile.in" >> .cvsignore 
echo "Makefile" >> .cvsignore 
echo "stamp-h" >> .cvsignore 
echo "config.h" >> .cvsignore 
echo "config.log" >> .cvsignore 
echo "config.status" >> .cvsignore 
echo "libtool" >> .cvsignore 

EOF
 chmod u+x  $PREPARECVSIMPORTFILE 
}

function write_chrefile
{

  cat > $CHREFILE <<EOF
#!/bin/bash

#
# Copyright (C) 2000 karsten reincke <karsten.reincke@karubik.de>
#  
# 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 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#  
# file <change-release> version <0.10> of project <GTGT>


# change the following 6 variables with respect to this pattern
# version-number is RELEASE.REVISION.STEP

OLDRELEASE=${RELE}    	# defines a release and a library interface
OLDREVISION=${BRAN}   	# defines a more important revision of a release
OLDSTEP=${STEP}       	# defines a lowlevel revesion of a release
      	      	      	# (doesn't concern libraries
OLDLIBAGE=0   	      	# indicates how many of the elder
      	      	      	# library-releases offer the same interface 

NEWRELEASE=${RELE}
NEWREVISION=${INCRBRAN}
NEWSTEP=${STEP}
NEWLIBAGE=0   	      	

#---------------------------------------------------------------------------------
# change the following 6 variables only if you know the revision system of libtool
OLDLIBTOOLCURRENT="\${OLDRELEASE}"
OLDLIBTOOLREVISION="\${OLDREVISION}"
OLDLIBTOOLAGE="\${OLDLIBAGE}"

NEWLIBTOOLCURRENT="\${NEWRELEASE}"
NEWLIBTOOLREVISION="\${NEWREVISION}"
NEWLIBTOOLAGE="\${NEWLIBAGE}"

#################################################################################
# don't change the following lines !

OLDVERSION="\${OLDRELEASE}\.\${OLDREVISION}\.\${OLDSTEP}"
OLDLIBVERSION="\${OLDLIBTOOLCURRENT}\:\${OLDLIBTOOLREVISION}\:\${OLDLIBTOOLAGE}"

NEWVERSION="\${NEWRELEASE}\.\${NEWREVISION}\.\${NEWSTEP}"
NEWLIBVERSION="\${NEWLIBTOOLCURRENT}\:\${NEWLIBTOOLREVISION}\:\${NEWLIBTOOLAGE}"

if [ \$# = 0 -o "\$1" = "-h" -o "\$1" = "--help" ]
then
  echo "script for changing recursively all revision- and branchnumbers"
  echo "usage:  ${CHREFILE} [ -h | --help | * | FILENAME ... ]"
  echo "for inserting old and new numbers please edit ${CHREFILE}"
  exit
fi

while [ ! -z \$1 ];
do
  if [ -d \$1 -o -L \$1 ]
  then
    if [ -d \$1 ]
    then
      cp ${CHREFILE} \$1/${CHREFILE}
      cd \$1
      ./${CHREFILE} *
      rm ${CHREFILE}
      cd ..
    else
      echo "ignoring link \$1"
    fi
  else
    if [ -f \$1 -a ! -L \$1 ]
    then
      INFILE=\${1}.xyz
      OUTFILE=\${1}
      echo "setting \$OLDVERSION onto \$NEWVERSION in \$OUTFILE"
      cp \$OUTFILE \$INFILE
      sed -e "/<#\$OLDVERSION#>/s/\$OLDVERSION/\$NEWVERSION/" \$INFILE > \$OUTFILE
      rm \$INFILE
    else
      echo "ignoring \$1"
    fi
  fi
  shift
done

if [ -f configure.in ]
then
  echo "setting \$OLDVERSION onto \$NEWVERSION in configure.in"
  mv configure.in configure.org
  sed -e "/(${project},\$OLDVERSION)/s/\$OLDVERSION/\$NEWVERSION/" configure.org > configure.in
  rm configure.org
fi

if [ -f Makefile.am ]
then
  echo "setting \$OLDVERSION onto \$NEWVERSION in libsection of ..."
  echo "... \$PWD\Makefile.am"
  mv Makefile.am Makefile.org
  sed -e "/info \$OLDLIBVERSION/s/\$OLDLIBVERSION/\$NEWLIBVERSION/" Makefile.org > Makefile.am  
  rm Makefile.org
fi

if [ -f ${DOXYFILE} ]
then
  echo "setting \$OLDVERSION onto \$NEWVERSION in ${DOXYFILE}"
  mv ${DOXYFILE} ${DOXYFILE}.org
  sed -e "/PROJECT_NUMBER = \$OLDVERSION/s/\$OLDVERSION/\$NEWVERSION/" ${DOXYFILE}.org > ${DOXYFILE} 
  rm ${DOXYFILE}.org
fi

EOF
chmod 775 $CHREFILE
}
# ============================================================
# == These are the highlevel functions for creating various ==
# == types of directories                                   ==
# ============================================================

# -------------------------------------------------------
# This function creates the toplevel package directory.
# FIXME: Add more documentation here
# -------------------------------------------------------

function create_toplevel_directory 
{
    echo "Ready to create a new packet skeleton in"
    echo "`pwd`"
    echo "using the copyright"
    get_copyright_data
 
    echo "package-name                    : $PROJECT"
    echo "initialization-author           : $AUT_NAME"
    echo "initialization-email            : $AUT_EMAIL"
    if [ $COMPANY != "GNU" ];
    then
        echo "software owning company         : $COMPANY"
        echo "using the short license-version : $COMP_SHORT_COPYRIGHT_NOTE"
        echo "using the long license-version  : $COMP_LONG_COPYRIGHT_NOTE"
    fi
     
    # Make the directories
    echo "project-programming language    : ${LANGUAGE}"
    echo "project-directory start-release : ${PROJECT}-${RELEASE}"
    
    mkdir ${project}-${RELEASE}
 
    cp gcng.conf ${project}-${RELEASE}/
 
    cd ${project}-${RELEASE}
 
    SDL="src src/daughter-module lib scripts doc m4"
    WORK_SDL="doc m4 scripts lib src src/daughter-module"
    
    for i in $SDL
    do
        echo "creating blank ${i}/Makefile.am"    
        mkdir $i
        cp gcng.conf ${i}/gcng.conf
        gcng -am ${PROJECT} ${i}/Makefile.am ${RELEASE} > ${i}/Makefile.am
    done
    
    
    echo "creating blank Makefile.am, configure.in and reconf"
    gcng -am ${PROJECT} Makefile.am ${RELEASE} > Makefile.am
    gcng -shS ${PROJECT} configure.in ${RELEASE} > configure.in
    gcng -shS ${PROJECT} reconf ${RELEASE} > reconf 
    chmod u+x reconf
 

    # Create templates for the standard information files
    echo "creating filled NEWS README AUTHORS THANKS ChangeLog"
    touch NEWS README AUTHORS THANKS ChangeLog
    make_NEWS
    make_README
    make_AUTHORS
    make_THANKS
    make_ChangeLog
    
    
    for i in $SDL; 
    do
        cd $i
        echo "creating source-templates and filling makefiles in ${i}"    
        case $i in
        "m4")
            make_m4_makefile
            ;;    
        "doc")
            make_doclevel_makefile
            make_htmldoc
            ;;
        "scripts")
            make_scriptlevel_makefile
            make_bashscript
            ;;
        "lib")
            make_liblevel_makefile_${LANGUAGE}
            make_liblevel_statlib_${LANGUAGE}
            ;;
        "src/daughter-module")
            make_damolevel_makefile_${LANGUAGE}
            make_damolevel_module_${LANGUAGE}
            cd ..
            ;;
        "src")
            make_simolevel_makefile_${LANGUAGE}
            make_simolevel_main_${LANGUAGE}
            make_simolevel_module_${LANGUAGE}
            ;;
        *)
            make_blank_makefile_${LANGUAGE} $i
            ;;
        esac
        cd ..   
    done
   
    make_toplevel_configure_${LANGUAGE}
    make_toplevel_makefile_${LANGUAGE}
    make_reconf_${LANGUAGE}
    make_local_${LANGUAGE}

    # Run the reconf script
    echo "+ Running reconf"
    sh ./reconf 

    echo "+ integrate doxygen"
    integrate_doxygen
    
    echo "+ integrating the correct license"
    if [ "$COMPANY" = "GNU" ]
    then
      echo "project will be published under GNU-license"
    else
      echo "project will be published under special company-license"
      rm COPYING
      cp ${COMP_LONG_COPYRIGHT_NOTE} COPYING
    fi
    
    echo "+ writing a change-release-script named $CHREFILE"
    write_chrefile
    
     echo "+ writing a prepare cvsimport-file named $CHREFILE"
    write_prepcvsimport
    # Important message
    echo "Distribution directory is ready."
    echo "keep the files AUTHORS, NEWS, README, THANKS up to date"
    echo "namely before cutting a distribution."

}

############################################################################
############################################################################

# 
# Parse the command-line arguments
#

# Force some command-line arguments to be passed

LANGUAGE="cpp"
PROJECT="DePpO"
RELE="0"
BRAN="1"

if [ $# -eq 0 ];
then
       usage
       exit
fi

case $1 in
 --help)
    usage
    exit
    ;;
 -h)
    usage
    exit
    ;;
 --version)
    version
    exit
    ;;
 -v)
    version
    exit
    ;;
 -cpp)
    LANGUAGE="cpp"
    ;;
 -c)
    LANGUAGE="c"
    ;;
 *)
    usage
    exit
    ;;        
esac


if [ $# -gt 1 ];
then
    PROJECT=$2
    if [ $# -gt 2 ];
    then
        RELE=$3
        if [ $# -gt 3 ];
        then
            BRAN=$4
        fi
    fi
fi

STEP=0

INCRBRAN=$[ $BRAN + 1 ]

case $LANGUAGE in
  c)
    CNSIMO=${PROJECT}_sister_module
    CNDAMO=${PROJECT}_daughter_module
    CNLIMO=${PROJECT}_library_module
    ;;
  cpp)
    CNSIMO=${PROJECT}SisterModule
    CNDAMO=${PROJECT}DaughterModule
    CNLIMO=${PROJECT}LibraryModule
   ;;
  *)
    echo "unknown languge"
    exit
esac

RELEASE="${RELE}.${BRAN}.${STEP}"
LIBRELEASE="${RELE}:${BRAN}:${STEP}"

# defining the module/library names
# (1) sister - module = same level as main-source
# beeing compiled into $(top_srcdir)/src
    
SIMONAME=`toUpper ${CNSIMO}`
Simoname=`toCapitalized ${CNSIMO}`
simoname=`toLower ${CNSIMO}`

# (2) daugther - module = sub-level of src 
# = really being an uninstalled, but linked static library
# being compiled into $(top_srcdir)/src/daughter-module
DAMONAME=`toUpper ${CNDAMO}`
Damoname=`toCapitalized ${CNDAMO}`
damoname=`toLower ${CNDAMO}`

# (3) library = linkable and installable static + shared
# library being compiled into $(top_srcdir)/lib
LIBRNAME=`toUpper ${CNLIMO}`
Librname=`toCapitalized ${CNLIMO}`
librname=`toLower ${CNLIMO}`

# (4) the rest

Project=`toCapitalized ${PROJECT}`
project=`toLower ${PROJECT}`
PROJECT=`toUpper $PROJECT`

DOXYFILE=Doxyfile
CHREFILE=change-release
PREPARECVSIMPORTFILE="prepare-cvs-import"
ADD_TOPLEVEL_DIST="$DOXYFILE $CHREFILE $PREPARECVSIMPORTFILE"

create_toplevel_directory

exit
