
# 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 widget descrition is stored in format
#	widget tcl-command Xtclass create-command method-handler
# there may be many defs in WFILE, so save them in a file, one per line
# for all of these, allow arbitrary white space before the keyword

sed -n 's/^[ 	]*widget[ 	]*//p' $WFILE > tmp_widgets

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

grep '^[   ]*include-file' $WFILE | awk '{print $2}' > tmp_includes

# if there are any reasons, they are stored as
#	reason <reason> <reason-string"
# multiple reasons are allowed, one per line, so save them in a file

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

# 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

# there may be multiple widget descriptions in a file, each belonging
# to the one set of includes and libraries

# loop through each widget description
cat tmp_widgets | \
while read tcl_command xt_class create_func method_func
do

    if [ -z "$tcl_command" ]
    then echo "Can\'t find tcl_command for this widget"
        exit 3
    fi
    if [ -z "$xt_class" ]
    then echo "Can\'t find Xt class for widget $tcl_command"
        exit 4
    fi
    if [ -z "$create_func" ]
    then echo "Can\'t find creation function for widget $tcl_command"
        exit 5
    fi
    if [ -z "$method_func" ]
    then echo "Can\'t find method function for widget $tcl_command"
        exit 6
    fi

    # now edit the src file
    awk '
            { print $0 }

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

    mv tmp src/tmExtern.c
done
		

# Insert the include file declarations after <Xm/Xm.h>
cat tmp_includes | \
while read includes
do
    awk '
	    { print $0 }
        /Xm\/Xm.h/ \
	    {
	  	    print "#include " INCLUDES
	    }
    ' INCLUDES="$includes" src/tmExtern.c > tmp3
    mv tmp3 src/tmExtern.c
done

# Patch up the  Tm_ExternReasons array by adding in an entry of the form
#       {reason, reason-string},
cat tmp_reasons | \
while read reasons
do
    awk '
	    { print $0 }
        /Tm_ReasonType Tm_ExternReasons/ \
	    {
	    	split(REASONS, line)
	        print "	{" line[1] ",	\"" line[2] "\"},"
	    }
    ' REASONS="$reasons" src/tmExtern.c > tmp4
    mv tmp4 src/tmExtern.c
done

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

# 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 src/tmExtern.c > tmp5
else
    cp src/tmExtern.c 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 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 $0, INCLUDE_DIR
	}
    /EXTRA_WIDGETS_LIB/ \
	{
	  print $0, LIBRARY
	}
' INCLUDE_DIR=$include_dir LIBRARY=$library src/Imakefile > tmp
mv tmp src/Imakefile

rm -f tmp*

exit 0
