#!/bin/sh
#
# Script to create a new module
#

OSDIRS="freebsd linux solaris winnt"

usage()
{
   echo "Usage: newmodule <modname1> [<modname2> ... <modnamen>]"
   exit 1;
}

[ $# -lt 1 ] && usage;

[ -r Makefile ] || usage;
TOPDIR=`grep 'TOPDIR[ 	]*=' Makefile 2>/dev/null`
[ -z "$TOPDIR" ] && usage;

topdir=`echo $TOPDIR | awk -F= '{print $2}'`
[ -r .subdir ] || $topdir/mak/subdir 2>&1 >/dev/null
SUBDIR=`sed 's/\/\.$//' .subdir`
[ -z "$SUBDIR" ] && usage;

for a in $*; do
   shortname=`echo $a | sed 's/^gswtk_//'`;
   if [ -d $shortname ]; then
      echo "Skipping '$a', module directory '$shortname' already exists";
   else
      mkdir $shortname $shortname/module
      mkdir $shortname/include $shortname/include/os
      mkdir $shortname/components $shortname/components/os
      for dir in $OSDIRS; do
         mkdir $shortname/include/os/$dir $shortname/components/os/$dir
      done

      #
      # Construct base makefile
      #
      cat <<EOF >$shortname/Makefile
${TOPDIR}/..

include \${TOPDIR}/mak/config.mak

include \${TOPDIR}/wr.mak/modulebase.mak

EOF

      #
      # Construct components directory makefiles
      #
      cat <<EOF >$shortname/components/Makefile
${TOPDIR}/../..

NODEFCFLAGS	= yes

include \${TOPDIR}/mak/config.mak

#SUBDIR	= os

OBJS	= init\${OBJ}

#DEFINES	+= -DGSW<module>SOURCE

include \${TOPDIR}/mak/component.mak
EOF

      cat <<EOF >$shortname/components/os/Makefile
${TOPDIR}/../../..

include \${TOPDIR}/mak/config.mak

SUBDIR	= \${OSDIRECTORY}

include \${TOPDIR}/mak/subdir.mak
EOF

      for dir in $OSDIRS; do
         cat <<EOF >$shortname/components/os/$dir/Makefile
${TOPDIR}/../../../..

NODEFCFLAGS	= yes

include \${TOPDIR}/mak/config.mak

OBJS	=

#DEFINES	+= -DGSW<module>SOURCE

include \${TOPDIR}/mak/component.mak
EOF
      done

      #
      # Construct module directory makefile
      #
      cat <<EOF >$shortname/module/Makefile
${TOPDIR}/../..

NODEFCFLAGS	= yes

include \${TOPDIR}/mak/config.mak

#CFLAGS	+= -DGSW<module>SOURCE

COMPONENTDIRS	= ${SUBDIR}/$shortname/components
#COMPONENTDIRS	+= ${SUBDIR}/$shortname/components/os/\${OSDIRECTORY}

include \${TOPDIR}/mak/module.mak
EOF

      # Construct init.c source file
      cat <<EOF >$shortname/components/init.c
/*
 * \$Id\$
 *
 *   Section: 
 * Component: Startup and Shutdown
 * 
 */


int _${shortname}_module_init    ( void );
int _${shortname}_module_shutdown( void );


/***************************************************************************\\
 * Public functions                                                        *
\\***************************************************************************/

int
_${shortname}_module_init()
{
   return 0;
}

int
_${shortname}_module_shutdown()
{
   return 0;
}
EOF

      # Construct module.def file
      cat <<EOF >$shortname/module/module.def
#
# Module definition file for $shortname module
#
# \$Id\$
#

MODULE		$a
CATEGORY	misc
IDSTRING	"<CHANGE THIS>"
VERSION		1

INIT		_${shortname}_module_init
SHUTDOWN	_${shortname}_module_shutdown
EOF

      # Construct .cvsignore files
      cat <<EOF >$shortname/.cvsignore
.subdir
EOF
      cat <<EOF >$shortname/module/.cvsignore
_gsw_module.c
_gsw_compset.c
.module.mak
EOF
      cat <<EOF >$shortname/components/.cvsignore
.subdir
.component.mak
EOF
      for dir in $OSDIRS; do
         cat <<EOF >$shortname/components/os/$dir/.cvsignore
.subdir
.component.mak
EOF
      done

      echo "Constructed '$a' module";
   fi
done

