#!/bin/bash

inpsvg="$1"
#sf=`echo '72/25.4' | bc -l`
#sf=`echo 1.0`
#sf=`echo '72/254' | bc -l`
sf='1'

if [[ "$2" != "" ]] ; then
  sf="$2"
fi

premul=`echo "1000000" | bc -l`
invmul=`echo "( 1 / $premul ) * $sf" | bc -l`

frapid="F8000"
fslow="F400"
S="1.0"

if [[ "$inpsvg" == "" ]] ; then
  echo "provide input svg"
  exit 1
fi

rawtype=`file $inpsvg`
checktype=`file -b $inpsvg | cut -f1 -d' '`
if [[ "$checktype" != "SVG" ]] ; then
  echo -e "file $inpsvg is of type:\n\n$rawtype\n\nNnot an SVG file? Exiting.\n"
  exit 1
fi

bn=`basename $inpsvg .svg`

# causes duplicate paths otherwise
#
sed -i 's/fill="[^"]*"/fill="none"/g' $inpsvg

echo "creating $bn.ps"
rsvg-convert -f ps -o $bn.ps $inpsvg

pstoedit -f gnuplot $bn.ps $bn.gp

echo "# premul: $premul" > ${bn}-ord.gp
#clipcli -s $bn.gp -F -x $premul -T >> ${bn}-ord.gp
clipcli -S evenodd -s $bn.gp -F -x $premul -T >> ${bn}-ord.gp

sfx_slow="$fslow S$S"
sfx_rapid="$frapid S0"

#echo gp2ngc -i ${bn}-ord.gp -s "$invmul" --sfx-rapid "$sfx_rapid" --sfx-slow "$sfx_slow" -o ${bn}.ngc
#gp2ngc -i ${bn}-ord.gp --sfx-rapid "$sfx_rapid" --sfx-slow "$sfx_slow" | ngc_scale -s "$invmul" > ${bn}.ngc

echo gp2ngc --preset laser -i ${bn}-ord.gp --rapid "$sfx_rapid" --slow "$sfx_slow" "|" ngc_scale -s "$invmul" "> ${bn}.ngc"
gp2ngc --preset laser -i ${bn}-ord.gp --rapid "$sfx_rapid" --slow "$sfx_slow" | ngc_scale -s "$invmul" > ${bn}.ngc

