#!/usr/bin/perl
# SCCS $Id: callgr-a,v 1.11 2003/06/13 13:29:13 dcs0mpw Exp $
#############################################################################
## FermaT Transformation System
## Copyright (C) 2001 Software Migrations Limited.
## Email: Martin.Ward@durham.ac.uk
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#############################################################################
#

BEGIN {
  $FermaT = $ENV{'FermaT'} || "/usr/local/fermat2";
  $FermaT =~ s/"//g;
  $ds = "/"; $ds = "\\" if ($^O eq "MSWin32");
  unshift(@INC, "$FermaT${ds}config");
}

use fermat;

($myname = $0) =~ s|(.*/)*||;   # strip path component from name
$Usage = "Usage: $myname [-my] file.wsl [out.wsl] [TR_name] [Data] [size]\n";

# Check one to five arguments:
die $Usage if (($#ARGV < 0) || ($#ARGV > 4));

&parse_args();
$options = &find_file("options");
$options = "$FermaT/sml/lib/options" if (!$options && -f "$FermaT/sml/lib/options");
($output = $input) =~ s/\.\w+$//;
$output .= ".cga";

$cmds = <<END_OF_INPUT;
  (\@New_Program (\@Parse_File "$input" //T_/Statements))
  (if (file-exists? "patch.scm")
      (load "patch.scm")
      (if (file-exists? "patch.tr")
	  (load "patch.tr")))
  (define //Options_/File "$options")
  (\@Find_Type //T_/A_/S)
  (\@Call_Graph (\@I) "$output")
END_OF_INPUT

&fermat($cmds) unless (newer($output, $input));

$vcg = $output;
if ($vcg =~ s/\.cg/\.vg/) {
  # $vcg is OK
} else {
  $vcg .= ".vcg";
}

mysystem qq[cgtovcg "$output" "$vcg"] unless newer($vcg, $output);
system qq[xvcg -timelimit 20 "$vcg"] if (-t);
exit(0);

