#!/bin/csh -f

echo
echo -n "Enter name of function curve : "
set name=($<)
set name_e = $name:e
if ($name_e != "fraw") then
	set name = "$name.fraw"
endif
if (! -e $name ) then
	echo "No function curve: $name"
	exit -1
endif
set num_lines = `cat $name|wc -l`
@ num_lin = $num_lines 
echo
echo "	Enter a Command Line, specifying with ## where"
echo "	the function curve values are to be inserted."
echo "	Note that the <start frame> of every layer"
echo "	can be prefixed with a +.  This means that it"
echo "	will be incremented by one every iteration."
echo
echo
echo "Example: composite output -s 1 30 1 layer0 1 layer1 +1 -f ##"
echo
echo "There are $num_lin points in $name."
echo
echo -n "Enter Command Line : "
set input=($<)
echo
set numbers = `cat $name|awk '{printf("%f\n",$2)}'`

set params = `echo $input`
loop1:
switch ("${params[1]}")
	case -s:	
		breaksw
	default:
		if ($#params == 1) then
			echo "ERROR: Must have -s option"
			exit -1
		endif
		shift params
		goto loop1
endsw

if ($#params <= "3") then
	echo "ERROR: -s needs 3 values."
	exit -1
endif

shift params
set start = ${params[1]}
set end = ${params[2]}
set step = ${params[3]}
set newval = $start
set maxsub = 0
set frame = (1 2 3 4 5 6 7 8 9 0)
set frame_orig = (1 2 3 4 5 6 7 8 9 0)

loop2:
switch ("${params[1]}")
	case +*:	
		@ maxsub++
		set tmp = `echo ${params[1]}|sed 's/+//'`
		@ frame[$maxsub] = $tmp
		@ frame_orig[$maxsub] = $tmp
		if ($#params == 1) then
			breaksw
		endif
		shift params
		goto loop2
	default:
		if ($#params == 1) then
			breaksw
		endif
		shift params
		goto loop2
endsw

# Skip one line only!
shift numbers

while ($#numbers > 0)
	set new = `echo $input|sed "s/##/${numbers[1]}/g"|sed "s/-s $start $end $step/-s $newval $newval 1/"`
	if ($maxsub != 0) then
		@ sub = 1
		while ($sub <= $maxsub)
			set new_tmp = `echo $new|sed "s/+$frame_orig[$sub]/$frame[$sub]/"`
			set new = `echo $new_tmp`
			@ frame[$sub]++
			@ sub++
		end
	endif
	$new
	if ( $status != 0 ) then
		echo "SYNTAX: $new"
    		exit -1
	endif
	echo $new
	@ newval = $newval + $step
	if ($newval > $end) then
		exit 1
	endif
	shift numbers
end
@ endval = $newval - $step
if ($endval != $end) then
	echo "Sorry, ran out of data. All $num_lin points used."
endif
