# SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
# SPDX-License-Identifier: BSD-2-Clause

CC?=		gcc
CFLAGS_STD?=	-std=c99
CFLAGS?=		-O -pipe
CFLAGS+=	${CFLAGS_STD}
CPPFLAGS_STD?=	-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
CPPFLAGS+=	${CPPFLAGS_STD}
LDFLAGS?=
LFLAGS?=	${LDFLAGS}
LIBS?=

RM=		rm -f
MKDIR?=		mkdir -p
CAT?=		cat

PROG=		timelimit
OBJS=		timelimit.o
SRCS=		timelimit.c

RUST_PROFILE?=	release
RUST_PROG=	target/${RUST_PROFILE}/timelimit-cli

CARGO?=		cargo

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

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

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

SHAREOWN?=	root
SHAREGRP?=	wheel
SHAREMODE?=	644

# development/debugging flags, you may safely ignore them
#CFLAGS+=	${BDECFLAGS}
#CFLAGS+=	-ggdb -g3

TIMELIMIT_FLAVOR?=	c

ifeq (${TIMELIMIT_FLAVOR},c)
FLAVORED_PROG=	${PROG}
else
ifeq (${TIMELIMIT_FLAVOR},rust)
FLAVORED_PROG=	${RUST_PROG}
ifeq (${RUST_PROFILE},release)
CARGO_RELEASE_FLAG=	--release
else
CARGO_RELEASE_FLAG=
endif
else
FLAVORED_PROG=	/nonexistent
endif
endif

INSTALL?=	install
COPY?=		-c
STRIP?=		-s

INSTALL_PROGRAM?=	${INSTALL} ${COPY} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE}
INSTALL_DATA?=		${INSTALL} ${COPY} -o ${SHAREOWN} -g ${SHAREGRP} -m ${SHAREMODE}

all:		${FLAVORED_PROG} ${MAN1GZ}

clean:		clean-${TIMELIMIT_FLAVOR}
		${RM} ${MAN1GZ}

clean-c:
		${RM} ${PROG} ${OBJS} autoconfig.h

clean-rust:
		${CARGO} clean

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

autoconfig.h:	autoconfig/try-err.c autoconfig/try-errno.c \
		autoconfig/try-setitimer.c autoconfig/try-sigaction.c \
		autoconfig/try-sysexits.c \
		Makefile
		: > autoconfig.h
		if ${CC} ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o try-err autoconfig/try-err.c; then \
			echo '#define HAVE_ERR' >> autoconfig.h; \
		else \
			echo '#undef HAVE_ERR' >> autoconfig.h; \
		fi
		test -f try-err && ${RM} try-err
		if ${CC} ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o try-errno autoconfig/try-errno.c; then \
			echo '#define HAVE_ERRNO_H' >> autoconfig.h; \
		else \
			echo '#undef HAVE_ERRNO_H' >> autoconfig.h; \
		fi
		test -f try-errno && ${RM} try-errno
		if ${CC} ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o try-sigaction autoconfig/try-sigaction.c; then \
			echo '#define HAVE_SIGACTION' >> autoconfig.h; \
		else \
			echo '#undef HAVE_SIGACTION' >> autoconfig.h; \
		fi
		test -f try-sigaction && ${RM} try-sigaction
		if ${CC} ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o try-setitimer autoconfig/try-setitimer.c; then \
			echo '#define HAVE_SETITIMER' >> autoconfig.h; \
		else \
			echo '#undef HAVE_SETITIMER' >> autoconfig.h; \
		fi
		test -f try-setitimer && ${RM} try-setitimer
		if ${CC} ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o try-sysexits autoconfig/try-sysexits.c; then \
			echo '#define HAVE_SYSEXITS_H' >> autoconfig.h; \
		else \
			echo '#undef HAVE_SYSEXITS_H' >> autoconfig.h; \
		fi
		test -f try-sysexits && ${RM} try-sysexits
		${CAT} autoconfig.h

timelimit.o:	timelimit.c config.h autoconfig.h
		${CC} ${CPPFLAGS} ${CFLAGS} -c timelimit.c

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

${RUST_PROG}:
		${CARGO} build ${CARGO_RELEASE_FLAG}

install:	all
		-${MKDIR} ${DESTDIR}${BINDIR}
		-${MKDIR} ${DESTDIR}${MANDIR}1
		-${MKDIR} ${DESTDIR}${EXAMPLESDIR}/tests
		${INSTALL_PROGRAM} ${FLAVORED_PROG} ${DESTDIR}${BINDIR}/timelimit
		${INSTALL_DATA} ${MAN1GZ} ${DESTDIR}${MANDIR}1/
		${INSTALL_DATA} t/*.t ${DESTDIR}${EXAMPLESDIR}/tests/

check:		all
ifeq (${TIMELIMIT_FLAVOR},rust)
		${CARGO} test ${CARGO_RELEASE_FLAG}
endif
		env TIMELIMIT='${CURDIR}/${FLAVORED_PROG}' prove -v t
