
# this shell script adds an externally defined widget to the
# src/tmExtern.c file. It uses a widget description from the
# directory extern_widgets.

if [ $# -ne 1 ]
then
    echo "Usage: $0 widget"
    exit 1
fi

WFILE=extern_widgets/$1
if [ ! -r $WFILE ]
then
    echo "Can\'t open widget description file \"$WFILE\""
    exit 2
fi

# the tcl command used to acces the widget is stored in format
#	command   <tcl-command>
# for all of these, allow arbitrary white space before the keyword

tcl_command=`grep '^[ 	]*command' $WFILE | awk '{print $2}'`
if [ -z "$tcl_command" ]
then
    echo "Can\'t find the tcl command for this widget"
    exit 3
fi

# the Xt class for the widget is stored in format
#	class <Xt-class>

xt_class=`grep '^[   ]*class' $WFILE | awk '{print $2}'`
if [ -z "$xt_class" ]
then
    echo "Can\'t find the Xt class for this widget"
    exit 4
fi

# the C function to create this widget is stored as
#	create-function  <C function>
create_func=`grep '^[   ]*create-function' $WFILE | awk '{print $2}'`
if [ -z "$create_func" ]
then
    echo "Can\'t find the C function name to create this widget"
    exit 5
fi


# the C function to handle methods for this widget is stored as
#	method-function  <C function>
method_func=`grep '^[   ]*method-function' $WFILE | awk '{print $2}'`
if [ -z "$method_func" ]
then
    echo "Can\'t find the C function name to handle methods for this widget"
    exit 6
fi

# if there are include files required, they are stored as
#	include-file  <file>
# multiple includes are allowed, one per line

includes=`grep '^[   ]*include-file' $WFILE | awk '{print $2}'`

# if there are any reasons, they are stored as
#	reason <reason> <reason-string"
# multiple reasons are allowed, one per line

# To allow white space of {tab | space}, change all tabs to spaces first :-(`
reasons=`tr "\t" " " < $WFILE | grep '^[   ]*reason' | awk '{print $2 " " $3}'`

# the directory include directive for the Imakefile is stored as
#	include-dir <dir>

include_dir=`grep '^[   ]*include-dir' $WFILE | awk '{print $2}'`

# the library directive for the Imakefile is stored as
#	library <lib>

library=`grep '^[   ]*library' $WFILE | awk '{print $2}'`


# debug: echo $tcl_command $xt_class $create_func $method_func $includes $reasons

#-----------------------------------------------------------------------

#
# Now we are going to embark on a series of edit jobs to patch up
# the various arrays in tmExtern.c. I use awk here because it finds
# the relevant lines easily for the editing. I could use one big awk
# statement, but right now I feel happier doing it one step at a time
#

# make a copy first, in case it stuffs up
cp src/tmExtern.c src/tmExtern.c.orig

# Patch up the Tm_ExternCommands array by adding in an entry of the form
#	{tcl_command, create_func, method_func},
awk '
        { print $0 }

    /Tm_Cmd Tm_ExternCommands/ \
	{
	  print "    {\"" TCL_COMMAND "\",	" CREATE ",	" METHOD "},"
	}
' TCL_COMMAND=$tcl_command CREATE=$create_func METHOD=$method_func \
  src/tmExtern.c > tmp1
		
# Patch up the Tm_ExternCommandToClass array by adding in an entry of the form
#	{tcl_command, xt_class},
awk '
        { print $0 }

    /Tm_CommandToClassType Tm_ExternCommandToClass/ \
	{
	  print "    {\"" TCL_COMMAND "\",	&" CLASS  "},"
	}
' TCL_COMMAND=$tcl_command CLASS=$xt_class tmp1 > tmp2
		

# Insert the include file declarations after <Xm/Xm.h>
awk '
	{ print $0 }
    /Xm\/Xm.h/ \
	{
	  n = split(INCLUDES, includes, "\n")
	  for (i in includes)
		print "#include " includes[i]
	}
' INCLUDES="$includes" tmp2 > tmp3

# Patch up the  Tm_ExternReasons array by adding in an entry of the form
#       {reason, reason-string},
awk '
	{ print $0 }
    /Tm_ReasonType Tm_ExternReasons/ \
	{
	  n = split(REASONS, reasons, "\n")
	  for (i in reasons) {
		split(reasons[i], line)
		print "	{" line[1] ",	\"" line[2] "\"},"
	  }
	}
' REASONS="$reasons" tmp3 > tmp4

#-----------------------------------------------------------------------

# now we get to add in the extra C code that belongs to/defines the
# various widget stuff

# If there is initialisation stuff, insert it now
if [ -r ${WFILE}.initialise ]
then
    awk '
            { print $0 }
        /Place your own external widgets initialisation code here/ \
   	    {
	       system("cat " INIT_FILE)
	    }
    ' INIT_FILE=${WFILE}.initialise tmp4 > tmp5
fi

# If there is expand percents stuff, insert it now
if [ -r ${WFILE}.expand ]
then
    awk '
            { print $0 }
        /Place your own external widgets expand percents code here/ \
   	    {
	       system("cat " EXPAND_FILE)
	    }
    ' EXPAND_FILE=${WFILE}.expand tmp5 > tmp6
else
    mv tmp5 tmp6
fi

# If there is any additional code stuff to be added for this widget
# do it now
if [ -r ${WFILE}.commands ]
then
    cat tmp6 ${WFILE}.commands > tmp7
else
    mv tmp6 tmp7
fi

#
# We are done with tmExtern.c - move the file back over what we started from,
# cross our fingers (well, yours at least - mine are bent enough)
# and move onwards (and upwards, and sideways, and....)
mv tmp7 src/tmExtern.c

#----------------------------------------------------------------------#--------------------------------------------------------------------------
# Now to fix up the Imakefile

cp src/Imakefile src/Imakefile.orig
awk '
     $1 !~ /EXTRA_WIDGETS_INCLUDE/ && $1 !~ /EXTRA_WIDGETS_LIB/ \
        { print $0 }
    /EXTRA_WIDGETS_INCLUDE/ \
	{
	  print "EXTRA_WIDGETS_INCLUDE = " INCLUDE_DIR
	}
    /EXTRA_WIDGETS_LIB/ \
	{
	  print "EXTRA_WIDGETS_LIB = " LIBRARY
	}
' INCLUDE_DIR=$include_dir LIBRARY=$library src/Imakefile > tmp
mv tmp src/Imakefile

rm -f tmp*

exit 0
