# 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

LANG=C
UNAME := $(shell uname)
MACH := $(shell uname -m | sed 's/i.86/x86_32/')

ifeq ($(UNAME),SunOS)
    EUID := $(shell /usr/xpg4/bin/id -u)
    SHELL=bash
    CC=gcc
    OLDGROFF=OLDGROFF
else
    EUID := $(shell id -u)
endif

SYSNAME := $(shell uname -n)

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

# Pathnames for this package...
BIN=$(PREFIX)/bin
FILTERBIN=$(PREFIX)/lib/cups/filter
MANDIR=$(PREFIX)/share/man
DOCDIR=$(PREFIX)/share/doc/c2esp/
PPDDIR=$(PREFIX)/share/ppd/c2esp/
INSTALL=install

# Programs and libraries
PROGS=
SCRIPTS= c2esplog
# put PROGS= myopldecode if you want to install this debug tool
#PROGS=		myopldecode
# DEVPROGS are compiled but not installed
DEVPROGS=	myopldecode
FILTERS=	c2esp c2espC command2esp
#SHELLS=
MANPAGES=
LIBJBG	=	jbig.o jbig_ar.o
LIBJBG85=	jbig85.o jbig_ar.o
BINPROGS=

# 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 +=	-g #to make debug info


#
# The usual build rules
#

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

all-test:
	#
	# Compile Dependencies...
	#
#	@declare EXITCODE = 0;
	@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
	@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/cups/image.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/cups/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/cups/driver.h; then \
	    echo "      ***"; \
	    echo "      *** Error: /usr/include/cups/driver.h is not installed!"; \
	    echo "      ***"; \
	    echo "      *** Install cups driver package"; \
	    echo "      *** for Ubuntu: sudo apt-get install libcupsdriver1-dev"; \
	    echo "      ***"; \
	    exit 1; \
	fi

	
	# ... OK!
	#

all-done:
	@echo
	@echo "It is possible that certain products which can be built using this"
	@echo "software module might form inventions protected by patent rights in"
	@echo "some countries (e.g., by patents about arithmetic coding algorithms"
	@echo "owned by IBM and AT&T in the USA). Provision of this software by the"
	@echo "author does NOT include any licences for any patents. In those"
	@echo "countries where a patent licence is required for certain applications"
	@echo "of this software module, you will have to obtain such a licence"
	@echo "yourself."
	@echo 

c2esp: c2esp.o c2espcommon.o $(LIBJBG85)
	$(CC) $(CFLAGS) -o $@ c2esp.o c2espcommon.o -lcupsimage -lcups -lcupsdriver $(LIBJBG85)

c2espC: c2espC.o c2espcommon.o 
	$(CC) $(CFLAGS) -o $@ c2espC.o c2espcommon.o -lcupsimage -lcups -lcupsdriver -lz

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

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

#
# Installation rules
#
install: all install-test install-prog install-ppd install-doc

	# Now use your printer configuration GUI to create a new printer.
	#
	# ESP Cxxx and Hero SUPPORT NOW INCLUDED FOR MONO & COLOUR, 300DPI & 600DPI
	# Note that the ESP Cxxx and Hero support is a new feature. Please report
	# how it works / does not work 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

	#

install-test:
	#
	# Installation Dependencies...
	#
	# none as yet
	# ... OK!
	#
    

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) $(SHELLS) $(FILTERBIN)/

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

MODEL=$(DESTDIR)/usr/share/cups/model
#LOCALMODEL=$(DESTDIR)/usr/local/share/cups/model
PPD=$(DESTDIR)/usr/share/ppd
VARPPD=/var/lp/ppd
install-ppd:
	#
	# Install PPD files for CUPS
	#
	$(INSTALL) -d -m 755 $(PPDDIR)
	    cd ppd; \
	    for ppdfile in *.ppd; do echo $$ppdfile; \
	    $(INSTALL) -c -m 644 $$ppdfile $(PPDDIR); \
	    done;

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

doc: README INSTALL 

install-doc: doc
	#
	# Install documentation
	#
	$(INSTALL) -d -m 755 $(DOCDIR)
	$(INSTALL) -c -m 644 COPYING $(DOCDIR)
	$(INSTALL) -c -m 644 INSTALL $(DOCDIR)
	$(INSTALL) -c -m 644 README $(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 -f $(MANDIR)/man1/arm2hpdl.1 $(MANDIR)/man1/usb_printerid.1
	-rm -rf $(PPDDIR)
	-rm -f $(BIN)/myopldecode
	-rm -f $(BIN)/c2esp
#	-rm -f $(BIN)/cupslog
	-rm -f $(BIN)/c2esplog
	-rm -f $(FILTERBIN)/c2esp
	-rm -f $(FILTERBIN)/c2espC
#	done
#remove any compessed ppds
	cd ppd; for ppdfile in *.ppd; do \
	    rm -f $(MODEL)/$$ppdfile.gz; \
	done;

#
# Clean
#
clean:
	-rm -f $(PROGS) $(BINPROGS) $(SHELLS) $(FILTERS) $(DEVPROGS)
	-rm -f *.o
	-rm -f *~
	-rm -f ppd/*~
	-rm -f *.tar.gz
#	-rm -f $(MANPAGES) manual.pdf

#
# Header dependencies
#
jbig.o: jbig.h
jbig85.o: jbig85.h
c2esp.o: jbig85.h c2espcommon.h
#c2espC.o: zlib.h 
myopldecode.o: jbig.h
command2esp.o: c2espcommon.h

#
#	PPD files
#
ppd: KodakESP_10.drv KodakESP_C_05.drv
	#
	# Generate PPD files using local tools
	#
	ppdc KodakESP_10.drv
	ppdc KodakESP_C_05.drv
