#! ngraph
# Ngraph initialization script

usage() {
    echo ${system::name} version ${system::version}
    echo ${system::copyright}
    echo 'Usage: ngraph [ options ] [ data | NGP ]'
    echo 'valid options:'
    echo ' -L ngp_file             load ngp file'
    echo ' -x column -y column     data column'
    echo ' -X -Y -U -R             axis'
    echo ' -d type                 plot type'
    echo ' -m type                 mark type (0 - 89)'
    echo ' -o size                 mark size'
    echo ' -l style -w width       line style and width'
#   echo ' -c R G B -C R G B       plot color (1st and 2nd)'	# obsolete
    echo ' -CR r -CG g -CB b       1st plot color'
    echo ' -cr r -cg g -cb b       2nd plot color'
    echo ' -s line                 head skip'
    echo ' -r step                 read step'
    echo ' -f line                 finale line'
    echo ' -vx num -vy num         neighbor average'
    echo ' -mx math -my math       math transformation'
    echo ' -ex type -ey type       scale type'
#   echo ' -zx min max inc         axis settings'		# obsolete
#   echo ' -zy min max inc         axis settings'		# obsolete
    echo ' -minx minimum           set minimum value of x axis'
    echo ' -maxx maximum           set maximum value of x axis'
    echo ' -incx increment         set increment value of x axis'
    echo ' -miny minimum           set minimum value of y axis'
    echo ' -maxy maximum           set maximum value of y axis'
    echo ' -incy increment         set increment value of y axis'
    echo ' -g                      load settings from data file'
    echo ' -png ngp_file           save png file'
    echo ' -pdf ngp_file           save pdf file'
    echo ' -ps  ngp_file           save ps file'
    echo ' -eps ngp_file           save eps file'
    echo ' -svg ngp_file           save svg file'
    echo ' -gra ngp_file           save gra file'
    echo ' -dialog                 show print dialog (with -p option)'
    echo ' -p ngp_file             print graph'
    echo ' -n                      output ngp-file to stdout'
    echo ' -h, --help              Show this help message'
    echo ' -v, --version           Show version of Ngraph'
    echo ' -V, --VERSION           Show detail information about Ngraph'
    del system:0
}

check_file() {
    if [ -z "$1" ]
    then
	echo "${system::name}: missing file operand"
	del system:0
    elif [ ! -f "$1" ]
    then
	echo "${system::name}: can't find the file $1."
	del system:0
    fi
}

clear_viewer() {
    for i in `derive -instance draw`
    do
	del ${i}:0-!
    done
    if exist -q gra:viewer
    then
	del gra:viewer
    fi
}

copy_gra_settings() {
    if exist -q gra:viewer
    then
	cpy gra:viewer,$1 draw_obj left_margin top_margin zoom paper_width paper_height decimalsign
    fi
}

save_image() {
    check_file "$1"
    clear_viewer
    . "$1"
    new gra2cairofile name=OUTPUT
    gra2cairofile::format=$2
    gra2cairofile::file=$3
    gra::device=gra2cairofile:OUTPUT
    gra::open
    gra::draw
    gra::close
    del gra
    del gra2cairofile
}

check_set() {
    if [ -n "$3" ]
    then
	VAL=$3
	if [ -n "$4" ]
	then
	    VAL=$4:$3
	fi
	put $1: $2=$VAL
	unset VAL
    fi
}

load_graph() {
    check_file "$1"
    path=`dirname "$1"`
    if [ -d "$path" ]
    then
	cd "$path"
    fi
    name=`pwd`/`basename "$1"`
    if [ -n "$LOGIN_SHELL" ]
    then
	system:0:login_shell="$LOGIN_SHELL $name"
    else
	. "$name"
    fi
    unset path name
}

IGNOREEOF=0

if new menu
then
    LOGIN_SHELL="menu:0:menu"
    system:0:login_shell="$LOGIN_SHELL"
fi
#
if [ -f "${NGRAPHHOME}/Ngraph.ngp" ]; then
    . "${NGRAPHHOME}/Ngraph.ngp"
elif [ -f "${NGRAPHCONF}/Ngraph.ngp" ]; then
    . "${NGRAPHCONF}/Ngraph.ngp"
