# makefile for c2esp

# Makefile for c2esp09 has -lcupsimage instead of /usr/lib/libcupsimage.so.2 (intention: to make sucessfully in non ubuntu distros)
# Makefile for c2esp13 has -lcups  (intention: to make sucessfully in Fedora)
# Makefile for c2esp19 has myopldecode compiled but no longer installed
# Makefile for c2esp20 has some redundant stuff deleted, ppd file names are echoed, c2espC added
# Makefile for c2esp21 has forceppd added
# Makefile for c2esp25 has minor changes. (*.in files are deleted from the doc directory)
# Makefile for c2esp25c runs ppdc at install stage if it is installed
# Makefile for c2esp26 uses cups-config to fix problems building with cups 1.6
# Makefile for c2esp27 ppds for ESP 1.2 and 3.2 added, use new drv files instead of ppd,

LANG=C
UNAME := $(shell uname)

# Installation prefix...
#PREFIX=/usr/local
PREFIX=$(DESTDIR)/usr

PPDCFOUND=0

# Pathnames for this package...
BIN=$(PREFIX)/bin

#the PREFIX is supplied by cups-config --serverbin
FILTERBIN := $(DESTDIR)$(shell cups-config --serverbin)/filter

MANDIR=$(PREFIX)/share/man
DOCDIR=$(PREFIX)/share/doc/c2esp
PPDDIR=$(PREFIX)/share/ppd/c2esp
DRVDIR=$(DESTDIR)$(shell cups-config --datadir)/drv/c2esp
INSTALL=install

# Programs and libraries
PROGS=
# put PROGS= myopldecode if you want to install this debug tool
#PROGS=		myopldecode
# DEVPROGS are compiled but not installed
DEVPROGS=	myopldecode
SCRIPTS= c2esplog
FILTERS=	c2esp c2espC command2esp
#LIBJBIG	=	jbig.o jbig_ar.o
#LIBJBIG85=	jbig85.o jbig_ar.o
# The jbig library. Debian and derivatives should now have the jbig library -ljbig
# -ljbig also contains jbig85
	LIBJBIG := -ljbig
	LIBJBIG85 := -ljbig

BINPROGS=
ESPDRVFILE = KodakESP_16.drv
CDRVFILE = KodakESP_C_07.drv

# User ID's
LPuid=-oroot
LPgid=-glp
ifeq ($(UNAME),Darwin)
    LPuid=-oroot
    LPgid=-gwheel
endif
ifeq ($(UNAME),FreeBSD)
    LPuid=-oroot
    LPgid=-gwheel
endif
ifeq ($(UNAME),OpenBSD)
    LPuid=-oroot
    LPgid=-gwheel
endif
ifeq ($(UNAME),SunOS)
    LPuid=-oroot
    LPgid=-glp
    INSTALL=/usr/ucb/install
endif
# If we aren't root, don't try to set ownership
ifneq ($(EUID),0)
    LPuid=
    LPgid=
endif

ifeq ($(UNAME),Linux)
#	BINPROGS += usb_printerid
endif

# Compiler flags
CFLAGS +=	-O2 -Wall
#CFLAGS +=	-m32 -O2 -Wall # to produce 32-bit binaries on 64-bit OS
#CFLAGS +=	-g #to make debug info

#cups version, checking that cups-config exists
ifeq ($(wildcard /usr/bin/cups-config),) 
#cups-config not found

	CUPSAPIVER := 99
	LIBCUPSDRIVER := -lcups -lcupsdriver
	LIBCUPSIMAGE := -lcups -lcupsimage
else
#cups-config found
	CUPSAPIVER := $(shell cups-config --api-version)
	LIBCUPSIMAGE := $(shell cups-config --image --libs)
endif

ifeq ($(CUPSAPIVER),1.6)
	LIBCUPSDRIVER := -lcups -lcupsfilters
	FILTERHEADERS := cupsfilters
else
	LIBCUPSDRIVER := -lcups -lcupsdriver
	FILTERHEADERS := cups
endif


#
# The usual build rules
#

all:	common-test cups-test $(FILTERS) $(PROGS) $(DEVPROGS) $(BINPROGS) all-done

common-test:
	#
	# cups-config...
	#
ifeq ($(CUPSAPIVER),99)
	   @echo "      ***"; \
	   echo "      *** Warning: /usr/bin/cups-config is not installed"; \
	   echo "      ***"; \
	   echo "      *** Try to install it in the package libcups2-dev"; \
	   echo "      *** for Ubuntu: sudo apt-get install libcups2-dev"; \
	   echo "      *** Then run make again."; \
	   echo "      ***"; \
	   echo "      *** If you cannot install cups-config, make will assume"; \
	   echo "      *** that your cups version is 1.5 or lower."; \
	   echo "      ***";
