DESTDIR?=/
PREFIX?=/usr/local
LOCALBASE?=/usr/local/

VER_MAJOR=6
VER_MINOR=6

PORTNAME?=munger
PORTVERSION?=${VER_MAJOR}.${VER_MINOR}

INC=-I${LOCALBASE}/include
LIB= -lcalendar -lcrypt -lm -lcurses -L${LOCALBASE}/lib/

DATADIR?=${DESTDIR}${PREFIX}/share/${PORTNAME}
DEST=${DESTDIR}${PREFIX}/bin
SCRIPTDIR=${PREFIX}/bin
LIBDIR=${DESTDIR}${PREFIX}/lib
MANDIR=${DESTDIR}${PREFIX}/man/man1/

SCRIPTS= cat.munger transform.munger grep.munger view.munger mush.munger \
	fmt.munger filter.munger httpd.munger y.munger tables.munger \
	cal.munger xml2sqlite.munger rss.munger echo.munger

DATA=library.munger options.munger xmlsqlite.munger

SRC=intrinsics.c lisp.c lisp.h

VERSION=-DVERSION_MAJOR=${VER_MAJOR} -DVERSION_MINOR=${VER_MINOR}
DFLAGS= -DDATADIR=\"${DATADIR}\" ${VERSION}

.if defined(WITH_SQL)
DFLAGS+= -DSQL
LIB+= -pthread -lsqlite3
.endif

CFLAGS+= -O2 -Wall ${DFLAGS}
LFLAGS= -fpic -shared -Wl

INSTALL_PROGRAM?=/usr/bin/install -o root -g wheel -m555 -c -s
INSTALL_SCRIPT?=/usr/bin/install -o root -g wheel -m555 -c
INSTALL_DATA?=/usr/bin/install -o root -g wheel -m444 -c
INSTALL_MAN?=/usr/bin/install -o root -g wheel -m444 -c

MKDIR?=mkdir -p -m 555
CP?=/bincp
SED?=/usr/bin/sed
RM?=/bin/rm
GZIP_CMD?=/usr/bin/gzip
SHELL=/bin/sh

all: munger

debug: ${SRC}
	${CC} ${CFLAGS} -O0 -g -DDEBUG ${INC} lisp.c ${LIB} -o munger

munger: ${SRC}
	${CC} ${CFLAGS} ${INC} -o $@ lisp.c ${LIB}

tags: ${SRC}
	ctags ${SRC}

clean:
	${RM} -rf munger munger.gmon munger.gprof *.o tmp *.core tags *~ *.gz *.1 *.csv

.PHONY: install
install: all
	${MKDIR} ${DATADIR} ${DEST} ${MANDIR} ${LIBDIR}
.for I in ${SCRIPTS}
	${SED} -e "s|MUNGERPATH|${SCRIPTDIR}/munger|1" $I >tmp
	${INSTALL_SCRIPT} tmp ${DATADIR}/$I
.endfor
	${INSTALL_DATA} ${DATA} ${DATADIR}
	${RM} tmp
	${INSTALL_PROGRAM} munger ${DEST}
	cp munger.man munger.1
	${GZIP_CMD} -f munger.1
	${INSTALL_MAN} munger.1.gz ${MANDIR}

.PHONY: deinstall
deinstall:
	${RM} -rf ${DESTDIR}${PREFIX}/share/${PORTNAME}
	${RM} -f ${DEST}/munger
	${RM} -f ${MANDIR}/munger.1.gz
	${RM} -f ${MANDIR}/../cat1/munger.1.gz
