#!/usr/bin/make -f
  
ifneq "$(wildcard /usr/share/quilt/quilt.make)" ""
	include /usr/share/quilt/quilt.make
endif

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

#CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

config.status: configure
	dh_testdir
	autoreconf -f -v -i
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man

build-arch: config.status build-arch-stamp
build-arch-stamp:
	dh_testdir

	$(MAKE)
	touch $@

build-indep: config.status build-indep-stamp
build-indep-stamp:
	dh_testdir

	touch $@

build: patch build-arch build-indep

clean: unpatch
	dh_testdir
	rm -f build-arch-stamp build-indep-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	dh_clean

psource:=open-vm-source

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	
	dh_installdirs -p$(psource)  usr/src/modules/$(sname)/debian
	dh_installdirs -popen-vm-tools usr/share/modass/packages
	# Installing configuration files
	install -D -m 0644 debian/config/xautostart.conf debian/open-vm-tools-gui/etc/vmware-tools/xautostart.conf
	install -D -m 0644 debian/config/vmware-user.desktop debian/open-vm-tools-gui/usr/share/autostart/vmware-user.desktop
	install -D -m 0644 debian/config/vmware-guestd debian/open-vm-tools/etc/pam.d/vmware-guestd
	install -D -m 0644 debian/config/tools.conf debian/open-vm-tools/etc/vmware-tools/tools.conf

	dh_install

	# open-vm-source
	cp -a modules/linux/* debian/$(psource)/usr/src/modules/$(sname)
	cp debian/*modules.in* debian/$(psource)/usr/src/modules/$(sname)/debian
	cp debian/rules debian/changelog debian/copyright \
	    debian/compat debian/$(psource)/usr/src/modules/$(sname)/debian/
	cd debian/$(psource)/usr/src && tar cjf $(sname).tar.bz2 modules && rm -rf modules
	ln -sf default.sh $(CURDIR)/debian/open-vm-tools/usr/share/modass/packages/$(psource)

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install 
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs  -i
	dh_installdocs -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -s
	dh_testroot -s
	dh_installdocs -s
	dh_installinit -s
	dh_installman -s debian/checkvm.1
	dh_installchangelogs  -s
	dh_strip -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install  binary-modules kdist kdist_configure kdist_config kdist_image kdist_clean

# The short upstream name, used for the module source directory
sname:=open-vm
moddir:=vmblock vmhgfs vmmemctl vmxnet

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# prefix of the target package name
PACKAGE=open-vm-modules
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass
# load generic variable handling
-include $(MA_DIR)/include/generic.make
# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make

# module assistant calculates all needed things for us and sets
# following variables:
# KSRC (kernel source directory), KVERS (kernel version string), KDREV
# (revision of the Debian kernel-image package), CC (the correct
# compiler), VERSION (the final package version string), PKGNAME (full
# package name with KVERS included), DEB_DESTDIR (path to store DEBs)

# The kdist_configure target is called by make-kpkg modules_config and
# by kdist* rules by dependency. It should configure the module so it is
# ready for compilation (mostly useful for calling configure).
# prep-deb-files from module-assistant creates the neccessary debian/ files 
kdist_config kdist_configure: prep-deb-files

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)
kdist_clean:
	@for i in $(moddir);\
	do \
	    $(MAKE) KERNELDIR=$(KSRC) KVERREL=$(KVERS) -C "$$i" clean ; \
	done
	rm -f *.o
#
### end  KERNEL SETUP

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules:
	dh_testroot
	dh_clean -k
	dh_installdirs lib/modules/$(KVERS)/misc
	dh_installdirs -p$(PKGNAME) lib/modules/$(KVERS)/misc

	# Build the modules
	# vmblock vmhgfs  vmmemctl  vmxnet
	@for i in $(moddir);\
	do \
	    $(MAKE) KERNELDIR=$(KSRC) KVERREL=$(KVERS) -C "$$i" ; \
	    cp "$$i"/"$$i".ko debian/$(PKGNAME)/lib/modules/$(KVERS)/misc ; \
	done

	dh_installdocs
	dh_installchangelogs
	dh_installmodules
	sed -e "s/@KERNEL@/$(KVERS)/g" debian/preinst.modules.in > debian/preinst
	sed -e "s/@KERNEL@/$(KVERS)/g" debian/postrm.modules.in > debian/postrm
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)
	dh_clean -k


