#!/usr/local/bin/perl -w

die "Usage: gpmklabel file string vars ... \n" unless $#ARGV >= 1;

$file = shift(@ARGV);
$string = shift(@ARGV);

use vars qw($val);

open(IP, $file) || die "Couldn't open $file for reading.";
#print "opening $file\n";

# Store `var = val' lines in an assoc array, with var as key and val as value.

while(<IP>) {
    next if /^#/;		# ignore comment lines.
    if ( ($var, $val ) = $_ =~ /^([^ ]+) = (.*)$/ ) {
	$vars{$var} = $val;
    }
}

# Create the list of values according to the remaining command-line arguments.
foreach $var (@ARGV) {
    $val = $vars{$var};
    #print "debug: $var is $val\n";
    push @vals, $val;
}

@l = ($string, @vals);
printf @l;

__END__

=head1 NAME

gpmklabel - Parse gnuplot variable files to make labels and titles.

=head1 SYNOPSIS

gpmklabel datafile string [vars]

=head1 DESCRIPTION

Datafile is the file produced by the gnuplot `save var' command.
String is a printf-style format string using %f and %d to include
values of variables.  The remaining command line arguments are the
variables to include (in order) in the format string.

=head1 OPTIONS

=head1 ENVIRONMENT

=head1 EXAMPLES

    gnuplot> grad = 10.235; vel = 6.2
    gnuplot> save var "var.dat"
    gnuplot> `gpmklabel var.dat "set title \"gradient %.2f v %f\"" grad vel
    gnuplot> show title
            title is "gradient 10.23 v 6.200000", offset at 0.000000, 0.000000


If you want to read more than one datafile, you should specify the
input to come from stdin using the special filename `-'. This example
assumes r.dat contains the value of r:

    `cat var.dat r.dat | gpmklabel - "set title \"vel %.2f corr %f \" " v r`

=head1 FILES

=head1 AUTHOR

Stephen Eglen stephen@anc.ed.ac.uk
http://www.anc.ed.ac.uk/~stephen/gnuplot
=head1 SEE ALSO

=head1 DIAGNOSTICS

=head1 BUGS

=cut
