#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export LC_ALL=C

TZGEN := $(CURDIR)/tzgen
TZSOURCE := $(CURDIR)/tzsource
TIMEZONES := africa \
             antarctica \
             asia \
             australasia \
             europe \
             northamerica \
             southamerica \
             etcetera \
             factory \
             solar87 \
             solar88 \
             solar89 \
             backward \
             systemv 

TEMPLATES_FILE := $(CURDIR)/debian/templates
TEMPLATES_AREAS := Africa \
                   America \
		   Antarctica \
		   Australia \
		   Asia \
		   Atlantic \
		   Canada \
		   Europe \
		   Indian \
		   Pacific \
		   US \
		   SystemV \
		   Etc

QUILT := quilt --quiltrc ../debian/patches/quiltrc

build: build-stamp
build-stamp:
	dh_testdir

	# Unpack the sources
	mkdir $(TZSOURCE)
	for x in $$(ls tzdata*.tar.gz); do \
	  tar -xzvC $(TZSOURCE) -f $${x} ;\
	done
	
	# Apply patches
	@cd $(TZSOURCE); \
	if $(QUILT) next >/dev/null 2>&1; then \
	  echo -n "Applying patches..."; \
	  if $(QUILT) push -a -v > $(CURDIR)/patch-log 2>&1; then \
	    echo "successful."; \
	  else \
	    echo "failed! (check $(CURDIR)/patch-log for details)"; \
	    exit 1; \
	  fi; \
	else \
	  echo "No patches to apply"; \
	fi
	
	# Build
	for zone in $(TIMEZONES); do \
	  /usr/sbin/zic -d $(TZGEN) -L /dev/null -y $(TZSOURCE)/yearistype.sh $(TZSOURCE)/$${zone} ; \
	  /usr/sbin/zic -d $(TZGEN)/posix -L /dev/null -y $(TZSOURCE)/yearistype.sh $(TZSOURCE)/$${zone} ; \
	  /usr/sbin/zic -d $(TZGEN)/right -L $(TZSOURCE)/leapseconds -y $(TZGEN)/yearistype.sh $(TZSOURCE)/$${zone} ; \
	done

	# Generate a posixrules file
	/usr/sbin/zic -d $(TZGEN) -p America/New_York

	# Generate the templates file
	( echo "Template: tzdata/Areas" ; \
	echo "Type: select" ; \
	echo -n "_Choices: " ; \
	echo "$(TEMPLATES_AREAS)" | sed -e 's# #, #g' -e 's#, $$##' ; \
	echo "_Description: Geographic area:" ; \
	echo " Please select the geographic area in which you live, then you will" ; \
	echo " be shown a list of cities which represent the time zone in which they" ; \
	echo " are located." ; \
	echo ; \
	for i in $(TEMPLATES_AREAS) ; do \
	    echo "Template: tzdata/Zones/$$i" ; \
	    echo "Type: select" ; \
	    echo -n "_Choices: " ; \
	    cd $(CURDIR)/tzgen/$$i ; \
	    find . -maxdepth 1 -type f | sed -e 's#^\./##' | sort -n | tr '\n' ',' | sed -e 's#,#, #g' -e 's#, $$#\n#' ; \
	    echo "_Description: Time zone:" ; \
	    echo " Please select the time zone corresponding to your location." ; \
	    echo ; \
	done ) > $(TEMPLATES_FILE)
	debconf-updatepo
						
	touch $@

clean:
	dh_testdir
	dh_testroot
	-rm -rf $(TZSOURCE) $(TZGEN) .pc
	rm -f build-stamp
	rm -f patch-log
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	# Do nothing

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdirs
	dh_install
	dh_installchangelogs
	dh_link
	dh_installdocs
	dh_installinfo
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
