#! /usr/bin/perl
$bindir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/bin";
$includedir    = "/afs/cad.njit.edu/u/a/l/alexg/BSP/include";
$libdir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/lib";
$mandir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/man";
$davinci       = "";
$davinci       =~ /\/\w+$/ && ($davincihome = $`);






















push(@INC,$includedir);
push(@INC,$bindir);
require "bsputil";
require "bspprof_theory.prl";

select(STDERR); $| = 1; select(STDOUT); # no STDERR buffering, please.
($Pgm = $0) =~ s|.*/||;
$Version        = "1.3, 25th Nov 1997";
$bug_reports_to = 'bsplib-bugs@comlab.ox.ac.uk';

$ShortUsage = "\n$Pgm usage: for basic information, try the `-help' option\n";



$arch   = &backtick("$bindir/bsparch -arch");
$arch   = &backtick("bsparch -arch")  if ($arch eq "");
$device = "";
$cgprofdir= "$bindir/$arch/";
$icondir  = "$includedir/icons/";
$Verbose  = 0;
$InputFile="";
$date     ="";
$machname ="";
$nprocs   =0;
$hide          =0.01;
$Logo          =1;
$Colour        =1;
$DeltaNormalise=2;




  arg: while ($_ = $ARGV[0]) {
  shift(@ARGV);
  #--------HELP------------------------------------------------
  /^-help$/   && do {&FormatManual("bspcgprof.1"); exit(0);};

  /^-v$/      && do {$Verbose = 1; next arg;};

  /^-hide$/   && do {$hide= &grab_next_arg("-hide");
	             if (($hide =~ /^(\d+.\d+)$/) || ($hide =~ /^(\d+)$/)) {
                       $hide = $1/100.0;
                     } else {
                       print STDERR "$Pgm: -hide requires a percentage as ",
				    "an argument\n";
		       $Status++;
		     }
	             next arg;};

  /^-nologo$/    && do {$Logo  =0; next arg;};
  /^-gr(e|a)y$/  && do {$Colour=0; next arg;};
  /^-colou?r$/   && do {$Colour=1; next arg;};
  /^-normalise$/ && do {$DeltaNormalise = &grab_next_arg("-normalise");
		        if ($DeltaNormalise =~ /^(\d+)$/) {
			  $DeltaNormalise = int($DeltaNormalise);
                        } else {
                         print STDERR "$Pgm: -normalise requires an integer ",
				      "an argument\n";
		         $Status++;
                        }
	                next arg;};

  /\.bsp$/    && do {$InputFile = $_; next arg;};
  print STDERR "Unrecognised option \"",$_,"\"\n";
  $Status++;
  }
  
  if ($InputFile eq "") {
    print STDERR "No input file given\n";
    $Status++;
  }  
  if ($Status>0) {
    print STDERR $ShortUsage;
    exit(1);
  }
  print STDERR "$Pgm: ($Version)\n" if $Verbose;
  
  if ($ENV{'DAVINCIHOME'}) {
    $davincihome = $ENV{'DAVINCIHOME'};
    $davinci     = $davincihome . "/daVinci";
  }
  &readProfileHeader();
  &setBSPmachineParams($nprocs);
  $cmd = sprintf("%s -startappl %s 'cgprof %s %d \"%s\" ".
	         "\"%s\" %.1f %.1f %.1f %.1f %d %d %d %d'",
	         $davinci,$cgprofdir,$InputFile,$nprocs,$machname,$date,
	         $bsp_s,$bsp_l,$bsp_g,$hide,$Verbose,$Logo,$Colour,
		 $DeltaNormalise);
  $cmd = "env DAVINCI_ICONDIR=$icondir TCL_LIBRARY=$davincihome/lib/tcl ".
         "TK_LIBRARY=$davincihome/lib/tk DAVINCIHOME=$davincihome " . $cmd;
  print STDERR "$Pgm: exec $cmd\n" if $Verbose;
  exec $cmd;
  exit(0);




sub readProfileHeader {
  local($found);

  if (!open(PROFILE,$InputFile)) {
    printf STDERR "$Pgm: unable to open profile file \"$InputFile\".\n";
    exit(1);
  }
  $found=0;

  while(<PROFILE>) {
   if (/^F/) {
      if (/-prof/ && /-flibrary-level\s+(\d+)/) {
        $libtype = "P$1";
      } elsif (/-flibrary-level\s+(\d+)/) {
        $libtype = "O$1";
      }
      $found++;

    } elsif (/^P\s*/) {
      $nprocs = int($');
      $found++;

    } elsif (/^D\s*/) {
      chop($date = $');
      $found++;

    } elsif (/^N\s*/) {
      chop($arch = $');
      $machname= &backtick("$bindir/bsparch -convertname $arch");
      $machname= &backtick("bsparch -convertname $arch") if ($machname eq "");
      $found++;

    } elsif (/^X\s*/) {
      chop($device = $');
    }
    last if ($found>=4);
  }
  close(PROFILE);
}