endif
	#
	# Compile Dependencies general...
	#

	@if ! type $(CC) >/dev/null 2>&1; then \
	    echo "      ***"; \
	    echo "      *** Error: $(CC) is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install Software Development (gcc) package"; \
	    echo "      ***"; \
	    exit 1; \
	fi
	@if ! test -f /usr/include/stdio.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/stdio.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install Software Development (gcc) package"; \
	    echo "      *** for Ubuntu: sudo apt-get install build-essential"; \
	    echo "      ***"; \
	    exit 1; \
	fi

cups-test:
	# Compile Dependencies in cups...
ifeq ($(CUPSAPIVER),99)
	@echo "The cups api version is unknown, assuming <=1.5"
else
	@echo "cups api detected $(CUPSAPIVER)"
endif

ifeq ($(CUPSAPIVER),1.6)
	@echo "The cups tests for version 1.6"
	@echo look in /usr/include/cupsfilters for image and driver

# make config.h
	echo "#define HAVE_CUPSFILTERS 1" >config.h

	@if ! test -f /usr/include/cups/raster.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/cups/raster.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups raster library package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcupsimage2-dev"; \
	    echo "      ***"; \
	    exit 1; \
	fi
	@if ! test -f /usr/include/$(FILTERHEADERS)/image.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/$(FILTERHEADERS)/image.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups-filters package"; \
	    echo "      *** for Ubuntu: something like sudo apt-get install ??(libcupsfilters-dev)"; \
	    echo "      ***"; \
	    exit 1; \
        fi
	@if ! test -f /usr/include/cups/sidechannel.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/cups/sidechannel.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups development package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcups2-dev"; \
	    echo "      ***"; \
	    exit 1; \
	fi
	@if ! test -f /usr/include/$(FILTERHEADERS)/driver.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/$(FILTERHEADERS)/driver.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups-filters package"; \
	    echo "      *** for Ubuntu: something like sudo apt-get install ??(libcupsfilters-dev)"; \
	    echo "      ***"; \
	    exit 1; \
	fi

else
	@echo "The cups tests for version up to 1.5"
	@echo should look in /usr/include/cups/ for image and driver

# make config.h
	echo "#define HAVE_CUPSFILTERS 0" >config.h

	@if ! test -f /usr/include/cups/raster.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/cups/raster.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups raster library package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcupsimage2-dev"; \
	    echo "      ***"; \
	    exit 1; \
	fi
	@if ! test -f /usr/include/$(FILTERHEADERS)/image.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/$(FILTERHEADERS)/image.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups raster library package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcupsimage2-dev"; \
	    echo "      ***"; \
	    exit 1; \
        fi
	@if ! test -f /usr/include/cups/sidechannel.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/cups/sidechannel.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups development package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcups2-dev"; \
	    echo "      ***"; \
	    exit 1; \
	fi
	@if ! test -f /usr/include/$(FILTERHEADERS)/driver.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/$(FILTERHEADERS)/driver.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups driver package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcupsdriver1-dev"; \
	    echo "      ***"; \
	    exit 1; \
	fi

endif

	# ... OK!
	#

all-done:
	@echo
	@echo "Patent warning that used to be here no longer applies"
	@echo 

#c2esp: c2esp.o c2espcommon.o
#	$(CC) $(CFLAGS) -o $@ c2esp.o c2espcommon.o $(LIBCUPSDRIVER) $(LIBCUPSIMAGE) $(LIBJBG85)

c2esp: c2esp.o c2espcommon.o
	$(CC) $(CFLAGS) -o $@ c2esp.o c2espcommon.o $(LIBCUPSDRIVER) $(LIBCUPSIMAGE) $(LIBJBIG85)

c2espC: c2espC.o c2espcommon.o 
	$(CC) $(CFLAGS) -o $@ c2espC.o c2espcommon.o $(LIBCUPSDRIVER) $(LIBCUPSIMAGE) -lz

command2esp: command2esp.o c2espcommon.o
	$(CC) $(CFLAGS) -o $@ command2esp.o  c2espcommon.o $(LIBCUPSDRIVER)

#myopldecode: myopldecode.o
#	$(CC) $(CFLAGS) -o $@ $@.o $(LIBJBG) -lz

myopldecode: myopldecode.o
	$(CC) $(CFLAGS) -o $@ $@.o $(LIBJBIG) -lz

#
# Installation rules
#
#use next line if you need to install ppds
#install: all install-prog install-ppd install-doc
install: all install-prog install-drv install-doc
 
	#
	# Now use your printer configuration GUI to create a new printer if required.
	# If you are upgrading, "make forceppd" may force cups to use the new ppd versions
	# for your existing print queues.
	#
	# Please report problems in the help forum.
	# http://sourceforge.net/projects/cupsdriverkodak/forums
	#

	@if [ "$(PROGS)" = "" ]; then \
		echo "the development tool myopldecode was not installed"; \
	fi
	@if [ "$(PROGS)" = "myopldecode" ]; then \
		echo "the development tool myopldecode was installed"; \
	fi
    

UDEVBIN=$(DESTDIR)/bin/

