#!/usr/local/bin/perl
# Geoffrey Furnish
# Institute for Fusion Studies
# University of Texas at Austin
# 6 July 1995
#
# This script is used to automatically generate the [incr Tcl] methods
# needed in the PLWin [incr Tcl] class.  These methods mirror most of
# the Tcl API commands which are generated by the companion script,
# pltclgen.
#
# However, PLWin is a widget wrapper, and as such, not all of the Tcl
# API has any business being in the widget.  So we remove various
# shadow methods for portions of the API which are not relevant to
# widgets, such as the stream manipulation functions, etc.
#
# Also, note that this script is not quite as automatic as pltclgen.
# The output must be inserted by hand into PLWin.tcl.  Hopefully this
# will not seem like an unreasonable limitation, since the Tcl API
# only changes occasionally by this point.
###############################################################################

require "getopts.pl";

&Getopts('v');

$verbose = $opt_v;

$specfile = "plapi.tpl";	# PLplot API template specification file.
$genfile  = "gen.itcl";		# Generated methods go here.

open( SPECFILE, "<$specfile") || die "Can't open PLplot API spec file.";
open( GENFILE,  ">$genfile" ) || die "Can't open output file.";

# Set up the list of excludable functions.  Have to think a bit more
# carefully about what should really go in here.

@ignore = (
	   "plend",
	   "plend1",
	   "plfamadv",
	   "plgfam",
	   "plsfam",
	   "plsfnam",
	   "plsstrm",
	   );

# Scan the PLplot API template specification file looking for function
# "prototypes".  These are introduced with the token "pltclcmd".  When
# we find one, go process it.  Anything other than a comment or a
# valid function "prototype" is considered an error, and is printed to
# stdout.

while( <SPECFILE> ) {
    chop;			# skip the \n.

    if (/([^\#]*)\#.*/) {	# Discard # to end of line.
        $_ = $1;
    }
    next if /^\s*$/;            # Discard empty lines.

    if (/^pltclcmd (\w+) (.*)/) {
	$cmd = $1;
	$rtype = $2;

	$exclude = 0;
	foreach(@ignore) {
	    $exclude = 1 if /$cmd/;
	}

	if (!$exclude) {
	    print "Generating itcl method for $cmd.\n";

	    print GENFILE "    method $cmd {args} {\n";
	    print GENFILE "        eval \$plwin cmd $cmd \$args\n";
	    print GENFILE "    }\n";
	    print GENFILE "\n";
	}

	next;
    }
}
