# based on Makefile.2003-08-15.dsm with additions by Julian H. Stacey

#  
#  Copyright (c) 2003, David S. Madole
#  All rights reserved.
#  
#  For latest sources and information on this software, please
#  go to http://www.madole.net/scanjet
#  
#  Questions, comments or suggestions should be directed to
#  David S. Madole <david@madole.net>.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright
#     notice unmodified, this list of conditions, and the following
#     disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
# 
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.
#  

#
#  Revision History:
#
#  2003-07-19  Initial release. (DSM)
#

prefix = /usr/local

PREFIX = $(prefix)
BINDIR = $(PREFIX)/bin
ETCDIR = $(PREFIX)/etc
INCDIR = $(PREFIX)/include
LIBDIR = $(PREFIX)/lib
RCDDIR = $(ETCDIR)/rc.d

CC ?= cc
CFLAGS ?= -O

all:	sjlcd sjrun sjctl pbm2lj tiffres tiffrotate sjrun.conf.default \
	sjrun.lang.default

sjlcd:	sjlcd.c
	$(CC) $(CFLAGS) -o sjlcd ${.CURDIR}/./sjlcd.c $(CFLAGS) $(LDFLAGS)

sjctl:	sjctl.sh
	sed "s:%BINDIR%:$(BINDIR):g" ${.CURDIR}/./sjctl.sh >sjctl

sjrun:	sjrun.sh
	sed "s:%ETCDIR%:$(ETCDIR):g" ${.CURDIR}/./sjrun.sh >sjrun

pbm2lj:	pbm2lj.c
	$(CC) $(CFLAGS) -o pbm2lj ${.CURDIR}/./pbm2lj.c $(CFLAGS) $(LDFLAGS)

tiffres: tiffres.c
	$(CC) $(CFLAGS) -o tiffres ${.CURDIR}/./tiffres.c $(CFLAGS) $(LDFLAGS)

tiffrotate: tiffrotate.c
	$(CC) $(CFLAGS) -o tiffrotate -I$(INCDIR) -L/$(LIBDIR) \
	      ${.CURDIR}/./tiffrotate.c -ltiff

sjrun.conf.default: sjrun.sh
	sed	-e '1,2d' \
		-e '/End of copyright/,/Start of configuration/d' \
		-e '/End of configuration/,$$d' \
		-e '/^[^#]/s/^/#/' \
		-e 's:%ETCDIR%:$(ETCDIR):g' \
		${.CURDIR}/./sjrun.sh >sjrun.conf.default

sjrun.lang.default: sjrun.sh
	sed	-e '1,2d' \
		-e '/End of copyright/,/Start of translation/d' \
		-e '/End of translation/,$$d' \
		-e /^[^#]/s/^/#/ \
		-e 's:%ETCDIR%:$(ETCDIR):g' \
		${.CURDIR}/./sjrun.sh >sjrun.lang.default

install: all
	install -d -o root -g wheel -m 0755 $(DESTDIR)$(BINDIR)
	install -c -s -o root -g wheel -m 0755 sjlcd $(DESTDIR)$(BINDIR)
	install -c -s -o root -g wheel -m 0755 pbm2lj $(DESTDIR)$(BINDIR)
	install -c -s -o root -g wheel -m 0755 tiffres $(DESTDIR)$(BINDIR)
	install -c -s -o root -g wheel -m 0755 tiffrotate $(DESTDIR)$(BINDIR)
	install -c -o root -g wheel -m 0755 sjrun $(DESTDIR)$(BINDIR)
	install -c -o root -g wheel -m 0755 sjctl $(DESTDIR)$(BINDIR)
	install -d -o root -g wheel -m 0755 $(DESTDIR)$(RCDDIR)
	ln -sf $(DESTDIR)$(BINDIR)/sjctl $(DESTDIR)$(RCDDIR)/sjrun.sh
	install -d -o root -g wheel -m 0755 $(DESTDIR)$(ETCDIR)
	install -c -o root -g wheel -m 0644 sjrun.conf.default \
		$(DESTDIR)$(ETCDIR)
	install -c -o root -g wheel -m 0644 sjrun.lang.default \
		$(DESTDIR)$(ETCDIR)

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/sjlcd
	rm -f $(DESTDIR)$(BINDIR)/sjrun
	rm -f $(DESTDIR)$(BINDIR)/sjctl
	rm -f $(DESTDIR)$(BINDIR)/pbm2lj
	rm -f $(DESTDIR)$(BINDIR)/tiffres
	rm -f $(DESTDIR)$(BINDIR)/tiffrotate
	rm -f $(DESTDIR)$(RCDDIR)/sjrun.sh
	rmdir -p $(RCDDIR)
	rm -f $(DESTDIR)$(ETCDIR)/sjrun.conf.default
	rm -f $(DESTDIR)$(ETCDIR)/sjrun.lang.default
	rmdir -p $(ETCDIR)

clean:
	rm -f sjlcd sjrun sjctl pbm2lj tiffres tiffrotate
	rm -f sjrun.conf.default sjrun.lang.default


# The next include is to support bsd style make obj.
# The "${.CURDIR}/./" above support obj.
.include <bsd.prog.mk>

# .include <berklix.mk>

