#!/bin/sh
#########################################################
## DIS defines the extension of the postscript output file
## generated with the option "set display -save=filename"
## No display will be activated (i.e. "set display -off")
## if DIS containes an empty string
## the following extensions should not be used:
## dat, daterr, gsp, sta
##########################################################

DIS="dis"
##DIS=""

if test $# -ge 1 ; then
  GASPAN=$1
  if test -r $GASPAN ; then
    echo "testing "$GASPAN
  else
    echo "  no output file from gaspan, sorry"
    exit
  fi
else
  echo "usage: gtestsuite gaspanfile [select [n], gauss, tail, all, test]"
  exit
fi

if test $# -ge 2 ; then
  OPTION=$2
else 
  OPTION="table"
fi

if test $OPTION = "select" && test $# -le 2 ; then
  OPTION = "table"
fi

##############################################################
## select the procedure you like to run if you call gtestsuite
## without any options by setting the strings to either "yes"
## or "no"
##############################################################

if test $OPTION = "table" ; then
  symmetric_peaks="yes"
  symmetric_peaks_error="yes"
  symmetric_peaks_fac="yes"
  asymmetric_peaks="yes"
  asymmetric_peaks_ch="yes"
  asymmetric_peaks_fac="yes"
  with_calibration="yes"
  q3d="yes"
  q3dc="yes"
else
# keep the following always to no
  symmetric_peaks="no"
  symmetric_peaks_error="no"
  symmetric_peaks_fac="no"
  asymmetric_peaks="no"
  asymmetric_peaks_ch="no"
  asymmetric_peaks_fac="no"
  with_calibration="no"
  q3d="no"
  q3dc="no"
fi

######################################################
## select one of the following fit options   #########
######################################################

  save_fits="no"
  save_display="yes"
  print_fit_history="yes"
  parameter_check="no"
  extended_parameter_check="no"
  timing=""
  cleanup="no"

###################################

if test $OPTION = "select"; then
  if   test $3 = "1"; then symmetric_peaks="yes"
  elif test $3 = "2"; then symmetric_peaks_error="yes"; shift
  elif test $3 = "3"; then symmetric_peaks_fac="yes"; shift
  elif test $3 = "4"; then asymmetric_peaks="yes"; shift
  elif test $3 = "5"; then asymmetric_peaks_ch="yes"; shift
  elif test $3 = "6"; then asymmetric_peaks_fac="yes"; shift
  elif test $3 = "7"; then with_calibration="yes"; shift
  elif test $3 = "8"; then q3d="yes"; shift
  elif test $3 = "9"; then q3dc="yes"
  else echo "the number mustbe between 1 and 9"
  fi
fi  

##################################
if test $OPTION = "gauss" || test $OPTION = "all" || test $OPTION = "test" ; then
  symmetric_peaks="yes"
  symmetric_peaks_error="yes"
  symmetric_peaks_fac="yes"
fi

if test $OPTION = "tail" || test $OPTION = "all" || test $OPTION = "test" ; then
  asymmetric_peaks="yes"
  asymmetric_peaks_ch="yes"
  asymmetric_peaks_fac="yes"
  with_calibration="yes"
  q3d="yes"
  q3dc="yes"
fi

if test $OPTION = "test" ; then
  save_fits="no"
  save_display="yes"
  print_fit_history="yes"
  testprint="6"
  parameter_check="no"
  extended_parameter_check="no"
  timing=
  cleanup="no"
fi

rm -f *.diff
if test $print_fit_history = "yes" ; then
  rm -f fithistory.gsp
fi

# gspcompare compares the output data
make gspcompare
  echo "testing "$GASPAN
  if test $fileispresent="yes";   then

