#!/bin/sh
#
# wf2tr - convert a runfile with only waveforms to one with traces
#
# Usage:  wf2tr [-n num] [-l length] [-w 'wflist'] [-r st,en] runfile outfile
#
# where:  -n num	specifies the number of frames to generate in the run
#			(default is 1)
#	  -l length	specifies window length for generated sweeps
#			(default is run length divided by number of frames,
#			 or 1s, whichever is less)
#	  -w 'wflist'   specifies list of waveforms to convert
#			(default is "all")
#	  -r st,en	specifies the start and end of the range to convert,
#			in seconds (default is 0 to max)
#	  runfile	specifies an input run file name to be converted
#			(no default, runfile must be specified)
#	  outfile	specifies an output run file name to be created
#			(no default, outfile must be specified)
#
# Copyright (c) 2005, Gilles Detillieux, Spinal Cord Research Centre,
# University of Manitoba.  All Rights Reserved.
#

nframes=1 length= wflist=all range=all
T=/tmp/wtr$$
while :
do
	case "$1" in
	-\?|-help|--help)	sed -n '3,/^# Univ/s/^#/ /p' "$0"; exit ;;
	-debug|--debug)	shift; T=wtr.out. ;;
	-n|-N)	shift; nframes="$1"; shift ;;
	-l|-L)	shift; length="$1"; shift ;;
	-w|-W)	shift; wflist="$1"; shift ;;
	-r|-R)	shift; range="$1"; shift ;;
	-*)	set --; break ;;
	*)	break ;;
	esac
done

case "$#" in
2)	;;
*)	echo "Usage:  wf2tr [-n num] [-l length] [-w 'wflist'] [-r st,en] runfile outfile
	or wf2tr --help	for detailed usage information" >&2; exit 1 ;;
esac

case "$nframes" in
[1-9]*)	;;
*)	echo "$0: Invalid number of frames: -n $nframes" >&2
	exit 1
	;;
esac

range=`echo "$range" | tr -d ' '`
case "$range" in
""|[Aa][Ll][Ll])
	start=0 end=max
	;;
[0-9]*\,[0-9]* | [0-9]*\,[Mm][Aa][Xx])
	start=`echo "$range" | sed 's/^\([-0-9.e+]*\),.*/\1/'`
	end=`echo "$range" | sed 's/^[^,]*, *\([-0-9.e+MmAaXx]*\) *$/\1/'`
	case "$start,$end" in
	[0-9]*\,[0-9Mm]*)	;;
	*)	echo "$0: Invalid range specified: -r $range" >&2
		exit 1
		;;
	esac
	;;
*)	echo "$0: Invalid range specified: -r $range" >&2
	exit 1
	;;
esac

case "$end" in
[0-9]*)	;;
*)	case "$end" in
	[Mm][Aa][Xx])	;;
	*)	echo "$0: Unknown end value '$end', assuming maximum" >&2 ;;
	esac
	rend=`echo "sdutsqqqcvwqyn" |
		TERM=dumb DISPLAY= analysis "$1" 2>/dev/null |
		sed -n 's/^Run length:[ 	]*\([0-9.]*\).*/\1/p'`
	case "$rend" in
	[0-9]*)	end="$rend" ;;
	*)	echo "$0: Unable to determine length of run '$1'" >&2
		exit 1
		;;
	esac
	;;
esac

case "$length" in
[0-9]*)	;;
"")
	srate=`echo "cvwqyn" |
		TERM=dumb DISPLAY= analysis "$1" 2>/dev/null |
		sed -n 's/^Sampling rate:[ 	]*\([0-9.]*\).*/\1/p'`
	length=`sysvecho "scale=10\nw=($end-$start-(1/$srate))/$nframes\nif (w > 1) w = 1\nw" | bc 2>/dev/null` ;;
*)	echo "$0: Invalid window length specified: -l $length" >&2
	exit 1
	;;
esac

outfile=$2
case "$outfile" in
*.frm)	;;
*)	outfile="$outfile.frm" ;;
esac

if [ -e "$outfile" ]
then
	echo "$0: Output runfile already exists: $outfile" >&2
	exit 1
fi

case "${T}" in
/tmp/*)	trap "rm -f ${T}[ab]" 0			# cleanup on exit
	trap "exit 1" 1 2 3 15			# exit, & cleanup, if killed
	;;
esac
ascript=${T}a
diags=${T}b

cat <<! > $ascript
sf$1
nnndutsqqqawcsrs$start
e$end
qcw-1
qawl$wflist
w$length
qb$nframes
pyqqby$2
y
qyn
!

if ! TERM=dumb DISPLAY= analysis < $ascript > $diags 2>&1
then
	sed -n -e 's/FATAL ERROR: /analysis: /p' $diags >&2
	echo "$0: analysis failed!" >&2
fi