fi
#
LOADSETTINGS=0
SHOW_DIALOG=0
XAXIS=0
YAXIS=1
SAVE_NGP=0
NULL_DEVICE=/dev/null
#
while [ "$1" ]
do
    case $1 in
	*.ngp|*.NGP)
	    load_graph "$1"
	    break
	    ;;
	*.gra|*.GRA)
	    for drawable in `derive -instance draw`
	    do
		if [ $drawable != "merge" ]
		then
		    del $drawable:0-!
		fi
	    done
	    new merge file:"$1"
	    ;;
	-L)
	    shift
	    load_graph "$1"
	    break
	    ;;
	-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"
	    ;;
	-cr)
	    shift
	    COLORR="$1"
	    ;;
	-cg)
	    shift
	    COLORG="$1"
	    ;;
	-cb)
	    shift
	    COLORB="$1"
	    ;;
	-CR)
	    shift
	    COLORR2="$1"
	    ;;
	-CG)
	    shift
	    COLORG2="$1"
	    ;;
	-CB)
	    shift
	    COLORB2="$1"
	    ;;
	-c)
	    echo "warning: option $1 is obsolete"
	    echo '         use -cr -cg and -cb option'
	    shift
	    COLORR="$1"
	    shift
	    COLORG="$1"
	    shift
	    COLORB="$1"
	    ;;
	-C)
	    echo "warning: option $1 is obsolete"
	    echo '         use -CR -CG and -CB option'
	    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
	    put axis:$XAXIS type="$1"
	    ;;
	-ey)
	    shift
	    put axis:$YAXIS type="$1"
	    ;;
	-minx)
	    shift
	    put axis:$XAXIS min="$1"
	    ;;
	-maxx)
	    shift
	    put axis:$XAXIS max="$1"
	    ;;
	-incx)
	    shift
	    put axis:$XAXIS inc="$1"
	    ;;
	-miny)
	    shift
	    put axis:$YAXIS min="$1"
	    ;;
	-maxy)
	    shift
	    put axis:$YAXIS max="$1"
	    ;;
	-incy)
	    shift
	    put axis:$YAXIS inc="$1"
	    ;;
	-zx)
	    echo "warning: option $1 is obsolete"
	    echo '         use -minx -maxx and -incx option'
	    shift
	    put axis:$XAXIS min="$1"
	    shift
	    put axis:$XAXIS max="$1"
	    shift
	    put axis:$XAXIS inc="$1"
	    ;;
	-zy)
	    echo "warning: option $1 is obsolete"
	    echo '         use -miny -maxy and -incy option'
	    shift
	    put axis:$YAXIS min="$1"
	    shift
	    put axis:$YAXIS max="$1"
	    shift
	    put axis:$YAXIS inc="$1"
	    ;;
	-g)
	    LOADSETTINGS=1
	    ;;
	-n)
	    SAVE_NGP=1
	    ;;
	-p)
	    shift
	    check_file "$1"
	    clear_viewer
	    . "$1"
	    menu::print false $SHOW_DIALOG
	    del system:0
	    ;;
	-dialog)
	    SHOW_DIALOG=2
	    ;;
	-png)
	    save_image "$2" png `basename $2 .ngp`.png
	    del system:0
	    ;;
	-ps)
	    save_image "$2" ps3 `basename $2 .ngp`.ps
	    del system:0
	    ;;
	-eps)
	    save_image "$2" eps3 `basename $2 .ngp`.eps
	    del system:0
	    ;;
	-pdf)
	    save_image "$2" pdf `basename $2 .ngp`.pdf
	    del system:0
	    ;;
	-svg)
	    save_image "$2" svg1.1 `basename $2 .ngp`.svg
	    del system:0
	    ;;
	-gra)
	    check_file $2
	    clear_viewer
	    . $2

	    new gra2cairofile file=$NULL_DEVICE
	    new gra name=DUMMY
	    gra:DUMMY:device=gra2cairofile:0
	    gra:DUMMY:open

	    new gra2file name=OUTPUT
	    new gra name=OUTPUT
	    copy_gra_settings OUTPUT
	    gra2file::file=`basename $2 .ngp`.gra
	    gra:OUTPUT:device=gra2file:OUTPUT
	    gra:OUTPUT:open
	    gra:OUTPUT:draw
	    gra:OUTPUT:close
	    del gra:OUTPUT
	    del gra2file:OUTPUT

	    gra:DUMMY:close
	    del gra:DUMMY
	    del gra2cairofile
	    del system:0
	    ;;
	-h|-help|--help)
	    usage
	    ;;
	-v|-version|--version)
	    echo ${system::name} version ${system::version}
	    echo ${system::copyright}
	    del system:0
	    ;;
	-V|-VERSION|--VERSION)
	    echo ${system::name} version ${system::version}
	    echo ${system::copyright}
	    echo
	    echo "  compiler:"
	    echo "    ${system::compiler}"
	    if exist -q menu
	    then
		echo
		echo "  library:"
		get menu: -field lib_version:4
	    fi
	    del system:0
	    ;;
	-*)
	    echo invalid option "$1"
	    echo
	    usage
	    ;;
	*)
	    check_file "$1"
	    new file file:"$1"
	    check_set file x $XCOLUMN
	    check_set file y $YCOLUMN
	    check_set file type $PLOTTYPE
	    check_set file mark_type $MARKTYPE
	    check_set file mark_size $MARKSIZE
	    check_set file line_style $LINESTYLE
	    check_set file line_width $LINEWIDTH
	    check_set file R $COLORR
	    check_set file G $COLORG
	    check_set file B $COLORB
	    check_set file R2 $COLORR2
	    check_set file G2 $COLORG2
	    check_set file B2 $COLORB2
	    check_set file head_skip $HEADSKIP
	    check_set file read_step $READSTEP
	    check_set file final_line $FINALLINE
	    check_set file smooth_x $AVERAGEX
	    check_set file smooth_y $AVERAGEY
	    check_set file math_x $MATHX
	    check_set file math_y $MATHY
	    check_set file axis_x "$XAXIS" axis
	    check_set file axis_y "$YAXIS" axis
	    if [ $LOADSETTINGS = 1 ]; then
		file::load_settings
	    fi
    esac
    shift
done

if [ $SAVE_NGP = 1 ]; then
    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
fi

pwd=`pwd`
cd "$NGRAPHCONF"
new shell name=init_d
for nsc in init.d/[0-9][0-9]*.nsc
do
    shell:init_d:shell "$nsc"
done
del shell:init_d
cd "$pwd"

unset usage check_file clear_viewer save_image check_set load_graph LOADSETTINGS SHOW_DIALOG XAXIS YAXIS SAVE_NGP NULL_DEVICE pwd
