#!/bin/sh

rm -f mplotr/vkmh.print
rm -f mplotr/S11std.print
rm -f mplotr/S12std.print

if [ ! -f mplotf/print_vkmh_2axle.mplotf ]; then
echo "
 * warning * In Script `basename $0`
             File \"mplotf/print_vkmh_2axle.mplotf\" is missing
             Shall script `basename $0` create the file for you (y/n)?
"
read input
 if [ "$input" = "y" ]; then
  mkdir -p mplotf
echo "
##
##      Input data for program MPLOT
##
  ilaser= 6 iscren= 0

  filt std  100  S11    S11std  lsa_11.pn
  filt std  100  S12    S12std  lsa_12.pn

##
## Write sliding STD-values to files
## ---------------------------------

  pryfil \"mplotr/vkmh.print\"
  print variable vkmh

  pryfil \"mplotr/S11std.print\"
  print variable S11std

  pryfil \"mplotr/S12std.print\"
  print variable S12std" > mplotf/print_vkmh_2axle.mplotf
 else
  exit 1
 fi
fi

mplot -no_addarg -no_interactive -use_MPfile  mplotf/print_vkmh_2axle.mplotf  $1 > /dev/null
if [ $? -ne 0 ]; then
echo "
 ***ERROR*** In Script `basename $0`
             Exit status from program MPLOT not equal 0'
             Please open file `basename $0` and remove
             the redirection of standard output to /dev/null
"
 exit 1
fi

nlines=`awk 'END {print NR}' mplotr/S11std.print`
if [ $? -ne 0 ]; then nlines=0; fi

if [ $nlines -lt 15 ]; then
 echo "$1	no results was found"
 exit 1
fi

# echo -n "$1	"
exec octave -q <<+

load mplotr/vkmh.print;         % vkmh  (:,1)= time;       vkmh  (:,2)= speed
load mplotr/S11std.print       % S11std(:,1)= lsa_11.pn;  S11std(:,2)= S11std
load mplotr/S12std.print       % S12std(:,1)= lsa_12.pn;  S12std(:,2)= S12std


for i= length(vkmh):-1:1
 if (S11std(i,2) > 2.5e3 ); break; end
 if (i <= 1); error('Cannot find level 2.5[kN]'); end
end
S11crit= vkmh(i,2);

for i= length(vkmh):-1:1
 if (S12std(i,2) > 2.5e3 ); break; end
 if (i <= 1); error('Cannot find level 2.5[kN]'); end
end
S12crit= vkmh(i,2);


%printf('%f %f %f %f\n', S11crit,S12crit)
printf('%f\n', min( [S11crit,S12crit] ))
+