# testspectrum wih all asymmetries
    if test $asymmetric_peaks = "yes"; then
      echo "artificially generated spectrum with all asymmetries: alltails.dat"
      echo "get opt alltails" > batch.bak
      if test $save_fits = "yes" ; then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes" ; then
        echo "set display -save=alltails.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f alltails.$DIS
      fi
      if test $print_fit_history = "yes" ; then
        echo "set fit -print" >> batch.bak
        if test $testprint ; then
          echo "test print "$testprint >> batch.bak
        fi
      fi
      if test $parameter_check = "yes" ; then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes" ; then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1alltails.dat ; then
        ./gspcompare gsp1alltails.dat alltails.gsp > alltails.diff
        if test -r gspcompare.bak ; then
          echo "  look at alltails.diff for difference"
        else
          echo "  results are okay"
          rm -f alltails.diff
        fi
      else
        echo "\--> no output file from gaspan, sorry"
        touch alltails.diff
        exit
      fi
    fi

# Gamma spectrum with symmetric peaks (Ta182-Source)
    if test $symmetric_peaks = "yes" ; then
      echo "gaussian peaks in gamma spectra: ta182.dat"
      echo "get opt ta182" > batch.bak
      if test $save_fits = "yes" ; then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes" ; then
        echo "set display -save=ta182.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f ta182spg.$DIS
      fi
      if test $print_fit_history = "yes" ; then
        echo "set fit -print" >> batch.bak
        if test $testprint ; then
          echo "test print "$testprint >> batch.bak
        fi
      fi
      if test $parameter_check = "yes" ; then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes" ; then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1ta182.dat ; then
        ./gspcompare gsp1ta182.dat ta182.gsp > ta182.diff
        if test -r gspcompare.bak ; then
          echo "  look at ta182.diff for difference"
        else
          echo "  results are okay"
          rm -f ta182.diff
        fi
      else
        echo "\--> no output file from gaspan, sorry"
        touch ta182.diff
        exit
      fi
    fi
# Gamma spectrum with symmetric peaks (Ta182-Source) and external error
# with error smoothing
    if test $symmetric_peaks_error = "yes" ;  then
      echo "gaussian peaks with external error and error smoothing: ta182.daterr"
      echo "get opt ta182" > batch.bak
      echo "set file -file=ta182-0.daterr" >> batch.bak
      if test $save_fits = "yes" ; then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes"
        then
        echo "set display -save=ta182-0.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f ta182-0.$DIS
      fi
      if test $print_fit_history = "yes"
        then
        echo "set fit -print" >> batch.bak
      fi
      if test $parameter_check = "yes"
        then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes"
        then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1ta182-0.daterr
        then
          ./gspcompare gsp1ta182-0.daterr ta182.gsp > ta182-0.diff
        if test -r gspcompare.bak
        then
          echo "  look at ta182-0.diff for difference"
        else
          echo "  results are okay"
          rm -f ta182-0.diff
        fi
      else
        echo "\07-> no output file from gaspan, sorry"
        touch ta182-0.diff
        exit
      fi
    fi
# Gamma spectrum with symmetric peaks (TA182-Source) and external error
# without error smoothing
    if test $symmetric_peaks_error = "yes" 
      then
      echo "gaussian peaks with external error and no error smoothing: ta182-0.daterr"
      echo "get opt ta182" > batch.bak
      echo "set file -file=ta182-0.daterr" >> batch.bak
      echo "set statistics -nosmooth_error" >> batch.bak
      if test $save_fits = "yes"
        then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes"
        then
        echo "set display -save=ta182-0-nosm.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f ta182-0-nosm.$DIS
      fi
      if test $print_fit_history = "yes"
        then
        echo "set fit -print" >> batch.bak
      fi
      if test $parameter_check = "yes"
        then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes"
        then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1ta182-0.daterr
        then
          ./gspcompare gsp1ta182-0.daterr ta182-0.gsp > ta182-0-nosm.diff
        if test -r gspcompare.bak
        then
          echo "  look at ta182-0-nosm.diff for difference"
        else
          echo "  results are okay"
          rm -f ta182-0-nosm.diff
        fi
      else
        echo "\07-> no output file from gaspan, sorry"
        touch ta182-0-nosm.diff
        exit
      fi
    fi
