# Copyright (c) 2008, 2009, 2011, 2012, 2013  Peter Pentchev
# All rights reserved.
#
# 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, 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.
#
# $Ringlet$

LOCALBASE?=	/usr/local
PREFIX?=	${LOCALBASE}
BINDIR?=	${PREFIX}/bin
MANDIR?=	${PREFIX}/man/man
EXAMPLESDIR?=	${PREFIX}/share/examples/${PROG}

LFS_CPPFLAGS?=	-D_FILE_OFFSET_BITS=64

PCRE_CPPFLAGS?=	-DHAVE_PCRE -I${LOCALBASE}/include
PCRE_LIBS?=	-L${LOCALBASE}/lib -lpcre

CC?=		gcc
CPPFLAGS?=
CPPFLAGS+=	${LFS_CPPFLAGS} ${PCRE_CPPFLAGS}
CFLAGS?=		-g -pipe
CFLAGS+=	-Wall -W
LDFLAGS?=
LIBS?=

RM=		rm -f
MKDIR?=		mkdir -p
SETENV?=	env

PROG=		confget
OBJS=		confget.o confget_common.o confget_http_get.o confget_ini.o
SRCS=		confget.c confget_common.c confget_http_get.c confget_ini.c
DEPENDFILE=	.depend

EXAMPLES=	t/t1.ini t/t2.ini

MAN1=		confget.1
MAN1GZ=		${MAN1}.gz

BINOWN?=	root
BINGRP?=	wheel
BINMODE?=	555

MANOWN?=	${BINOWN}
MANGRP?=	${BINGRP}
MANMODE?=	644

SHAREOWN?=	${BINOWN}
SHAREGRP?=	${BINGRP}
SHAREMODE?=	644

# development/debugging flags, you may safely ignore them
BDECFLAGS=	-W -Wall -std=c99 -pedantic -Wbad-function-cast -Wcast-align \
		-Wcast-qual -Wchar-subscripts -Winline \
		-Wmissing-prototypes -Wnested-externs -Wpointer-arith \
		-Wredundant-decls -Wshadow -Wstrict-prototypes -Wwrite-strings
#CFLAGS+=	${BDECFLAGS}
#CFLAGS+=	-ggdb -g3

TESTEXEC=	test-getline test-fgetln

all:		${PROG} ${MAN1GZ}

clean:
		${RM} ${PROG} ${OBJS} ${MAN1GZ}
		${RM} ${TESTEXEC} readaline.h

${PROG}:	${OBJS}
		${CC} ${LDFLAGS} -o ${PROG} ${OBJS} ${PCRE_LIBS}

.c.o:
		${CC} ${CPPFLAGS} ${CFLAGS} -c $<

${MAN1GZ}:	${MAN1}
		gzip -c9 ${MAN1} > ${MAN1GZ}.tmp
		mv ${MAN1GZ}.tmp ${MAN1GZ}

install:	all install-bin install-man install-examples

install-bin:
		-${MKDIR} ${DESTDIR}${BINDIR}
		install -c -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} ${PROG} ${DESTDIR}${BINDIR}/

install-man:
		-${MKDIR} ${DESTDIR}${MANDIR}1
		install -c -o ${MANOWN} -g ${MANGRP} -m ${MANMODE} ${MAN1GZ} ${DESTDIR}${MANDIR}1/

install-examples:
		-${MKDIR} ${DESTDIR}${EXAMPLESDIR}
		install -c -o ${SHAREOWN} -g ${SHAREGRP} -m ${SHAREMODE} ${EXAMPLES} ${DESTDIR}${EXAMPLESDIR}/

depend:
		touch readaline.h
		${SETENV} CPPFLAGS="-DCONFGET_MAKEDEP ${CPPFLAGS}" \
		DEPENDFILE="${DEPENDFILE}" sh makedep.sh ${SRCS}
		rm -f readaline.h
		
test:		all
		prove t

readaline.h:	test-getline.c Makefile
		rm -f readaline.h
		if ${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} -o test-getline test-getline.c ${LIBS}; then \
			echo '#define HAVE_GETLINE' > readaline.h; \
		else if ${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} -o test-fgetln test-fgetln.c ${LIBS}; then \
			echo '#define HAVE_FGETLN' > readaline.h; \
		else \
			echo 'Neither getline() nor fgetln() found!' 1>&2; \
			false; \
		fi; fi

.PHONY:		all clean depend install test

ifeq ($(wildcard ${DEPENDFILE}),${DEPENDFILE})
include ${DEPENDFILE}
endif
