#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=0

# This is the debhelper compatability version to use.
#export DH_COMPAT=5

# Get options like
#       export DEB_BUILD_OPTIONS="parallel=4"
debian_build_options_list:=$(shell echo "$$DEB_BUILD_OPTIONS"|sed 's/[^-_=[:alnum:]]/ /g'|tr a-z- A-Z_)
$(foreach o, $(debian_build_options_list), $(if $(findstring =,$o),$(eval DEB_BUILD_OPT_$o),$(eval DEB_BUILD_OPT_$o=1)))
MAKE_-J += $(addprefix -j, $(DEB_BUILD_OPT_PARALLEL))

export destdir = $(CURDIR)/debian/gpsdrive
export destdir_utils = $(CURDIR)/debian/gpsdrive-utils
export destdir_friendsd = $(CURDIR)/debian/gpsdrive-friendsd

configure: build/configure-stamp
build/configure-stamp:
	# ------------------ configure the package.
	dh_testdir
	mkdir -p build

	cd build; \
	cmake -DCMAKE_BUILD_TYPE=Release \
	      -DCMAKE_INSTALL_PREFIX=/usr \
	      -DPERL_SITELIB=/usr/share/perl5 \
	      -DMAN_INSTALL_DIR=/usr/share/man \
	      -DINFO_INSTALL_DIR=/usr/share/info \
	      -DDATADIR=/usr/share \
	      .. 

	rm -f build/build-stamp
	touch build/configure-stamp

build: build/configure-stamp build/build-stamp
build/build-stamp:
	dh_testdir

	cd build; $(MAKE) $(MAKEFLAGS) -j4
	# -j4 is for now hardcoded until i find out how to set this through debuild

	touch build/build-stamp

clean:
	# ------------ clean
	dh_testdir
	dh_testroot
	dh_clean -k
	rm -f build/build-stamp build/configure-stamp debian/files
	rm -f CMakeCache.txt
	rm -f build/CMakeCache.txt

	# ------------------  clean up after the build process.
	-cd build; $(MAKE)  $(MAKEFLAGS) clean

	dh_clean

install: build
	# ------------ install
	dh_testdir
	dh_testroot
	dh_installdirs

	# ------------------  install the package into debian/gpsdrive.
	cd build; $(MAKE)  $(MAKEFLAGS) DESTDIR=$(destdir) install


	mkdir -p $(destdir_friendsd)/usr/bin
	mv $(destdir)/usr/bin/friendsd $(destdir_friendsd)/usr/bin/friendsd 

	# move modules and scripts. They are provided via the
	# gpsdrive-utils package
	/bin/bash debian/move_utils.sh


	/bin/bash debian/create_missing_man_pages.sh $(destdir)          $(destdir)/usr/share/man/man1
	/bin/bash debian/create_missing_man_pages.sh $(destdir_friendsd) $(destdir_friendsd)/usr/share/man/man1
	/bin/bash debian/create_missing_man_pages.sh $(destdir_utils)    $(destdir_utils)/usr/share/man/man1

	mv $(destdir)/usr/share/man/man1/friendsd*         $(destdir_friendsd)/usr/share/man/man1
	mkdir -p $(destdir_utils)/usr/share/man/man1/
	mv $(destdir)/usr/share/man/man1/gpsd_nmea.sh.1 $(destdir_utils)/usr/share/man/man1/gpsd_nmea.1

	/bin/bash debian/rename_scripts.sh $(destdir_utils)


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

# Build architecture-dependent files here.
binary-arch: build install
	# ------------ binary-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdebconf
	dh_installdocs
	dh_installmenu
	dh_pysupport
#	dh_python
	dh_pycentral
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
#	dh_undocumented friendsd2.1 gpspoint2gpsdrive.pl.1 mb2gpsdrive.pl.1 geocache2way.1
	dh_installchangelogs ChangeLog
#	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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