# Copyright (c) 2008, 2009, 2011  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$

# You MUST define either -DHAVE_GETLINE or -DHAVE_FGETLN in CFLAGS_CONF!
#CFLAGS_CONF=	-DHAVE_GETLINE

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

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

CC?=		gcc
CFLAGS?=		-g -pipe
CFLAGS+=	-Wall -W ${CFLAGS_CONF} ${PCRE_CFLAGS}
LDFLAGS?=
LFLAGS?=	${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 -ansi -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

all:		${PROG} ${MAN1GZ}

clean:
		${RM} ${PROG} ${OBJS} ${MAN1GZ}

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

.c.o:
		${CC} ${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:
		${SETENV} CFLAGS="${CFLAGS}" DEPENDFILE="${DEPENDFILE}" \
		sh makedep.sh ${SRCS}
		
test:		all
		prove t

.PHONY:		all clean depend install test

include ${DEPENDFILE}
