#! /bin/bash
# Last edited on 2021-12-04 03:37:02 by stolfi

# Extracs from the {out.txt} files the Tcool and Tfab of reference
# programs (Slic3r, RP3, SCN, SCA) for all dataseta.

# Output file name is "${outdir}/plot_data_${alg}.csv"
# Output file format is "{DATASET} {TCOOLMAX} {TFAB} {N}" where
# {N} is a sequential number from 0

dir="$1"; shift
alg="$1"; shift
datasets=( $@ )

dfile="${dir}/plot_data_${alg}.csv"

rm -f ${dfile}
nd=0
for ds in ${datasets[@]} ; do

  afile="${dir}/out/${ds}_${alg}/out.txt"
  Tcoolmax=( `cat ${afile} | egrep -e 'CoolTime:'` ); Tcoolmax=${Tcoolmax[1]}
  Tfab_alg=( `cat ${afile} | egrep -e 'FabTime:'` ); Tfab_alg=${Tfab_alg[1]}
  Trast=( `cat ${afile} | egrep -e 'RastTime:'` ); Trast=${Trast[1]}
  echo "Tcoolmax = ($Tcoolmax)  Tfab_alg = ($Tfab_alg)  Trast = ($Trast)" 1>&2

  rfile="${dir}/out/${ds}_RP3/out.txt"
  Tfab_RP3=( `cat ${rfile} | egrep -e 'FabTime:'` ); Tfab_RP3=${Tfab_RP3[1]}

  sfile="${dir}/out/${ds}_SLIC3R/out.txt"
  Tfab_SLIC3R=( `cat ${sfile} | egrep -e 'FabTime:'` ); Tfab_SLIC3R=${Tfab_SLIC3R[1]}

  Tfab_min=`echo "r = ${Tfab_RP3}; s = ${Tfab_SLIC3R}; if (r < s){ r; } else { s; }" | bc -lq`
  echo "Tfab_RP3 = ($Tfab_RP3)  Tfab_SLIC3R = ($Tfab_SLIC3R)  Tfab_min = ($Tfab_min)" 1>&2

  Rfab=`echo "r = ${Trast}; a = ${Tfab_alg}; m = ${Tfab_min}; (a-r)/(m-r);" | bc -lq`

  printf "%s %.1f %.1f %d\n" "${ds}" "${Tcoolmax}" "${Rfab}" "${nd}"  >> ${dfile}
  nd=$(( ${nd} + 1 ))
done
