# Ngraph initialization script
new menu
system:0:login_shell=menu:0:menu
#
if [ -f "${NGRAPHHOME}/.Ngraph.ngp" ]; then
        . "${NGRAPHHOME}/.Ngraph.ngp"
elif [ -f "${NGRAPHLIB}/.Ngraph.ngp" ]; then
        . "${NGRAPHLIB}/.Ngraph.ngp"
fi
#
XCOLUMN=1
YCOLUMN=2
XAXIS=0
YAXIS=1
PLOTTYPE=mark
MARKTYPE=0
MARKSIZE=200
LINESTYLE=
LINEWIDTH=40
COLORR=0
COLORG=0
COLORB=0
COLORR2=255
COLORG2=255
COLORB2=255
HEADSKIP=0
READSTEP=1
FINALLINE=-1
AVERAGEX=0
AVERAGEY=0
MATHX=
MATHY=
LOADSETTINGS=0
#
while [ "$1" ]
do
	case $1 in
	*.prm|*.PRM|*.ngp|*.NGP)
		system:0:login_shell="menu:0:menu $1"
	;;
	*.gra|*.GRA)
		for drawable in `derive -instance draw`
		do
			del $drawable:0-!
		done
		new merge file:"$1"
	;;
	-x)
		shift
		XCOLUMN=$1
	;;
	-y)
		shift
		YCOLUMN=$1
	;;
	-X)	XAXIS=0
	;;
	-Y)	YAXIS=1
	;;
	-U)	XAXIS=2
	;;
	-R)	YAXIS=3
	;;
	-d)
		shift
		PLOTTYPE=$1
	;;
	-m)
		shift
		MARKTYPE=$1
	;;
	-o)
		shift
		MARKSIZE=$1
	;;
	-l)
		shift
		LINESTYLE=$1
	;;
	-w)
		shift
		LINEWIDTH=$1
	;;
	-c)
		shift
		COLORR=$1
		shift
		COLORG=$1
		shift
		COLORB=$1
	;;
	-C)
		shift
		COLORR2=$1
		shift
		COLORG2=$1
		shift
		COLORB2=$1
	;;
	-s)
		shift
		HEADSKIP=$1
	;;
	-r)
		shift
		READSTEP=$1
	;;
	-f)
		shift
		FINALLINE=$1
	;;
	-vx)
		shift
		AVERAGEX=$1
	;;
	-vy)
		shift
		AVERAGEY=$1
	;;
	-mx)
		shift
		MATHX=$1
	;;
	-my)
		shift
		MATHY=$1
	;;
	-ex)
		shift
		axis:0:type=$1
	;;
	-ey)
		shift
		axis:1:type=$1
	;;
	-zx)
		shift
		axis:0:min=$1
		shift
		axis:0:max=$1
		shift
		axis:0:inc=$1
	;;
	-zy)
		shift
		axis:1:min=$1
		shift
		axis:1:max=$1
		shift
		axis:1:inc=$1
	;;
	-g)
		LOADSETTINGS=1
	;;
	-n)
		echo '#!ngraph'
		echo '#%creator:' ${system:0:name}
		echo '#%version:' ${system:0:version}
		for i in `derive -instance draw`
		do
			get ${i}:0-! -id -field save
		done
		get gra:0 -id -field save
		del system:0                           
	;;
	-p)
		for i in `derive -instance draw`
		do
			del ${i}:0-!
		done
		if [ `exist gra:viewer` != 0 ]
		then
			del gra:viewer
		fi
		. $2
		new gra2prn name=OUTPUT
		gra2prn::driver=gra2ps
		gra2prn::prn=" | lpr"
		gra:viewer:device=gra2prn:OUTPUT
		gra:viewer:open
		gra:viewer:draw                      
		gra:viewer:close
		del system:0
	;;
	-h|-help)
		echo ${system::name} version ${system::version}
		echo ${system::copyright}
		echo 'Usage: ngraph [ options ] [ data | NGP | PRM ]'
		echo 'valid options:'
		echo ' -x column -y column     data column'
		echo ' -X -Y -U -R             axis'
		echo ' -d type                 plot type'
		echo ' -m type -o size         mark type and size'
		echo ' -l style -w width       line style and width'
		echo ' -c R G B -C R G B       plot color (1st and 2nd)'
		echo ' -s line                 head skip'
		echo ' -r step                 read step'
		echo ' -f line                 finale line'
		echo ' -vx num -vy num         neighbour average'
		echo ' -mx math -my math       math transformation'
		echo ' -ex type -ey type       scale type'
		echo ' -zx min max inc         axis settings'
		echo ' -zy min max inc         axis settings'
		echo ' -g                      load settings from data file'
		echo ' -n                      output ngp-file to stdout'
		del system:0
	;;
	*)
		new file file:$1
		file::x=$XCOLUMN
		file::y=$YCOLUMN
		file::type=$PLOTTYPE
		file::mark_type=$MARKTYPE
		file::mark_size=$MARKSIZE
		file::line_style=$LINESTYLE
		file::line_width=$LINEWIDTH
		file::R=$COLORR
		file::G=$COLORG
		file::B=$COLORB
		file::R2=$COLORR2
		file::G2=$COLORG2
		file::B2=$COLORB2
		file::head_skip=$HEADSKIP
		file::read_step=$READSTEP
		file::final_line=$FINALLINE
		file::smooth_x=$AVERAGEX
		file::smooth_y=$AVERAGEY
		file::math_x=$MATHX
		file::math_y=$MATHY
		file::axis_x=axis:$XAXIS
		file::axis_y=axis:$YAXIS
		if [ $LOADSETTINGS = 1 ]; then
			file::load_settings
		fi
	esac
	shift
done
