###########################################################################
# LPRngTool - An Configuration Tool For
#      LPRng - An Extended Print Spooler System
#
# See LICENSE for conditions of use.
#
###########################################################################
# MODULE: Makefile.in
# PURPOSE: top level Makefile for LPRngTool Software
##########################################################################

@SET_MAKE@

####### General configure settings

VERSION=@VERSION@
PACKAGE=@PACKAGE@


OSNAME=@OSNAME@
OSVERSION=@OSVERSION@

INSTALL=@INSTALL@
INSTALL_MAN=@INSTALL_MAN@

SHELL=@SHELL@
GROUPID=@GROUPID@
USERID=@USERID@

WISH=@WISH@

SRC=@srcdir@
# prefix
prefix=@prefix@
# exec prefix
exec_prefix=@exec_prefix@
# for binaries
bindir=@bindir@
# for admin type of binaries
sbindir=@sbindir@
# for the ghostscrip tools
datadir=@datadir@
# for the filters
libexecdir=@libexecdir@
# for the configuration stuff
sysconfdir=@sysconfdir@
# for the man pages
mandir=@mandir@


FILTER_DIR=@FILTER_DIR@
SPOOL_DIR=@SPOOL_DIR@
IFHP_PATH=@IFHP_PATH@
IFHP_CONF=@IFHP_CONF@
PRINTCAP_PATH=@PRINTCAP_PATH@
FILEUTIL=@FILEUTIL@
GZIP=@GZIP@
GS=@GS@
A2PS=@A2PS@
MPAGE=@MPAGE@
SMBCLIENT=@SMBCLIENT@
LPRNGTOOL_CONF=@LPRNGTOOL_CONF@
GSUPDIR=@GSUPDIR@
PAGER=@PAGER@
NPRINT=@NPRINT@
PAP=@PAP@


# what C compiler to use.
CC:=@CC@
# what command to use to "ranlib" or "lorder" the library.
RANLIB=@RANLIB@

# what C preprocessor to use.
CPP:=@CPP@
CPPFLAGS=@CPPFLAGS@

CFLAGS:=@CFLAGS@ -I. -I${SRC}/src

LDCC=@LDCC@
LDFLAGS=@LDFLAGS@



###  END OF CONFIGURE EDITS ###

SCRIPTS= postinstall.bsdi postinstall.freebsd postinstall.generic postinstall.linux postinstall.solaris
PROGS=lprngtool lprngtool.conf
FILTERS= atalkprint ncpprint smbprint
FILTERDATA = printerdb testpage.asc testpage.ps testpage-a4.ps

all: $(PROGS) $(FILTERS) $(FILTERDATA) ./postinstall
.PHONEY: all clean configure dist distclean install send tar update

$(SCRIPTS): header
	cat header ${SRC}/$@.sh >$@
	chmod 755 $@;

header: Makefile
	@echo "#!@SHELL@" >$@
	@echo PREFIX=$(prefix) >>$@
	@echo SYSCONFDIR=$(sysconfdir) >>$@
	@echo LPRNGTOOL_CONF=$(LPRNGTOOL_CONF) >>$@
	@echo MANDIR=$(mandir) >>$@
	@echo "VERSION=$(VERSION)" >>$@

# we set up the postinstall to be used
./postinstall: $(SCRIPTS) Makefile
	OSNAME=@OSNAME@; case "@OSNAME@" in \
	 *linux* ) OSNAME=linux;; \
	 *freebsd* ) OSNAME=freebsd;; \
	 *solaris* ) OSNAME=solaris;; \
	esac; \
	if test -f postinstall.$${OSNAME} ; then s=postinstall.$${OSNAME} ; else s=postinstall.generic; fi; \
	echo POSTINSTALL "'$$s'"; cp $$s postinstall;


.SUFFIXES: .script

lprngtool: lprngtool.script Makefile
lprngtool.conf: lprngtool.conf.script Makefile

