#! /usr/bin/ngraph -i
# $Id: ngp2.in,v 1.15 2010-02-18 06:38:49 hito Exp $

IGN_PATH=0
AUTO_SCALE=0
OPTION="-gra"
CHDIR=0
TRIM=0
COMMAND=""
PWD=`pwd`
NULL_DEVICE=/dev/null
DPI=86

trim_image() {
    if [ -f "$2" ]
    then
	echo trimming $2...
	trimmed="$2_trim.$1"
	case $1 in
            png)
		convert -trim "$2" "$trimmed"
		;;
            pdf)
		pdfcrop "$2" "$trimmed"
		;;
            svg*)
		inkscape -D --export-overwrite "$2"
		;;
	esac
	if [ -f "$trimmed" ]
	then
	    mv "$trimmed" "$2"
	fi
    fi
}

clear_viewer() {
    for i in `derive -instance draw fit parameter`
    do
	del ${i}:0-!
    done

    if exist -q gra:viewer
    then
	del gra:viewer
    fi
}

save_image() {
    new gra2cairofile name=OUTPUT
    new gra name=OUTPUT
    gra2cairofile:OUTPUT:format=$1
    gra2cairofile:OUTPUT:file=$2
    gra2cairofile:OUTPUT:dpi=$DPI
    gra:OUTPUT:device=gra2cairofile:OUTPUT

    if exist -q gra:viewer
    then
	cpy gra:viewer,OUTPUT draw_obj left_margin top_margin zoom paper_width paper_height decimalsign
    fi

    if exist -q fit:0
    then
	fit:0-!:display=false
    fi

    gra:OUTPUT:open
    gra:OUTPUT:draw
    gra:OUTPUT:close
    del gra:OUTPUT
    del gra2cairofile:OUTPUT

    if [ $TRIM -ne 0 ]
    then
	trim_image "$1" "$2"
    fi
}

save_gra() {
    if [ -n "$1" ]
    then
	new gra2cairofile file=$NULL_DEVICE
	new gra name=DUMMY
	gra:DUMMY:device=gra2cairofile:0
	gra:DUMMY:open

	new gra2file name=OUTPUT file="$1"
	new gra name=OUTPUT

	if exist -q gra:viewer
	then
	    cpy gra:viewer,OUTPUT draw_obj left_margin top_margin zoom paper_width paper_height decimalsign
	fi

	if exist -q fit:0
	then
	    fit:0-!:display=false
	fi

	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
    fi
}

show_usage() {
    echo "ngp2 version 1.10"
    echo "usage: ngp2 [option] ngpfile ..."
    echo
    echo "    -I            ignore path"
    echo "    -a            auto scale"
    echo "    -A            clear scale and auto scale"
    echo "    -c            change directory"
    echo "    -C command    execute command before drawing"
    echo "    -d dpi        set dot per inch"
    echo "    -t            trimming the image (PDF, PNG or SVG)"
    echo
    echo "    -ps, -ps3     output PostScript level 3"
    echo "    -ps2          output PostScript level 2"
    echo "    -eps, -eps3   output Encapsulated PostScript level 3"
    echo "    -eps2         output Encapsulated PostScript level 2"
    echo "    -pdf          output Portable Document Format"
    echo "    -svg, -svg1.1 output Scalable Vector Graphics version 1.1"
    echo "    -svg1.2       output Scalable Vector Graphics version 1.2"
    echo "    -png          output Portable Network Graphics"
    echo "    -h, --help    show this usage"
    echo

    del system
}

set +e

new menu

while true
do
    case "$1" in
	-I)
	    IGN_PATH=1
	    shift
	    ;;
	-a)
	    AUTO_SCALE=1
	    shift
	    ;;
	-A)
	    AUTO_SCALE=2
	    shift
	    ;;
	-c)
	    CHDIR=1
	    shift
	    ;;
        -C)
            shift
            COMMAND="$1"
            shift
            ;;
	-d)
	    shift
	    DPI=$1
	    shift
	    ;;
        -t)
            TRIM=1
            shift
            ;;
	-h|--help)
	    show_usage
	    ;;
	-*)
	    OPTION="$1"
	    shift
	    ;;
	*)
	    break
	    ;;
    esac
done

NGP_FILES=$@
if [ -z "$NGP_FILES" ]
then
    show_usage
fi

for NGP_FILE in $NGP_FILES
do
    clear_viewer

    if [ -f "$NGP_FILE" ]
    then
	NGP_NAME=`basename "$NGP_FILE" .ngp`
	PATH_NAME=`dirname $NGP_FILE`

	if exist -q menu:0
	then
	    menu::fullpath_ngp="$NGP_FILE"
	fi

	case "$OPTION" in
	    -)
		;;
	    *)
		echo "Drawing ${NGP_FILE}."
		;;
	esac

	new shell
	shell::set_security
	shell::shell "$NGP_FILE"
	del shell

        if [ x"$COMMAND" != "x" ]
        then
            eval $COMMAND
        fi

	if [ $IGN_PATH -ne 0 ]
	then
	    for i in `object file -instances`
	    do
		put file:$i file=`get file:$i -field basename`
	    done
	fi

	if [ $CHDIR -ne 0 ]
	then
	    cd $PATH_NAME
	fi

	if exist -q axis:0
	then
	    if exist -q file:0
	    then
		case "$AUTO_SCALE" in
		    2)
			axis:0-!:clear
			axis:0-!:auto_scale:'file:0-!'
			;;
		    1)
			axis:0-!:auto_scale:'file:0-!'
			;;
		esac
	    fi
	fi

	TMPFILE=${system:0:temp_file}

	case "$OPTION" in
#======================================================================
# options
#
# $TMPFILE : temporary file (automatically deleted).
# $NGP_NAME : base name of ngp file.

	    -ps|-ps3)
		save_image "ps3" $NGP_NAME.ps
		;;
	    -ps2)
		save_image "ps2" $NGP_NAME.ps
		;;
	    -eps|-eps3)
		save_image "eps3" $NGP_NAME.eps
		;;
	    -eps2)
		save_image "eps2" $NGP_NAME.eps
		;;
	    -pdf)
		save_image "pdf" $NGP_NAME.pdf
		;;
	    -svg|svg1.1)
		save_image "svg1.1" $NGP_NAME.svg
		;;
	    -svg1.2)
		save_image "svg1.2" $NGP_NAME.svg
		;;
	    -png)
		save_image "png" $NGP_NAME.png
		;;
	    -)
		save_gra $TMPFILE
		cat $TMPFILE
		;;
	    *)
		save_gra "$NGP_NAME.gra"
		;;
#======================================================================
	esac
	system:0:unlink_temp_file

	if [ $CHDIR -ne 0 ]
	then
	    cd $PWD
	fi
    fi
done

echo

if exist -q menu:0
then
    del menu:0
fi

del system:0
exit
