.include <src.opts.mk>

CLEANFILES+=	yearistype
CLEANDIRS+=	builddir
CONTRIBDIR=	${.CURDIR}/../../contrib/tzdata/
.PATH:		${CONTRIBDIR}

.if defined(LEAPSECONDS)
.warning "Using backwards compatibility variable for LEAPSECONDS; please use WITH_ZONEINFO_LEAPSECONDS_SUPPORT instead"
MK_ZONEINFO_LEAPSECONDS_SUPPORT= yes
.endif

.if ${MK_ZONEINFO_LEAPSECONDS_SUPPORT} != "no"
LEAPFILE=	-L ${CONTRIBDIR}leapseconds
.else
LEAPFILE=
.endif

TZFILES=	africa antarctica asia australasia etcetera europe \
		factory northamerica southamerica
TZFILES+=	backward systemv
POSIXRULES=	America/New_York

TZFILES:=	${TZFILES:S/^/${CONTRIBDIR}/}

TZBUILDDIR=	${.OBJDIR}/builddir
TZBUILDSUBDIRS=	\
		Africa \
		America/Argentina \
		America/Indiana \
		America/Kentucky \
		America/North_Dakota \
		Antarctica \
		Arctic \
		Asia \
		Atlantic \
		Australia \
		Etc \
		Europe \
		Indian \
		Pacific \
		SystemV
TZBUILDSUBDIRS+= US Mexico Chile Canada Brazil

.if !defined(_SKIP_BUILD)
all: zoneinfo
.endif
META_TARGETS+=	zoneinfo install-zoneinfo

zoneinfo: yearistype ${TDATA}
	mkdir -p ${TZBUILDDIR}
	cd ${TZBUILDDIR}; mkdir -p ${TZBUILDSUBDIRS}
	umask 022; cd ${.CURDIR}; \
	zic -D -d ${TZBUILDDIR} -p ${POSIXRULES} -m ${NOBINMODE} \
	    ${LEAPFILE} -y ${.OBJDIR}/yearistype ${TZFILES}

beforeinstall: install-zoneinfo
install-zoneinfo:
	mkdir -p ${DESTDIR}/usr/share/zoneinfo
	cd ${DESTDIR}/usr/share/zoneinfo;  mkdir -p ${TZBUILDSUBDIRS}
	cd ${TZBUILDDIR} && \
	    find -s * -type f -print -exec ${INSTALL} ${TAG_ARGS} \
	    -o ${BINOWN} -g ${BINGRP} -m ${NOBINMODE} \
	    \{} ${DESTDIR}/usr/share/zoneinfo/\{} \;
	${INSTALL} ${TAG_ARGS} -o ${BINOWN} -g ${BINGRP} -m ${NOBINMODE} \
	    ${CONTRIBDIR}/zone.tab ${DESTDIR}/usr/share/zoneinfo/

afterinstall:
#
# If the file /var/db/zoneinfo exists, and it is owned by root:wheel,
# and the contents of it exists in /usr/share/zoneinfo, then reinstall
# it.
#
	@if [ -f ${DESTDIR}/var/db/zoneinfo -a -O ${DESTDIR}/var/db/zoneinfo \
	    -a -G ${DESTDIR}/var/db/zoneinfo ]; then \
		zf=$$(cat ${DESTDIR}/var/db/zoneinfo); \
		if [ -f ${DESTDIR}/usr/share/zoneinfo/$${zf} ]; then \
			if [ ! -z "${DESTDIR}" ]; then \
				optC="-C ${DESTDIR}"; \
			fi; \
			echo "Updating /etc/localtime"; \
			tzsetup $${optC} -r; \
		fi; \
	else \
		echo "Run tzsetup(8) manually to update /etc/localtime."; \
	fi

.if ${MK_TESTS} != "no"
SUBDIR+=	tests
.endif

.include <bsd.prog.mk>
