#!/bin/sh
# there are a bunch of parameters that you might need to tweak to make this work well for your
# style of input.
# You'll also have to install and build a few things...  sorry about needing both imagemagick and graphicsmagick!
# sudo apt-get install -y imagemagick
# sudo apt-get install -y graphicsmagick
# cc -o ras2vec -Wall -Wno-unused-but-set-variable src/*.c -lm # 2>/dev/null
# ( cd hp2xx-3.4.4 ; make )
# ( cd cld/C ; make )
# ( cd svg2mx3 ; make )
#
# test this with:   ./img2svg steve-jobs-biography.png
#
if [ ! -f $1 ] ; then
    echo syntax: img2svg imagefile.ext
    exit 1
fi
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
if [ -f ${filename}.svg ] ; then
    if [ -f ${filename}.nc ] ; then
        >&2 echo This will overwrite ${filename}.svg and ${filename}.nc - remove them first if that is what you want.
        exit 1
    fi
    >&2 echo This will overwrite ${filename}.svg - remove it first if that is what you want.
    exit 1
fi
if [ -f ${filename}.nc ] ; then
    >&2 echo This will overwrite ${filename}.nc - remove it first if that is what you want.
    exit 1
fi
>&2 echo This may take a minute or two.  Sit back and relax.
gm convert -bordercolor white -scale "150%" $1 /tmp/$$.jpg
# convert the image to a line drawing in raster format
./cld/C/draw /tmp/$$.jpg | convert -bordercolor black -border 10x10 - -| convert -negate -depth 1 - /tmp/$$.png
gm convert /tmp/$$.png /tmp/$$.tif
# ras2vec doesn't have svg output, but we can use HPGL and convert it to svg
./ras2vec -d -e 1 -h /tmp/$$.tif && mv $$.hgl /tmp/
./hp2xx-3.4.4/sources/hp2xx -m svg /tmp/$$.hgl
mv /tmp/$$.svg ${filename}.svg
# and convert the svg to G-CODE
./svg2mx3/svg2mx3 -w 150 ${filename}.svg ${filename}.nc
rm /tmp/$$.*
ls -l ${filename}.nc