.script:
	sed  \
	-e 's,[@]GROUPID@,${GROUPID},g' \
	-e 's,[@]PRINTCAP_PATH@,${PRINTCAP_PATH},g' \
	-e 's,[@]FILTER_DIR@,${FILTER_DIR},g' \
	-e 's,[@]SPOOL_DIR@,${SPOOL_DIR},g' \
	-e 's,[@]USERID@,${USERID},g' \
	-e 's,[@]VERSION@,${VERSION},g' \
	-e 's,[@]WISH@,${WISH},g' \
	-e 's,[@]PACKAGE@,${PACKAGE},g' \
	-e 's,[@]VERSION@,${VERSION},g' \
	-e 's,[@]FILEUTIL@,${FILEUTIL},g' \
	-e 's,[@]GS@,${GS},g' \
	-e 's,[@]GZIP@,${GZIP},g' \
	-e 's,[@]A2PS@,${A2PS},g' \
	-e 's,[@]MPAGE@,${MPAGE},g' \
	-e 's,[@]SMBCLIENT@,${SMBCLIENT},g' \
	-e 's,[@]IFHP_PATH@,${IFHP_PATH},g' \
	-e 's,[@]IFHP_CONF@,${IFHP_CONF},g' \
	-e 's,[@]LPRNGTOOL_CONF@,${LPRNGTOOL_CONF},g' \
	-e 's,[@]GSUPDIR@,${GSUPDIR},g' \
	-e 's,[@]PAGER@,${PAGER},g' \
	-e 's,[@]NPRINT@,${NPRINT},g' \
	-e 's,[@]PAP@,${PAP},g' \
	$< >$@.edit
	if grep '#!.*sh' $@.edit >/dev/null ; then \
		chmod 755 $@.edit; \
	fi
	mv $@.edit $@

testpage.ps:
	fig2dev -Lps -zLetter testpage.fig testpage.ps

testpage-a4.ps:
	fig2dev -Lps -zA4 testpage-a4.fig testpage-a4.ps

install: all
	${SRC}/mkinstalldirs ${DESTDIR}${bindir}
	${SRC}/mkinstalldirs ${DESTDIR}${mandir}/man1
	${SRC}/mkinstalldirs ${DESTDIR}$(FILTER_DIR)

	# install lprngtool in ${DESTDIR}${bindir}
	${INSTALL} -c -m 755 lprngtool ${DESTDIR}${bindir}/lprngtool
	${INSTALL} -c -m 644 lprngtool.1 ${DESTDIR}${mandir}/man1/lprngtool.1
	if [ "${POSTINSTALL}" != "NO" ] ; then \
		MAKEINSTALL=YES DESTDIR=$(DESTDIR) $(SHELL) postinstall ; \
	fi;

# should only really copy MINIMUM filters, rest should come from RPMs
	for i in $(FILTERS) ; do \
		${INSTALL} -m 755 $$i ${DESTDIR}$(FILTER_DIR); \
	done

	for i in $(FILTERDATA) ; do \
		${INSTALL} -m 644 $$i ${DESTDIR}$(FILTER_DIR); \
	done


clean:
	rm -f $(PROGS) *.a *.o core errs a.out rewindstdin *.E \#* TAGS errors *~
	rm -f $(SCRIPTS) postinstall ltconfig header lprngtool lprngtool.conf *.edit

distclean: clean
	rm -f config.cache config.h config.log config.status Makefile
	rm -f DISTRIBUTIONS/FreeBSD*/work DISTRIBUTIONS/FreeBSD*/*.tgz ;

configure: configure.in
	autoconf
	autoheader

# use this to make a distribution
tar: distclean configure
	DIR=`pwd | sed 's,.*/,,'`; \
		echo RCS >X; \
		echo X >>X; \
		cd ..; \
		tar zXcf $${DIR}/X $${DIR}.tgz $${DIR}; \
		rm -f $${DIR}/X; \
		md5 $${DIR}.tgz | pgp -fast -u papowell@lprng > $${DIR}.tgz.md5

UPDATE=README configure.in lprngtool.conf.script lprngtool.script \
	DISTRIBUTIONS/*/Makefile DISTRIBUTIONS/*/*.spec

# use this to update the version numbers in files
update:
	if [ -f Makefile ] ; then ci -l Makefile; fi
	DIR=`pwd | sed 's,.*/,,'`; \
	DATE=`date`; VER=`pwd | sed -e 's,.*/,,' -e s/.*-// ` ; echo $${VER} $${DATE}; \
	echo $${DIR} >VERSION; \
	perl -spi -e "\
		s,^DISTNAME=.*,DISTNAME=$${DIR},; \
		s,^PORTNAME=.*,PORTNAME=$(PACKAGE),; \
		s,^PORTVERSION=.*,PORTVERSION=$${VER},; \
		s,package name \".*\",package name \"$${DIR}\",; \
	 if( /^VERSION[:=]/ ){ s/([:=]).*/\$${1}$${VER}/ }  \
	 if( /\s*#*\s*Version\s*[:=]/ ){ s/([:=]).*/\$${1} $${VER}/ }  \
	 if( /\s*#*\s*Last Modified\s*[:=]/ ){ s/([:=]).*/\$${1} $${DATE}/ } \
	" ${UPDATE}

dist: update tar send

send:
	if [ -f ../SENDTO ] ; then cd ..; sh SENDTO ; fi

