#!/bin/sh
#
# Convert SVG to DXF, then tell dxf2gcode to generate gcode for it.

set -e

SVG=/usr/share/icons/hicolor/scalable/apps/dxf2gcode.svg

# Ensure X is unavailable, the alternative is to use xvfb-run, but can
# cause dxf2gcode to hang on a dialog.
DISPLAY=
export DISPLAY

# Convert random SVG to test.dxf file
echo info: Converting SVG to EPS using inkscape
if ! inkscape $SVG -o $AUTOPKGTEST_TMP/test.eps 2>&1 ; then
    echo error: inkscape failed to convert SVG to EPS
    exit 1
fi
echo info: Converting EPS to DXF using pstoedit
if ! pstoedit -f dxf_s $AUTOPKGTEST_TMP/test.eps $AUTOPKGTEST_TMP/test.dxf 2>&1 ; then
    echo error: pstoedit failed to convert EPS to DXF
    exit 1
fi

# Convert test.dxf to test.ngc
echo info: Converting DXF to G-code using dxf2gcode
if ! dxf2gcode -platform  offscreen -q -e $AUTOPKGTEST_TMP/test $AUTOPKGTEST_TMP/test.dxf 2>&1| tee $AUTOPKGTEST_TMP/dxf2gcode.log ; then
    echo error: dxf2gcode returned error code when converting DXF to G-code
fi

if grep 'Export to FILE was successful' $AUTOPKGTEST_TMP/dxf2gcode.log &&
   [ -f $AUTOPKGTEST_TMP/test.ngc ]; then
    echo success: DXF to G-code conversion succeeded.
    exit 0
else
    echo error: dxf2gcode failed to convert DXF to G-code
    exit 1
fi