# Gamma spectrum with symmetric peaks spectrum multiplied with 0.0001: ta182-4.daterr
    if test $symmetric_peaks_fac = "yes" ; then
      echo "gaussian peaks in factorised  gamma spectra: ta182-4.dat"
      echo "get opt ta182" > batch.bak
      echo "set file -file=ta182-4.daterr" >> batch.bak
      echo "set statistics -nosmooth_error" >> batch.bak
      if test $save_fits = "yes"
        then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes"
        then
        echo "set display -save=ta182-4.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f ta182-4.$DIS
      fi
      if test $print_fit_history = "yes"
        then
        echo "set fit -print" >> batch.bak
      fi
      if test $parameter_check = "yes"
        then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes"
        then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1ta182-4.daterr
        then
          ./gspcompare gsp1ta182-4.daterr ta182-4.gsp > ta182-4.diff
        if test -r gspcompare.bak
        then
          echo "  look at ta182-4.diff for difference"
        else
          echo "  results are okay"
          rm -f ta182-4.diff
        fi
      else
        echo "\07-> no output file from gaspan, sorry" > ta182-4.diff
        exit
      fi
    fi
# Gamma spectrum with asymmetric peaks: eu152.dat
    if test $asymmetric_peaks = "yes"
      then
      echo "asymmetric peaks in gamma spectra: eu152.dat"
      echo "get opt eu152" > batch.bak
      if test $save_fits = "yes"
        then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes"
        then
        echo "set display -save=eu152.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f eu152.$DIS
      fi
      if test $print_fit_history = "yes"
        then
        echo "set fit -print" >> batch.bak
      fi
      if test $parameter_check = "yes"
        then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes"
        then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1eu152.dat
        then
          ./gspcompare gsp1eu152.dat eu152.gsp > eu152.diff
        if test -r gspcompare.bak
        then
          echo "  look at eu152.diff for difference"
        else
          echo "  results are okay"
          rm eu152.diff
        fi
      else
        echo "\07-> no output file from gaspan, sorry"
        touch eu152.diff
        exit
      fi
    fi
# Gamma spectrum with asymmetric peaks and parameter check: eu152.dat
    if test $asymmetric_peaks_ch = "yes"
      then
      echo "asymmetric peaks in gamma spectra with option set par -all_check: eu152.dat"
      echo "get opt eu152" > batch.bak
      if test $save_fits = "yes"
        then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes"
        then
        echo "set display -save=eu152-c.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f eu152-c.$DIS
      fi
      if test $print_fit_history = "yes"
        then
        echo "set fit -print" >> batch.bak
      fi
      echo "set parameter -all_check" >> batch.bak
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1eu152.dat
        then
          ./gspcompare gsp1eu152.dat eu152-c.gsp > eu152-c.diff
        if test -r gspcompare.bak
        then
          echo "  look at eu152-c.diff for difference"
        else
          echo "  results are okay"
          rm eu152-c.diff
        fi
      else
        echo "\07-> no output file from gaspan, sorry"
        touch eu152-c.diff
        exit
      fi
    fi
# Gamma spectrum with asymmetric peaks spectrum multiplied with 0.01: eu152-2.daterr
    if test $asymmetric_peaks_fac = "yes" 
      then
      echo "asymmetric peaks in factorised gamma spectrum: eu152-2.daterr"
      echo "get opt eu152" > batch.bak
      echo "set file -file=eu152-2.daterr" >> batch.bak
      echo "set statistic -nosmooth" >> batch.bak
      if test $save_fits = "yes"
        then
        echo "set fit -save" >> batch.bak
      fi
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes"
        then
        echo "set display -save=eu152-2.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f eu152-2.$DIS
      fi
      if test $print_fit_history = "yes"
        then
        echo "set fit -print" >> batch.bak
      fi
      if test $parameter_check = "yes"
        then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes"
        then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1eu152-2.daterr
        then
          ./gspcompare gsp1eu152-2.daterr eu152-2.gsp > eu152-2.diff
        if test -r gspcompare.bak
        then
          echo "  look at eu152-2.diff for difference"
        else
          echo "  results are okay"
          rm eu152-2.diff
        fi
      else
        echo "\07-> no output file from gaspan, sorry"
        touch eu152-2.diff
        exit
      fi
    fi
