#! /bin/bash

function cmd() {
  if smo -b -f "$filter" -x "$xbk" -i "./dat/${nam}.xy" -o "./smo/${nam}.xy"
  then
  # calculate area of low and high frequency part and output normalized
  # data in a table, same name, in the ./nrm directory
    if ./bin/nrm $nam
    then
      aws="$(grep "#" ./nrm/${nam}.xy | cut -d\  -f3)"
    else
      echo -e "# $nam : nrm failed!"
      return 1
    fi
  else
    echo -e "# $nam : smo failed!"
    return 2
  fi
  return 0
}

# usage:
#       ./showxbk.sh "15,150;300,500"
#
function showxbk() {
  echo "gsave 0.9 setgray Clip"
  while test $# -gt 0
  do
    echo "
$1 Xmin ge {
[
$1 Ymin $2 Ymin $2 Ymax $1 Ymax
] Surface } if
"
    shift 2
  done
  echo "grestore"
}

# generate jog graphics using the nrm file
# for the low frequency regin... The best scale
# must be choosen manually...
function gra1() {
  ymin=-0.02 # small negative value
  ymax=1 # 10 for low freq. 30..60 for high freq.
  xmin=0
  xmax=1500
  gradx="[(0)(500)(1000)(1500)]" # should make sense with xmin xmax
  grady="[ (0) ($ymax) ]"
  nrm="./nrm/${nam}.xy"
  jog="./fig1/${nam}.jog"
  miseEnPage="
  (HelveticaF) 15 Font
  [ $xmin $xmax $ymin $ymax ] Axe2
  $xbkjog
  [ $xmin $xmax $ymin $ymax ] Axe2
  $gradx GradX
  (cm^-^1) GradX
  $grady GradY
  (a.u.) GradY
  [ Xmin 0 Xmax 0 ] 1 Dash Lines
  1 Size Cross Black 0 Dash NoLines
  gsave
  Clip
  "
#-------------------------------------------------------------------------------
  echo "$miseEnPage" > "$jog"
  eval "showxbk $(echo "$1" | sed "s/[,;]/ /g")" >> "$jog"
  echo "
  /data [
    $(awk -v xmax="$xmax" '$1<xmax && !/#/' "$nrm")
  ] def
  data 4 1 2 Cols Black Plot
  data 4 1 3 Cols Blue Plot
  data 4 1 4 Cols Red Plot
  grestore
  gsave
  (Helvetica) 10 Font
  170 430 (nam) (typ) (wat) (xbk / f) (aws) Wrt
  (Courrier) 10 Font
  230 430 ($nam $compo) ($typ) ($wat) ($xbk / $filter) ($aws) Wrt
  grestore
  " >> "$jog"
  return 0
}

function gra2() {
  ymin=-0.1 # small negative value
  ymax=2.5 # 10 for low freq. 30..60 for high freq.
  xmin=2500
  xmax=4000
  gradx="[(2500)(3000)(3500)(4000)]" # should make sense with xmin xmax
  grady="[ (0) ($ymax) ]"
  nrm="./nrm/${nam}.xy"
  jog="./fig2/${nam}.jog"

  miseEnPage="
  (HelveticaF) 15 Font
  [ $xmin $xmax $ymin $ymax ] Axe2
  $xbkjog
  $gradx GradX
  (cm^-^1) GradX
  $grady GradY
  (a.u.) GradY
  [ Xmin 0 Xmax 0 ] 1 Dash Lines
  1 Size Cross Black 0 Dash NoLines
  gsave
  Clip"

  echo "$miseEnPage" > "$jog"

  echo "
  /data [
    $(awk -v xmin="$xmin" '$1>xmin && !/#/' "$nrm")
  ] def

  data 4 1 2 Cols Black Plot
  data 4 1 3 Cols Blue Plot
  data 4 1 4 Cols Red Plot
  grestore
  gsave
  (Helvetica) 10 Font
  170 430 (nam) (typ) (wat) (xbk / f) (aws) Wrt
  (Courrier) 10 Font
  230 430 ($nam $compo) ($typ) ($wat) ($xbk / $filter) ($aws) Wrt
  grestore
  " >> "$jog"
  return 0
}

out="result.txt"
echo "" > "$out"
# empty lines are echoed
# note that aws will be ignored as it is calculated
# the all point is to have the same format for input and output
while read nam compo wat filter xbk typ
do
  if test "$(expr index "$nam" "#")" -eq 1 || test -z "$nam"
  then
    :
  else
    if cmd
    then
       ybk="0;$xbk;4000"
       xbkjog=`eval "showxbk $(echo "$ybk" | sed 's/[,;]/ /g')"`
       echo -e "$nam\t$compo\t$wat\t$aws\t$xbk\t$typ" >> "$out"
       echo -e "$nam\t$compo\t$wat\t$aws\t$xbk\t$typ"
       # low and high frequ. graphics in fig1/*.jog & fig2/*.jog
       gra1
       gra2
    fi
  fi
done
