#
# Makefile for Seiko Instruments USA Inc. Smart Label Printer Driver
#
# Original work Copyright (C) Easy Software Products
# Modifications Copyright (C) 2026 by Seiko Instruments Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

mfdir     := $(shell pwd)
program   := seikoslp.rastertolabel
ppddir    := $(DESTDIR)/usr/share/cups/model
filterdir := $(DESTDIR)/usr/lib/cups/filter

PKGCONF	  := $(shell command -v pkg-config 2>dev/null)
CXXFLAGS  += -g
LDFLAGS   :=
LDLIBS    :=
ifeq ($(PKGCONF),)
	CXXFLAGS  += $(shell cups-config --cflags)
	LDFLAGS   += $(shell cups-config --ldflags)
	LDLIBS    += $(shell cups-config --image --libs)
else
	CXXFLAGS  += $(shell pkg-config --cflags)
	LDFLAGS   += $(shell pkg-config --ldflags)
	LDLIBS    += $(shell pkg-config --image --libs)
endif

build:
	make clean
	$(CXX) *.cxx -o $(program) $(LDFLAGS) $(CXXFLAGS) $(LDLIBS)
	# set up the filter directory in the ppd correctly.
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp440.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp450.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp620.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp650.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp850.ppd	

install:
	make build
	mkdir -p "$(filterdir)"
	mkdir -p "$(ppddir)"
	install -m 755 $(program) "$(filterdir)/"
	cp *.ppd "$(ppddir)"

uninstall:
	rm -f "$(ppddir)"/siislp*.ppd
	rm -rfv "$(filterdir)/$(program)"

clean:	
	# set up the filter directory in the ppd correctly
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp440.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp450.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp620.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp650.ppd
	perl -p -i -e 's(^.cupsFilter.*\Z) <*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/$(program)">g' siislp850.ppd
	rm -f $(program) *.o *~
	rm -rf "$(mfdir)/pretty"

archive:
	make clean
	rm -f "$(mfdir)/../SeikoSLPLinuxDriver.zip"
	ditto -c -k --keepParent "$(mfdir)" "$(mfdir)/../SeikoSLPLinuxDriver.zip"


pretty:
	# pretty up the source code files using bcpp.  (C++ compatible pretty printer.)
	make clean
	mkdir "$(mfdir)/pretty"
	mv *.h "$(mfdir)/pretty/"
	mv *.cxx "$(mfdir)/pretty/"
	bcpp "$(mfdir)/pretty/SIISLPProcessBitmap.cxx" "$(mfdir)/SIISLPProcessBitmap.cxx"
	bcpp "$(mfdir)/pretty/SeikoSLPCommands.h" "$(mfdir)/SeikoSLPCommands.h"
	bcpp "$(mfdir)/pretty/SIISLPProcessBitmap.h" "$(mfdir)/SIISLPProcessBitmap.h"
	bcpp "$(mfdir)/pretty/RasterToSIISLP.h" "$(mfdir)/RasterToSIISLP.h"
	bcpp "$(mfdir)/pretty/DriverUtils.h" "$(mfdir)/DriverUtils.h"
	bcpp "$(mfdir)/pretty/RasterToSIISLP.cxx" "$(mfdir)/RasterToSIISLP.cxx"
	bcpp "$(mfdir)/pretty/DriverUtils.cxx" "$(mfdir)/DriverUtils.cxx"
	bcpp "$(mfdir)/pretty/SeikoInstrumentsVendorID.h" "$(mfdir)/SeikoInstrumentsVendorID.h"
	rm -rf "$(mfdir)/pretty"
	