# Gamma spectrum with asymmetric peaks: eu152.dat
# in addition energy and efficiency calibration and
    if test $with_calibration = "yes" ; then
      echo "asymmetric peaks with energy and efficiency calibration: eu152.dat"
      rm -f batch.bak
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes" ; then
        echo "set display -save=eu152-e.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f eu152-e.$DIS
      fi
      echo "set display -energy" >> batch.bak
      if test $print_fit_history = "yes" ; then
        echo "set fit -print" >> batch.bak
      fi
      echo "get opt eu152-e" >> batch.bak
#      echo "y" >> batch.bak
#      echo "y" >> batch.bak
      if test $save_fits = "yes" ; then
        echo "set fit -save" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1eu152.dat ; then
        ./gspcompare gsp1eu152.dat eu152-e.gsp > eu152-e.diff
        if test -r gspcompare.bak ; then
          echo "  look at eu152-e.diff for difference" 
        else
          echo "  results are okay"
          rm eu152-e.diff
        fi
      else
        echo "-> no output file from gaspan, sorry"
        touch  eu152-e.diff
        exit
      fi
    fi
# Q3D particle spectrum with large asymmetries: list of regions: q3d.dat
    if test $q3d = "yes" ; then
      echo "asymmetric peaks in q3d particle spectrum with list of regions: q3d.dat"
      rm -f batch.bak
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes" ; then
        echo "set display -save=q3d.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f q3d.$DIS
      fi
      if test $print_fit_history = "yes" ; then
        echo "set fit -print" >> batch.bak
      fi
      echo "get opt q3d" >> batch.bak
      echo " " >> batch.bak
      echo " " >> batch.bak
      if test $save_fits = "yes" ; then
        echo "set fit -save" >> batch.bak
      fi
      if test $parameter_check = "yes" ; then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes" ; then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1q3d.dat ; then
        ./gspcompare gsp1q3d.dat q3d.gsp > q3d.diff
        if test -r gspcompare.bak ; then
          echo "  look at q3d.diff for difference"
        else
          echo "  results are okay"
          rm q3d.diff
        fi
      else
        echo "-> no output file from gaspan, sorry"
        touch q3d.diff
        exit
      fi
    fi
# Q3D particle spectrum with large asymmetries: list of regions
    if test $q3dc = "yes" ; then
      echo "asymmetric peaks with option: set par -all_check: q3d.dat "
      rm -f batch.bak
      if test -z $DIS ; then
        echo "set display -off" >> batch.bak
      elif test $save_display = "yes" ; then
        echo "set display -save=q3d-c.$DIS" >> batch.bak
        echo "set display -log" >> batch.bak
        rm -f q3d-c.$DIS
      fi
      if test $print_fit_history = "yes" ; then
        echo "set fit -print" >> batch.bak
      fi
      echo "get opt q3d" >> batch.bak
      echo " " >> batch.bak
      echo " " >> batch.bak
      if test $save_fits = "yes" ; then
        echo "set fit -save" >> batch.bak
      fi
      if test $parameter_check = "yes" ; then
        echo "set parameter -check" >> batch.bak
      fi
      if test $extended_parameter_check = "yes" ; then
        echo "set parameter -extended_check" >> batch.bak
      fi
      echo "set parameter -all_check" >> batch.bak
      echo "go" >> batch.bak
      echo "exit" >> batch.bak
      $timing nice -n 19 $GASPAN < batch.bak > /dev/null
      if test -r gsp1q3d.dat ; then
        ./gspcompare gsp1q3d.dat q3d-c.gsp > q3d-c.diff
        if test -r gspcompare.bak ; then
          echo "  look at q3d-c.diff for difference"
        else
          echo "  results are okay"
          rm q3d-c.diff
        fi
      else
        echo "-> no output file from gaspan, sorry" 
        touch q3d-c.diff
        exit
      fi
    fi
#cleanup
  if test $cleanup = "yes" ; then
    rm -f gsp*.dat* par*.dat* *.bak*
    rm gspcompare
  fi
# gaspan was not found
  else
    echo "\07 not found: "$GASPAN
  fi
