# Requires BSD make.  Your mileage may vary.
#
# Note: this distribution is aimed for UCB/FAMSF use.  You
# need to look into all the scripts and change them to suite
# your site's needs.
#
# Define these if you are with the Tertiary Disk Project (translation: don't)
#EXTRA_FLAGS+= -DUCB
#UCB= yes
#GRIDROOT= ${WWWROOT}/data/famsf/gridpix/
# Define this to non-zero to enable resizing buttons
RESIZE=1
# Define this to non-zero to enable subdirectory support
SUBDIR=0
# Directory prefix
PREFIX?= /usr/local
# The root of your www tree
WWWROOT= ${PREFIX}/www
# The subdirectory of ${WWWROOT} that gridpix files reside in
GRIDSUBDIR=	gridpix
# The root of all gridpix files -- don't forget the trailing slash!
GRIDROOT= ${WWWROOT}/data/${GRIDSUBDIR}/
# Full pathname to perl
PERL=	/usr/bin/perl
# Version of this software
VERSION= 2.1
# Address of contact person
MAINTAINER=	\"please@change.this\"

.if defined(UCB)
MAINTAINER=	\"guestbook@www.famsf.org\"
.endif
EXTRA_FLAGS+=	-DMAINTAINER=${MAINTAINER}

.if defined(RESIZE) && ${RESIZE} != 0
EXTRA_FLAGS+= -DRESIZE
.else
RESIZE=0
.endif
.if defined(SUBDIR) && ${SUBDIR} != 0
EXTRA_FLAGS+= -DSUBDIR
.else
SUBDIR=0
.endif

CGIS=	mkhtml.cgi gpxinfo.cgi main.cgi subdir.cgi gpxhelp.cgi getimg2.cgi
IMAGES= about-h.png about.png dir-h.png dir.png down.png \
	enlargeh-h.png enlargeh.png enlargev-h.png enlargev.png \
	help-h.png help.png nodir.png nodown.png noshrinkh.png noshrinkv.png \
	nozoomout.png shrinkh-h.png shrinkh.png shrinkv-h.png shrinkv.png \
	zoomout-h.png zoomout.png
SCROLLBARS= downarrow-h.png downarrow.png downarrows-h.png downarrows.png \
	hbard.png hbardl.png hbardll.png hbardr.png hbardrr.png \
	hbarl-h.png hbarl.png \
	leftarrow-h.png leftarrow.png leftarrows-h.png leftarrows.png \
	rightarrow-h.png rightarrow.png rightarrows-h.png rightarrows.png \
	uparrow-h.png uparrow.png uparrows-h.png uparrows.png \
	vbard.png vbardb.png vbardbb.png vbardt.png vbardtt.png \
	vbarl-h.png vbarl.png
DOCS=	help2.html

# Standard rules
CFLAGS=	-O -pipe -Wall -I/usr/X11R6/include -I${PREFIX}/include ${EXTRA_FLAGS} -DGRIDROOT=\"${GRIDROOT}\" -DGRIDSUBDIR=\"${GRIDSUBDIR}\"
LDFLAGS= -static -O
LDLIBS=	-L/usr/X11R6/lib -lppm -lpgm -lpbm -L${PREFIX}/lib -ljpeg -lm

PROGS=	gridpack gridunpack mkhtml gpxinfo
OBJS=	${PROGS:S/$/.o/} utils.c

all:	${PROGS}
${OBJS}:	gridpix.h utils.h Makefile
gridpack.o: gridpack.c
mkhtml.o: mkhtml.c
gpxinfo.o: gpxinfo.c
gridunpack.o: gridunpack.c

gridpack: gridpack.o utils.o
	${CC} ${LDFLAGS} $> -o $@ ${LDLIBS}

gridunpack: gridunpack.o utils.o
	${CC} ${LDFLAGS} $> -o $@ ${LDLIBS}

mkhtml: mkhtml.o utils.o
	${CC} ${LDFLAGS} $> -o $@ ${LDLIBS}

gpxinfo: gpxinfo.o utils.o
	${CC} ${LDFLAGS} $> -o $@ ${LDLIBS}

install:
	mkdir -p ${GRIDROOT}/docs
	install -c docs/help2.html ${GRIDROOT}/docs
	for i in gridpack gridunpack gpxinfo mkhtml; do \
		rm -f ${PREFIX}/bin/$$i; \
		install -c -m 555 $$i ${PREFIX}/bin; \
	done
	for i in ${CGIS}; do \
		rm -f ${WWWROOT}/cgi-bin/$$i; \
		sed -e "s^%%GRIDROOT%%^${GRIDROOT}^" \
			-e "s/%%RESIZE%%/${RESIZE}/" \
			-e "s/%%SUBDIR%%/${SUBDIR}/" \
			-e "s^%%PERL%%^${PERL}^" $$i > /tmp/$$i; \
		install -c -m 555 /tmp/$$i ${WWWROOT}/cgi-bin/$$i; \
		rm -f /tmp/$$i; \
	done
	ln -sf ${PREFIX}/bin/gridunpack ${WWWROOT}/cgi-bin/gettile.cgi
#	for i in about.html info.html gridpix.html; do \
#		rm -f ${WWWROOT}/data/$$i; \
#		install -c -m 444 $$i ${WWWROOT}/data; \
#	done
	mkdir -p ${WWWROOT}/data/images/sb
.for image in ${IMAGES}
	cp -p images/${image} ${WWWROOT}/data/images
.endfor
	mkdir -p ${WWWROOT}/data/images/sb
.for sb in ${SCROLLBARS}
	cp -p images/sb/${sb} ${WWWROOT}/data/images/sb
.endfor
.if defined(UCB)
	cp -p docs/help.html ${GRIDROOT}/docs
	for i in getimg.cgi; do \
		rm -f ${WWWROOT}/cgi-bin/$$i; \
		sed -e "s^%%GRIDROOT%%^${GRIDROOT}^" \
			-e "s/%%RESIZE%%/${RESIZE}/" \
			-e "s/%%SUBDIR%%/${SUBDIR}/" \
			-e "s^%%PERL%%^${PERL}^" $$i > /tmp/$$i; \
		install -c -m 555 /tmp/$$i ${WWWROOT}/cgi-bin/$$i; \
		rm -f /tmp/$$i; \
	done
	cd museum/images; sh install.sh
.endif

clean:
	rm -fr gridpack gridunpack mkhtml gpxinfo *.o gridpix-${VERSION}.tar.gz

tarball:
	rm -rf GridPix-${VERSION}
	mkdir GridPix-${VERSION}
	cp -p COPYRIGHT README CHANGELOG Makefile GridPix-${VERSION}
	cp -p gpxinfo.c gridpack.c gridunpack.c mkhtml.c utils.c \
		gridpix.h utils.h ${CGIS} \
		GridPix-${VERSION}
	mkdir -p GridPix-${VERSION}/images
.for image in ${IMAGES}
	cp -p images/${image} GridPix-${VERSION}/images
.endfor
	mkdir -p GridPix-${VERSION}/images/sb
.for sb in ${SCROLLBARS}
	cp -p images/sb/${sb} GridPix-${VERSION}/images/sb
.endfor
	mkdir -p GridPix-${VERSION}/docs
.for doc in ${DOCS}
	cp -p docs/${doc} GridPix-${VERSION}/docs
.endfor
	tar cvzf GridPix-${VERSION}.tar.gz GridPix-${VERSION}
	rm -rf GridPix-${VERSION}
