#! /usr/bin/perl
#

#$Id: idef2tif.src,v 1.6 2002/10/28 14:21:29 jurgenv Exp $

# idef2tif takes an interface definition file (idef) and returns a 
# toolbus interface file (tifs). 
#
# An idef file is a Tscript with exactly one process, and no toolbus(..) 
# statement. The idea is that such a file exactly defines the interface
# to a tool, and nothing more. In order to use it you need to #include it
# in a larger Tscript which does include a toolbus(..) statement.

# Author: Tobias Kuipers
# Date:   1-feb-1999

# Test for perl version:
if ($] < 5) {
  print "Please get perl 5 or better. idef2tif doesn't work under this version of perl\n";
  exit(2);
}

# 17-feb-2000, Merijn de Jonge
# Make sure location of toolbus binary is contained in PATH variable
$ENV{'PATH'} = "/usr/bin:$ENV{'PATH'}" ;


$idefsuf = '.idef';
$tifsuf = '.tifs';

sub usage {
  print "Usage: idef2tif inputfile$idefsuf [outputfile$tifsuf]\n";
  exit 0;
}

if ($#ARGV < 0) {
  &usage;
}

# Get filename form commandline
$inputfile = $ARGV[0];
# Get basename
@dirarray = split(/\//,$inputfile);
$basename = pop(@dirarray);
$dir = join("/",@dirarray);
if ($dir ne "") {
  $dir .= "/";
}

# Where to put temporary files?
$tmpdir=$ENV{'TMPDIR'}?$ENV{'TMPDIR'}:'/tmp';
# Check for right suffix
if ($basename =~ /(.+)\Q$idefsuf\E$/) {
  $base = $1;
} else {
  print STDERR "$basename has unknown suffix\n";
  &usage;
}

if ($#ARGV < 1) {
  $outputfile = "$base$tifsuf";
}
else {
  $outputfile = "$ARGV[1]";
}

$stmp = "$tmpdir/$base.tmp.$$";
$serr = "$tmpdir/$base.err.$$";

open(IDEF,"$inputfile") || die("can't open $inputfile");
open(TB,">$stmp") || die("can't create $stmp ($!)");

$pc = 0;
while(<IDEF>) {
  if (/process (\w+) is/) {
    $process = $1;
    $pc++;
  }
  if (/\s*toolbus\(/) {
    print STDERR "toolbus(..) statements not allowed in idef file\n";
    print STDERR "Abort.\n";
    exit 3;
  }
  print TB $_;
}

close(IDEF);

if ($pc != 1) {
  print STDERR "Not exactly 1 process definition in $inputfile ($pc)\n";
  print STDERR "Please make sure your idef file has exactly 1 process definition.\n";
  print STDERR "Abort.";
  exit 2;
}

print TB "toolbus(",$process,")\n";
close(TB);

system("toolbus -gentifs $stmp 2> $serr > /dev/null");
#$err=-s $serr;
$err=$?;
if($err) {
  open(ERR,"<$serr") || die "error opening $serr ($!)";
  print STDERR while(<ERR>);
  close ERR;
} else {
  system("mv $stmp.tifs $outputfile");
  print STDOUT "Tool interfaces written to \`$outputfile\'\n";
}

unlink($serr,$stmp);

exit $err;
