# 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 c2esp18a has myopldecode compiled but no longer installed
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/ #new in ver08
INSTALL=install

# 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

# I think modtime is not used 15/8/11
# Definition of modtime()
#MODTIME= date -d "1/1/1970 utc + `stat -t $$1 | cut -f14 -d' '` seconds" "+%a %b %d %T %Y"
#ifeq ($(UNAME),FreeBSD)
#    MODTIME= stat -f "%Sm" -t "%a %b %d %T %Y" $$1
#endif
#ifeq ($(UNAME),OpenBSD)
#    MODTIME= stat -f "%Sm" -t "%a %b %d %T %Y" $$1
#endif
#ifeq ($(UNAME),Darwin)
#    MODTIME= stat -f "%Sm" -t "%a %b %d %T %Y" $$1
#endif
#ifeq ($(UNAME),SunOS)
#    MODTIME= `ls -e $$1 | cut -c42-61`
#endif



# 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
FILTERS=	c2esp command2esp
#SHELLS=
MANPAGES=
LIBJBG	=	jbig.o jbig_ar.o
LIBJBG85=	jbig85.o jbig_ar.o
BINPROGS=

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...
	#
	@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/sidechannel.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 
	@echo "The debug tool myopldecode is compiled, but not installed, put PROGS=myopldecode into the makefile if you want  to install it."

#linkable stuff from ddk
#CUPSDRVLIBS	=	-lcupsimage -lcups -lgssapi_krb5 -lgnutls -lz -lpthread -lm -lcrypt
#CUPSDRVLIBS_ST	=	/usr/lib/libcupsimage.a -ltiff -ljpeg -lpng /usr/lib/libcups.a -lgssapi_krb5 -lgnutls -lz -lpthread -lm -lcrypt
#CUPSLIBS	=	-lcups -lgssapi_krb5 -lgnutls -lz -lpthread -lm -lcrypt
#CUPSLIBS_ST	=	/usr/lib/libcups.a -lgssapi_krb5 -lgnutls -lz -lpthread -lm -lcrypt

#c2esptst: c2esptst.o $(LIBJBG)
#	$(CC) $(CFLAGS) -o $@ c2esptst.o -lcupsimage -lcups -lcupsdriver $(LIBJBG)

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

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

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

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

	# Now use your printer configuration GUI to create a new printer.
	# The debug tool myopldecode is compiled, but not installed, put PROGS=myopldecode into the makefile if you want  to install it.
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

	$(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 \
	$(INSTALL) -c -m 644 $$ppdfile $(PPDDIR); \
	    done;


cups:	
	#
	# restart cups
	#
	if [ -x /etc/init.d/cups ]; then \
	    /etc/init.d/cups restart; \
	elif [ -x /etc/rc.d/rc.cups ]; then \
	    /etc/rc.d/rc.cups restart; \
	elif [ -x /etc/init.d/cupsys ]; then \
	    /etc/init.d/cupsys restart; \
	elif [ -x /etc/init.d/cupsd ]; then \
	    /etc/init.d/cupsd restart; \
	elif [ -x /usr/local/etc/rc.d/cups.sh ]; then \
	    /usr/local/etc/rc.d/cups.sh restart; \
	elif [ -x /usr/local/etc/rc.d/cups.sh.sample ]; then \
	    cp /usr/local/etc/rc.d/cups.sh.sample /usr/local/etc/rc.d/cups.sh; \
	    /usr/local/etc/rc.d/cups.sh restart; \
	fi

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
README: README.in
	rm -f $@
	cp $@.in $@
# changed 15/8/11 these URLs no longer used?
#	sed < $@.in > $@ \
#	    -e "s@\$${URLOAK}@$(URLOAK)@" \
#	    -e "s@\$${URLZJS}@$(URLZJS)@"
	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 > $@
# changed 15/8/11 these URLs no longer used?
#	cat $@.tmp $@.in | sed > $@ \
#	    -e "s@\$${URLOAK}@$(URLOAK)@" \
#	    -e "s@\$${URLZJS}@$(URLZJS)@"
	rm -f $@.tmp
	chmod -w $@

#
# Uninstall
#
uninstall:
#	-rm -f $(MANDIR)/man1/arm2hpdl.1 $(MANDIR)/man1/usb_printerid.1
#	-rm -rf /usr/share/ppd/c2esp/
	-rm -rf $(PPDDIR)
	-rm -f $(BIN)/myopldecode
#	-rm -f $(BIN)/c2esptst
#	-rm -f $(FILTERBIN)/c2esptst
	-rm -f $(BIN)/c2esp
	-rm -f $(FILTERBIN)/c2esp
#	done
# is next bit needed??
	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 index.html
	-rm -f *.tar.gz
#	-rm -f getweb
#	-rm -f patch.db
#	-rm -f $(MANPAGES) manual.pdf
#	-rm -f *.zjs *.zm *.zc *.zc? *.zc?? *.oak *.pbm *.pksm *.cmyk
#	-rm -f pksm2bitcmyk
#	-rm -f *.icm.*.ps
#	cd icc2ps; $(MAKE) $@

#
# Header dependencies
#
jbig.o: jbig.h
jbig85.o: jbig85.h
#c2esptst.o: jbig85.h
c2esp.o: jbig85.h 
myopldecode.o: jbig.h

#
#	PPD files
#
ppd:
	#
	# Generate PPD files using local tools
	#

