#!/bin/csh

set usage = \
'usage : plotsol [-h|-i] [OPT ...] [COLS] [RANGES] FPM|SPM\
OPT   : -bw | (-bl n) | (-k name) | (-eps eps-name)\
COLS  : X:Y[:Z]\
RANGES: "XRANGE" ["YRANGE" ["ZRANGE"]]\
FPM   : RUN | ( PFILE|-  QRUN|QFILE )\
SPM   : (QRUN|QFILE|-) LAB ...\
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/tmpsol"
set eps_name   = ""
set eps_mode   = "color solid"
set gp_options = "-persist"
set gp_files   = ".gp"
set rm_files   = "rm -f"
set mono       = ""
set hidden3d   = 0
set bl         = ""
set slt        = "with lines 2"
set ult        = "with lines 1"
set nlt        = "with lines 3"
set lpt        = "with lines lt 3 lw 2"
set bpt        = "with linespoints pt 1 lt 3 ps 2 lw 2"
set hbt        = "with lines lt 6 lw 2"
set pdt        = "with lines lt 4 lw 2"
set trt        = "with lines lt 6 lw 2"
set uzt        = "with points pt 12 lt 5"

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

	case "-bl":
		set bl = "-bl $2"
		shift; 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 nlt = "with lines 3"
			set lpt = "with lines lt 3 lw 2"
			set bpt = "with linespoints pt 1 lt 3 lw 2"
			set hbt = "with lines lt 7 lw 2"
			set pdt = "with lines lt 4 lw 2"
			set trt = "with lines lt 7 lw 2"
			set uzt = "with points pt 12 lt 5"
		else
			set slt = "with lines 1"
			set ult = "with lines 4"
			set nlt = "with lines 1"
			set lpt = "with lines lt 1 lw 2"
			set bpt = "with linespoints lt 1 lw 2 pt 1"
			set hbt = "with linespoints lt 1 lw 2 pt 6"
			set pdt = "with linespoints lt 1 lw 2 pt 4"
			set trt = "with linespoints lt 1 lw 2 pt 6"
			set uzt = "with points pt 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 nlt = "with lines 1"
			set lpt = "with lines lt 1 lw 2"
			set bpt = "with linespoints lt 1 lw 2 pt 1"
			set hbt = "with linespoints lt 1 lw 2 pt 6"
			set pdt = "with linespoints lt 1 lw 2 pt 4"
			set trt = "with linespoints lt 1 lw 2 pt 6"
			set uzt = "with points pt 12"
		else
			set slt = "with lines 1"
			set ult = "with lines 4"
			set nlt = "with lines 1"
			set lpt = "with lines lt 1 lw 2"
			set bpt = "with linespoints lt 1 lw 2 pt 1"
			set hbt = "with linespoints lt 1 lw 2 pt 6"
			set pdt = "with linespoints lt 1 lw 2 pt 4"
			set trt = "with linespoints lt 1 lw 2 pt 6"
			set uzt = "with points pt 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
else
	set cols = ()
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 "plotsol: too few arguments"  >> /dev/stderr
	echo $usage:q >> /dev/stderr
	exit 1
endif

if(! ( -ef data/p.$1 || "$1" == "-" || -ef "$1" ) ) then
	echo "plotsol: run or file '$1' does not exist" >> /dev/stderr
	exit 1
endif

if( "$1" == "-" ) then
	cat > ${tmp_name}.stdin
	set src = ${tmp_name}.stdin
else
	set src = "$1"
endif

if( (! -ef data/p.$src) && -ef "$src" ) then
	if( $#argv < 2 ) then
		echo "plotsol: too few arguments" >> /dev/stderr
		echo $usage:q >> /dev/stderr
		exit 1
	endif
	if( -ef data/q.$2 || -ef "$2" ) then
		set src = ( "$src" "$2" )
		shift
		if( $#argv != 1) then
			echo "plotsol: too many arguments for family plotting mode" >> /dev/stderr
			echo $usage:q >> /dev/stderr
			exit 1
		endif
	endif
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

if($#cols == 0) then
	@ NDIM = 2
	if(-ef data/r.$src[1] ) then
		@ NDIM = `printac $src[1] NDIM`
	else if( $#src == 2 ) then
		if ( -ef data/r.$src[2] ) then
			@ NDIM = `printac $src[2] NDIM`
		endif
	endif
	if( $NDIM > 1 ) then
		set cols = (2 3)
	else
		set cols = (1 2)
	endif
endif

if( $#argv == 1 ) then
	set ncols = ()
	foreach c ($cols[1-])
		@ c = $c + 1
		set ncols = ($ncols[1-] $c)
	end
	set cols = ($ncols[1-])
	if("$bl" == "") then
		foreach c ($cols[1-])
			if($c == 1) then
				set bl = "-bl 1"
				set hidden3d = 1
			endif
		end
	endif
	fam2dat -a -p 1 $bl ${src:gq} ${tmp_name}s.dat ${tmp_name}u.dat
	if( $status ) then
		echo "plotsol: fam2dat returned with error, aborting" >> /dev/stderr
		goto err_exit
	endif
	echo "# fam2dat -a -p 1 $bl ${src:gq}  ${tmp_name}s.dat ${tmp_name}u.dat" \
		>> ${tmp_name}.gp
	fam2dat -a -p 1 ${src:gq} \
		-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 "plotsol: fam2dat returned with error, aborting" >> /dev/stderr
		goto err_exit
	endif
	echo "# fam2dat -a -p 1 ${src:gq} "\\ >> ${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
else
	sol2dat -a $bl "$src" ${tmp_name}s.dat $argv[2-]
	if( $status ) then
		echo "plotsol: sol2dat returned with error, aborting" >> /dev/stderr
		goto err_exit
	endif
	echo "# sol2dat -a "$src" $bl ${tmp_name}s.dat $argv[2-]" >> ${tmp_name}.gp
endif

echo "" >> ${tmp_name}.gp

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

if( $#argv == 1 ) echo "set key box" >> ${tmp_name}.gp
if( $hidden3d )   echo "set hidden3d offset 0" >> ${tmp_name}.gp
echo "set grid" >> ${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( $#argv == 1) then
	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
else
	echo '"'${tmp_name}'s.dat" using '${cols}' notitle '$nlt >> ${tmp_name}.gp
endif

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

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

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

exit 0

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

err_exit:

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

exit 1
