CAE_TOOLS = /home/qi/cae-tools
CAMEO = $(CAE_TOOLS)/cameo
SPOOL = $(CAE_TOOLS)/spool/spool
CNGT = $(CAE_TOOLS)/cngt/cngt
GP2RML = $(CAE_TOOLS)/gp2rml/gp2rml

# #1:  5,  5
# #2:  5, 45
# #3: 65,  5
# #3: 70, 45
# Z1 = -44
# PIECE_Z = 5.5

# new run (10 45, 135 85, 7.8)
# #1: 13 45
# #2: 75 48
# Z1 = -44
# PIECE_Z = 7.9

# run with acrylic (corner at 4, 0)
# #1: 15, 10
# #2: 15, 50
# #3: 80, 10
# PIECE_Z = 5.15

X0 = 80
Y0 = 10
Z1 = -45
PIECE = -5 -5 30 55
PIECE_Z = 5.15
Z_STEP = 2
CLEARANCE = 2
SPEED_XY = 1
SPEED_Z = 1
OVERSHOOT = 0.2

Z_OFFSET = 0.1
FIN_SPEED_XY = 3
FIN_SPEED_Z = 3

# acrylic
# ~0.1 mm / 12 mil tool correction # not needed
#MILL = 120mil
SPEED_XY = 2
SPEED_Z = 2
FIN_SPEED_XY = 5
FIN_SPEED_Z = 5

PARAMS = X0=$(X0)mm Y0=$(Y0)mm Z1=$(Z1)mm MILL=$(MILL) PIECE_Z=$(PIECE_Z)mm \
	 Z_OFFSET=$(Z_OFFSET)mm

PARTS = top mid bot
PART = top

.PHONY:		all plot plot-fin mill pos cng clean

all:		$(PART)-mill.rml $(PART)-finish.rml

case.gp:	case.fpd
		fped -g case.fpd || { rm -f $@; exit 1; }

$(PART).gp:	case.gp
		$(CAMEO)/fped2d2z.pl -r 0=-$(OVERSHOOT) $(PART) $^ >$@ || \
		    { rm -f $@; exit 1; }

$(PART)-piece.gp: $(PART).gp
		./genpiece.pl -r $(PIECE) $< >$@ || { rm -f $@; exit 1; }

$(PART)-area.gp: $(PART)-piece.gp $(PART).gp cam.sh Makefile
		./cam.sh PART=$(PART) CAMEO=$(CAMEO)/cameo $(PARAMS) OUT=$@ || \
		    { rm -f $@; exit 1; }

$(PART)-finish.gp: $(PART)-piece.gp $(PART).gp cam.sh Makefile
		./cam.sh PART=$(PART) CAMEO=$(CAMEO)/cameo $(PARAMS) \
		    Z_OFFSET=0mm REVERSE=reverse OUT=$@ || { rm -f $@; exit 1; }

$(PART)-mill.gp: $(PART)-area.gp Makefile
		$(CAMEO)/zstack.pl $(Z1) $(Z_STEP) $< >$@ || \
		    { rm -f $@; exit 1; }

$(PART)-mill.rml: $(PART)-mill.gp Makefile
		$(GP2RML) $(CLEARANCE) $(SPEED_XY) $(SPEED_Z) $< >$@ || \
		    { rm -f $@; exit 1; }

$(PART)-finish.rml: $(PART)-finish.gp Makefile
		$(GP2RML) $(CLEARANCE) $(FIN_SPEED_XY) $(FIN_SPEED_Z) \
		    $< >$@ || { rm -f $@; exit 1; }

plot:		$(PART)-mill.gp
		echo 'splot "$<" with lines' | gnuplot -persist

plot-fin:	$(PART)-finish.gp
		echo 'splot "$<" with lines' | gnuplot -persist

mill:		$(PART)-mill.rml $(PART)-finish.rml
		PORT=/dev/ttyUSB0 $(SPOOL) $^

pos:
		$(CNGT) 0

cng:		$(PART)-mill.gp
		$(CNGT) $(Z1) 10 $<

clean:
		rm -f case.gp
		rm -f $(PARTS:%=%-piece.gp) $(PARTS:%=%.gp)
		rm -f $(PARTS:%=%-area.gp)
		rm -f $(PARTS:%=%-mill.gp)
		rm -f $(PARTS:%=%-finish.gp)

spotless:	clean
		rm -f $(PARTS:%=%-mill.rml)
		rm -f $(PARTS:%=%-finish.rml)
