#!/bin/csh

set usage = \
'usage : plotbd [-h|-i] [OPT ...] [COLS] [RANGES] (RUN|PFILE|-) ...\
OPT   : -bw | (-k name) | (-eps eps-name)\
COLS  : X:Y[:Z]\
RANGES: "XRANGE" ["YRANGE" ["ZRANGE"]]\
NOTE  : gnuplot ranges must be enclosed in "..."'

if( "$1" == "-h" || $#argv == 0 ) then
	echo $usage:q >> /dev/stderr
	exit 1
endif

set tmp_name   = "data/tmpbd"
set eps_name   = ""
set eps_mode   = "color solid"
set gp_options = "-persist"
set gp_files   = ".gp"
set rm_files   = "rm -f"
set mono       = ""
set slt        = "with lines 2"
set ult        = "with lines 1"
set lpt        = "with points pt 13 lt 3"
set bpt        = "with points pt 1 lt 3 ps 2"
set hbt        = "with points pt 6 lt 6"
set pdt        = "with points pt 4 lt 4"
set trt        = "with points pt 6 lt 6"
set uzt        = "with points pt 12 lt 5"

while( 1 )
	switch( "$1" )
	
	case "-i":
		set gp_options = ""
		set gp_files   = ".gp -"
		shift; breaksw

	case "-k":
		set rm_files = ":"
		set tmp_name = $2
		shift; shift; breaksw
	
	case "-eps":
		set eps_name = $2
		if( "$mono" == "") then
			set slt = "with lines 2"
			set ult = "with lines 1"
			set lpt = "with points pt 13 lt 3"
			set bpt = "with points pt 1 lt 3 ps 2"
			set hbt = "with points pt 6 lt 7"
			set pdt = "with points pt 4 lt 4"
			set trt = "with points pt 6 lt 7"
			set uzt = "with points pt 12 lt 5"
		else
			set slt = "with lines 1"
			set ult = "with lines 3"
			set lpt = "with points 13"
			set bpt = "with points pt 1 ps 2"
			set hbt = "with points 6"
			set pdt = "with points 4"
			set trt = "with points 6"
			set uzt = "with points 12"
		endif
		shift; shift; breaksw
	
	case "-bw":
		set mono     = "-mono"
		set eps_mode = "monochrome dashed"
		if( "$eps_name" == "") then
			set slt = "with lines 1"
			set ult = "with lines 4"
			set lpt = "with points 13"
			set bpt = "with points pt 1 ps 2"
			set hbt = "with points 6"
			set pdt = "with points 4"
			set trt = "with points 6"
			set uzt = "with points 12"
		else
			set slt = "with lines 1"
			set ult = "with lines 3"
			set lpt = "with points 13"
			set bpt = "with points pt 1 ps 2"
			set hbt = "with points 6"
			set pdt = "with points 4"
			set trt = "with points 6"
			set uzt = "with points 12"
		endif
		shift; breaksw
	
	default:
		break
	
	endsw
end

set cols = `awk 'BEGIN {\
	if("'$1:q'"~/^[0-9]+:[0-9]+(:[0-9]+)?$/) \
		{split("'$1:q'",cols,":"); exit 0} else exit 1} \
	END { for(i in cols) print cols[i] }'`

if( $status == 0 ) then
	shift
	set ncols = ()
	foreach c ($cols[1-])
		@ c = $c + 3
		set ncols = ($ncols[1-] $c)
	end
	set cols = ($ncols[1-])
else
	set cols = (4 5)
endif

awk 'BEGIN {if("'$1:q'"~/^\[[0-9\.e+-]*:?[0-9\.e+-]*\]$/) exit 0; else exit 1}'

if( $status == 0 ) then
	set xrange = "$1"
	shift
else
	set xrange = ()
endif

awk 'BEGIN {if("'$1:q'"~/^\[[0-9\.e+-]*:?[0-9\.e+-]*\]$/) exit 0; else exit 1}'

if( $status == 0 ) then
	set yrange = "$1"
	shift
else
	set yrange = ()
endif

awk 'BEGIN {if("'$1:q'"~/^\[[0-9\.e+-]*:?[0-9\.e+-]*\]$/) exit 0; else exit 1}'

if( $status == 0 ) then
	set zrange = "$1"
	shift
else
	set zrange = ()
endif

if( $#argv == 0 ) then
	echo "plotbd: too few arguments"  >> /dev/stderr
	echo $usage:q >> /dev/stderr
	exit 1
endif

test -d data || mkdir data

rm -f ${tmp_name}*.dat ${tmp_name}.gp >& /dev/null

echo "# data files generated with command:" >> ${tmp_name}.gp
echo "# bd2dat -a RUN ${tmp_name}s.dat ${tmp_name}u.dat "\\ >> ${tmp_name}.gp
echo -n "#   -LP ${tmp_name}LP.dat -BP ${tmp_name}BP.dat " >> ${tmp_name}.gp
echo "-HB ${tmp_name}HB.dat "\\ >> ${tmp_name}.gp
echo -n "#   -PD ${tmp_name}PD.dat -TR ${tmp_name}TR.dat " >> ${tmp_name}.gp
echo "-UZ ${tmp_name}UZ.dat" >> ${tmp_name}.gp
echo "#" >> ${tmp_name}.gp
@ rows = $#argv / 10
if( $rows >= 1) then
	echo "# RUN = $argv[1-10]" >> ${tmp_name}.gp
else
	echo "# RUN = $argv[1-]" >> ${tmp_name}.gp
endif
set i = 1
while( $i < $rows )
	@ beg = $i * 10 + 1
	@ end = $i * 10 + 10
	echo "#       $argv[$beg-$end]" >> ${tmp_name}.gp
	@ i = $i + 1
end
@ rnum = $i * 10
if( $rnum < $#argv ) then
	@ beg = $i * 10 + 1
	echo "#       $argv[$beg-]" >> ${tmp_name}.gp
endif
echo "" >> ${tmp_name}.gp

foreach run ($argv[1-])
	if(! ( -ef data/p.${run} || "${run}" == "-" || -ef "${run}" ) ) then
		echo "plotbd: run or file '${run}' does not exist" >> /dev/stderr
		goto err_exit
	endif
	bd2dat -a $run ${tmp_name}s.dat ${tmp_name}u.dat \
		-LP ${tmp_name}LP.dat -BP ${tmp_name}BP.dat -HB ${tmp_name}HB.dat \
		-PD ${tmp_name}PD.dat -TR ${tmp_name}TR.dat -UZ ${tmp_name}UZ.dat
	if( $status ) then
		echo "plotbd: bd2dat returned with error, aborting" >> /dev/stderr
		goto err_exit
	endif
end

if( "$eps_name" != "" ) then
	echo -n 'set terminal postscript eps enhanced ' >> ${tmp_name}.gp
	echo ${eps_mode}' "Helvetica-Bold" 19'          >> ${tmp_name}.gp
	echo 'set output "'${eps_name}'.eps"'           >> ${tmp_name}.gp
endif

echo "set grid" >> ${tmp_name}.gp
echo "set key box" >> ${tmp_name}.gp

if( $#cols == 2 ) then
	set cols = "${cols[1]}:${cols[2]}"
	echo "plot $xrange$yrange "\\ >> ${tmp_name}.gp
else
	set cols = "${cols[1]}:${cols[2]}:${cols[3]}"
	echo "splot $xrange$yrange$zrange "\\ >> ${tmp_name}.gp
endif

if(-ef ${tmp_name}s.dat) then
	echo -n '"'${tmp_name}s.dat'" using '${cols}' title " stable" '$slt \
		>> ${tmp_name}.gp
	if(-ef ${tmp_name}u.dat) then
		echo ", "\\ >> ${tmp_name}.gp
		echo -n '"'${tmp_name}u.dat'" using '${cols}' title " unstable" '$ult \
			>> ${tmp_name}.gp
	endif
else
	if(-ef ${tmp_name}u.dat) then
		echo -n '"'${tmp_name}u.dat'" using '${cols}' title " unstable" '$ult \
			>> ${tmp_name}.gp
	endif
endif
if(-ef ${tmp_name}LP.dat) then
	echo ", "\\ >> ${tmp_name}.gp
	echo -n '"'${tmp_name}LP.dat'" using '${cols}' title " LP" '$lpt \
		>> ${tmp_name}.gp
endif
if(-ef ${tmp_name}BP.dat) then
	echo ", "\\ >> ${tmp_name}.gp
	echo -n '"'${tmp_name}BP.dat'" using '${cols}' title " BP" '$bpt \
		>> ${tmp_name}.gp
endif
if(-ef ${tmp_name}HB.dat) then
	echo ", "\\ >> ${tmp_name}.gp
	echo -n '"'${tmp_name}HB.dat'" using '${cols}' title " HB" '$hbt \
		>> ${tmp_name}.gp
endif
if(-ef ${tmp_name}PD.dat) then
	echo ", "\\ >> ${tmp_name}.gp
	echo -n '"'${tmp_name}PD.dat'" using '${cols}' title " PD" '$pdt \
		>> ${tmp_name}.gp
endif
if(-ef ${tmp_name}TR.dat) then
	echo ", "\\ >> ${tmp_name}.gp
	echo -n '"'${tmp_name}TR.dat'" using '${cols}' title " TR" '$trt \
		>> ${tmp_name}.gp
endif
if(-ef ${tmp_name}UZ.dat) then
	echo ", "\\ >> ${tmp_name}.gp
	echo -n '"'${tmp_name}UZ.dat'" using '${cols}' title " UZ" '$uzt \
		>> ${tmp_name}.gp
endif
echo "" >> ${tmp_name}.gp

gnuplot $mono $gp_options ${tmp_name}${gp_files}

if( $status ) then
	echo "plotbd: gnuplot returned with error, aborting" >> /dev/stderr
	goto err_exit
endif

$rm_files ${tmp_name}*.dat ${tmp_name}.gp >& /dev/null

exit 0

####################### error exit

err_exit:

rm -f ${tmp_name}*.dat ${tmp_name}.gp >& /dev/null
if( ${eps_name} != "" ) rm -f ${eps_name}.eps

exit 1
