#!/usr/bin/perl
#
#

@gov = split (/\//,$0); pop @gov; $BINDIR = join ('/',@gov);

$getpar   = "$BINDIR/getpar";
$fitsfilter = "$BINDIR/fitsfilter";

# Read common PERL subs
require "$BINDIR/perlutil.pl";

# Check external packages
&check_ftools;


# Initialize internal variables ($UNDEF etc)
&ini_vars;

@PARS = &fixpars (@ARGV); # this is needed to escape arguments with parens etc
chop (
      ($inspec,$ospec) = `$getpar i,o @PARS`
);

# Fill in default values;
if ( $inspec eq $UNDEF )  {  die "please set the expression with -i\n"}
if ( $ospec  eq $UNDEF )  {  die "please set the output spectrum with -o\n"}

$firstfile = $inspec;
$firstfile =~ s/\+.*//;

system ("mathpha","expr=$inspec","units=C","outfil=!$ospec","exposure=CALC",
	"arfile=$firstfile","rmfile=$firstfile","backfile=$firstfile",
	"areascal=%","ncomments=0");
system ($fitsfilter,"$ospec\[SPECTRUM\][col !STAT_ERR; !QUALITY; POISSERR=T]",$ospec."_$$");
rename ($ospec."_$$",$ospec);

#####################################################################
sub ini_vars {
  $UNDEF = "undefined";
}