install-prog:
	#
	# Install driver, and development tools
	#

	if [ "$(PROGS)" != "" ]; then \
		$(INSTALL) -d $(BIN); \
		$(INSTALL) -c $(PROGS) $(BIN); \
	fi

	if [ "$(SCRIPTS)" != "" ]; then \
		$(INSTALL) -d $(BIN); \
		$(INSTALL) -c $(SCRIPTS) $(BIN); \
	fi
	$(INSTALL) -d $(FILTERBIN)
	$(INSTALL) -c $(FILTERS) $(FILTERBIN)/

	if [ "$(BINPROGS)" != "" ]; then \
	    $(INSTALL) -d $(UDEVBIN); \
	    $(INSTALL) -c $(BINPROGS) $(UDEVBIN); \
	fi

MODEL=$(DESTDIR)$(shell cups-config --datadir)/model
#PPD=$(DESTDIR)/usr/share/ppd
#VARPPD=/var/lp/ppd

install-ppd:
	#
	# Generate PPD files using ppdc tool of currently installed CUPS version
	# run ppdc if it is present
	@if test -f /usr/bin/ppdc; then \
	    echo "ppdc is available so running it"; \
		mkdir -p ppd; \
		ppdc $(ESPDRVFILE); \
		ppdc $(CDRVFILE); \
	fi

	#
	# work around a bug in CUPS ppdc 1.4.x for "8x10" format by post-editing PPD files
	#
	for i in ppd/*.ppd ; do \
	    mv $$i $$i.orig ; \
	    sed '1,$$s/8 x 10"/8x10/g' $$i.orig > $$i ; \
	done
	rm ppd/*.orig

	#
	# Install PPD files for CUPS
	#
	$(INSTALL) -d -m 755 $(PPDDIR)
	rm -f $(PPDDIR)/*.ppd
	    cd ppd; \
	    for ppdfile in *.ppd; do \
	        echo $$ppdfile; \
	    $(INSTALL) -c -m 644 $$ppdfile $(PPDDIR); \
	    done;

install-drv:
	# make files with ppdc if it is present
	@if test -f /usr/bin/ppdc; then \
	    echo "ppdc is available so testing drv files"; \
		ppdc -t $(ESPDRVFILE); \
		ppdc -t $(CDRVFILE); \
	    echo "Making ppds to keep them compatible with drv files."; \
		ppdc $(ESPDRVFILE); \
		ppdc $(CDRVFILE); \
	fi

	$(INSTALL) -d -m 755 $(DRVDIR)
	$(INSTALL) -c -m 644 $(ESPDRVFILE) $(DRVDIR)
	$(INSTALL) -c -m 644 $(CDRVFILE) $(DRVDIR)

forceppd:
	# attempt to assign new ppds to existing queues
	./c2esp.postinst configure

doc: README INSTALL 

install-doc: doc
	#
	# Install documentation
	#
	rm -f $(DOCDIR)/INSTALL.in
	rm -f $(DOCDIR)/README.in
	$(INSTALL) -d -m 755 $(DOCDIR)
	$(INSTALL) -c -m 644 COPYING $(DOCDIR)
	$(INSTALL) -c -m 644 INSTALL $(DOCDIR)
	$(INSTALL) -c -m 644 README $(DOCDIR)
	$(INSTALL) -c -m 644 c2esp.txt $(DOCDIR)

# make README file from README.in just copy and make read only
README: README.in
	rm -f $@
	cp $@.in $@
	chmod -w $@

# make INSTALL file from INSTALL.in
# adds a contents section to the start, using the capitalised subject headings.
INSTALL: INSTALL.in Makefile
#INSTALL: INSTALL.in
	rm -f $@
	echo "TOPICS" > $@.tmp
	echo "------" >> $@.tmp
	grep ^[A-Z][A-Z] $@.in | sed "s/^/    * /" >> $@.tmp
	echo >> $@.tmp
	cat $@.tmp $@.in > $@
	rm -f $@.tmp
	chmod -w $@

#
# Uninstall
#
uninstall:
	-rm -rf $(PPDDIR)
	-rm -rf $(DRVDIR)
	-rm -f $(BIN)/myopldecode
	-rm -f $(BIN)/c2esp
	-rm -f $(BIN)/c2esplog
	-rm -f $(FILTERBIN)/c2esp
	-rm -f $(FILTERBIN)/c2espC
#	done
#remove any compressed ppds. Is this a real thing? or is MODEL something I made by mistake in an early version?
	cd ppd; for ppdfile in *.ppd; do \
	    rm -f $(MODEL)/$$ppdfile.gz; \
	done;

#
# Clean
#
clean:
	-rm -f $(PROGS) $(BINPROGS) $(FILTERS) $(DEVPROGS)
	-rm -f *.o
	-rm -f *~
# Do not remove ppds, so that users without ppdc can install them
#	-rm -f ppd/*
	-rm -f *.tar.gz
	-rm -f config.h

#
# Header dependencies
#
c2esp.o: c2espcommon.h
c2espC.o: c2espcommon.h
command2esp.o: c2espcommon.h
c2espcommon.o: c2espcommon.h

