#!/usr/bin/make -f

# This has to be exported to make some magic below work.
export DH_OPTIONS

# Set the host and build architectures for use with config.cache loading,
# cross-building, etc.
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH_OS	:= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

export DEB_HOST_GNU_TYPE
export DEB_BUILD_GNU_TYPE
export DEB_HOST_ARCH_OS

pyversion := $(shell pyversions -vd)

CFLAGS = -g -Wall

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

include /usr/share/quilt/quilt.make

DESTDIR=`pwd`/debian/tmp

conf_args = \
		--cache-file=./config.cache \
		--with-fhs \
		--enable-shared \
		--enable-static \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libdir=/usr/lib/samba \
		--with-privatedir=/etc/samba \
		--with-piddir=/var/run/samba \
		--localstatedir=/var \
		--with-rootsbindir=/sbin \
		--with-pammodulesdir=/lib/security \
		--with-pam \
		--with-syslog \
		--with-utmp \
		--with-readline \
		--with-pam_smbpass \
		--with-libsmbclient \
		--with-winbind \
		--with-shared-modules=idmap_rid,idmap_ad,idmap_adex,idmap_hash \
		--with-automount \
		--with-ldap \
		--with-ads \
		--without-smbmount \
		--with-dnsupdate \
		--without-libtdb \
		--without-libnetapi \
		--with-modulesdir=/usr/lib/samba \
		--datarootdir=/usr/share \
		--with-swatdir=/usr/share/samba/swat \
		--with-lockdir=/var/run/samba  \


ifeq ($(DEB_HOST_ARCH_OS),linux)
  conf_args += \
		--with-cifsmount \
		--with-cifsupcall \
		--with-acl-support \
		--with-quotas
  mount_cifs	= yes
  smbfs		= yes
else
  conf_args +=	--without-quotas --without-cifsmount
  mount_cifs	= no
  smbfs		= no
endif

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  conf_args += --build $(DEB_BUILD_GNU_TYPE)
else
  conf_args += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

configure: patch configure-stamp
configure-stamp:
	dh_testdir

	if [ -f debian/config.cache ]; then \
		cp -f debian/config.cache source/config.cache; \
	fi

	[ -f source/Makefile ] || (cd source && CFLAGS="$(CFLAGS)" ./configure $(conf_args))

	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir

	$(MAKE) -C source # headers
	$(MAKE) -C source everything nsswitch/libnss_wins.so

	touch build-stamp

clean:	clean-patched unpatch

clean-patched: 
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	[ ! -f source/Makefile ] || $(MAKE) -C source distclean

	# Delete stuff left after a build that is not deleted by 'make clean'
	rm -f source/bin/wbinfo source/bin/winbindd source/bin/debug2html \
	source/bin/libsmbclient.a \
	source/client/mount.cifs source/client/umount.cifs \
	source/include/stamp-h

	debconf-updatepo

	dh_clean

ifeq ($(smbfs),no)
  DH_EXTRAS=-Nsmbfs
endif

install: DH_OPTIONS=$(DH_EXTRAS)
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	mkdir -p $(DESTDIR)/usr/lib/cups/backend $(DESTDIR)/usr/share/samba \
	         $(DESTDIR)/etc/pam.d $(DESTDIR)/etc/dhcp3/dhclient-enter-hooks.d \
		$(DESTDIR)/etc/ufw/applications.d

	$(MAKE) -C source install DESTDIR=$(DESTDIR)

	# Rename to *.samba3 for alternatives
	mv $(DESTDIR)/usr/bin/smbstatus $(DESTDIR)/usr/bin/smbstatus.samba3
	mv $(DESTDIR)/usr/bin/nmblookup $(DESTDIR)/usr/bin/nmblookup.samba3
	mv $(DESTDIR)/usr/share/man/man1/nmblookup.1 $(DESTDIR)/usr/share/man/man1/nmblookup.samba3.1
	mv $(DESTDIR)/usr/bin/net $(DESTDIR)/usr/bin/net.samba3
	mv $(DESTDIR)/usr/share/man/man8/net.8 $(DESTDIR)/usr/share/man/man8/net.samba3.8
	mv $(DESTDIR)/usr/bin/testparm $(DESTDIR)/usr/bin/testparm.samba3
	mv $(DESTDIR)/usr/share/man/man1/testparm.1 $(DESTDIR)/usr/share/man/man1/testparm.samba3.1

	mv $(DESTDIR)/usr/share/samba/lowcase.dat $(DESTDIR)/usr/share/samba/lowcase.dat.samba3
	mv $(DESTDIR)/usr/share/samba/upcase.dat $(DESTDIR)/usr/share/samba/upcase.dat.samba3
	mv $(DESTDIR)/usr/share/samba/valid.dat $(DESTDIR)/usr/share/samba/valid.dat.samba3

	# Starting with Samba 3.0.6 libsmbclient.so is installed in
	# /usr/lib/samba. We don't want it there since it is not in the
	# default library path. Here we move it to /usr/lib/.
	mv $(DESTDIR)/usr/lib/samba/libsmbclient* $(DESTDIR)/usr/lib/

	# same problem with libwbclient.
	mv $(DESTDIR)/usr/lib/samba/libwbclient* $(DESTDIR)/usr/lib/

	# Install other stuff not installed by "make install"
	install -m 0755 debian/mksmbpasswd.awk $(DESTDIR)/usr/sbin/mksmbpasswd
	install -m 0644 debian/mksmbpasswd.8 $(DESTDIR)/usr/share/man/man8/mksmbpasswd.8
	install -m 0644 source/nsswitch/libnss_winbind.so $(DESTDIR)/lib/libnss_winbind.so.2
	install -m 0644 source/nsswitch/libnss_wins.so $(DESTDIR)/lib/libnss_wins.so.2
	# Install torture stuff
	install -m 0755 source/bin/smbtorture $(DESTDIR)/usr/bin/smbtorture
	install -m 0755 source/bin/msgtest $(DESTDIR)/usr/bin/msgtest
	install -m 0755 source/bin/masktest $(DESTDIR)/usr/bin/masktest
	install -m 0755 source/bin/locktest $(DESTDIR)/usr/bin/locktest
	install -m 0755 source/bin/locktest2 $(DESTDIR)/usr/bin/locktest2
	install -m 0755 source/bin/nsstest $(DESTDIR)/usr/bin/nsstest
	install -m 0755 source/bin/vfstest $(DESTDIR)/usr/bin/vfstest
	install -m 0755 source/bin/pdbtest $(DESTDIR)/usr/bin/pdbtest
	install -m 0755 source/bin/talloctort $(DESTDIR)/usr/bin/talloctort
	install -m 0755 source/bin/replacetort $(DESTDIR)/usr/bin/replacetort
	install -m 0755 source/bin/tdbtorture $(DESTDIR)/usr/bin/tdbtorture
	install -m 0755 source/bin/smbconftort $(DESTDIR)/usr/bin/smbconftort
	mkdir -p $(DESTDIR)/usr/share/pam-configs
	install -m 0644 debian/libpam-smbpass.pam-config $(DESTDIR)/usr/share/pam-configs/smbpasswd-migrate

ifeq ($(mount_cifs),yes)
	install -m 0755 debian/mount.smbfs $(DESTDIR)/sbin/mount.smbfs
endif

	# For CUPS to support printing to samba printers, it's necessary
	#	to make the following symlink (according to
	#	Erich Schubert <debian@vitavonni.de> in #109509):
	ln -s ../../../bin/smbspool $(DESTDIR)/usr/lib/cups/backend/smb

	# ufw integration
	install -m644 debian/samba.ufw.profile $(DESTDIR)/etc/ufw/applications.d/samba

	cp debian/smb.conf* $(DESTDIR)/usr/share/samba/
	install -m755 debian/panic-action $(DESTDIR)/usr/share/samba/panic-action.samba3
	# Install samba-common's conffiles - they'll get moved later to their
	# correct place by dh_movefiles.
	cp debian/gdbcommands $(DESTDIR)/etc/samba/
	cp debian/samba.pamd $(DESTDIR)/etc/pam.d/samba
	install -m755 debian/samba-common.dhcp $(DESTDIR)/etc/dhcp3/dhclient-enter-hooks.d/samba

	dh_movefiles

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs -A debian/README.build
	# Ignore COPYING. Otherwise, an extra copy of the GPL licence 
	# in smbldap-tools example is included in samba-doc
	dh_installexamples -Xsmbldap-tools-* -XCOPYING
	dh_installchangelogs
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.

binary-arch: DH_OPTIONS=-a $(DH_EXTRAS)
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs -A debian/README.build
	dh_installexamples
	dh_installlogrotate
	dh_installlogcheck
	DH_OPTIONS= dh_installinit -psamba -- "defaults 20 19"
	DH_OPTIONS= dh_installinit -pwinbind
	dh_installcron 
	for pkg in samba samba-common smbfs winbind; do \
	  install -m 0644 -D debian/$$pkg.lintian debian/$$pkg/usr/share/lintian/overrides/$$pkg || exit; \
	done
	dh_installchangelogs -Nlibpam-smbpass
	DH_OPTIONS= dh_installchangelogs -plibpam-smbpass source/pam_smbpass/CHANGELOG
	DH_OPTIONS= dh_strip -psamba -psmbclient -pwinbind -psamba-common --dbg-package=samba-dbg
	DH_OPTIONS= dh_strip -Nsamba -Nsmbclient -Nwinbind -Nsamba-common
	dh_link
	dh_compress
	dh_fixperms

	# Upstream makefile installs this using "install" without -m, so
	# it becomes executable
	chmod a-x debian/libsmbclient-dev/usr/include/libsmbclient.h

ifeq ($(mount_cifs),yes)
	chmod u+s debian/smbfs/sbin/mount.cifs
	chmod u+s debian/smbfs/sbin/umount.cifs
endif

	# Set some reasonable default perms for the samba logdir.
	chmod 0750 debian/samba-common/var/log/samba/
	chown root:adm debian/samba-common/var/log/samba/
	chmod 1777 debian/samba/var/spool/samba/

	DH_OPTIONS= dh_makeshlibs -plibsmbclient -V'libsmbclient (>= 2:3.2.0)'
	DH_OPTIONS= dh_makeshlibs -plibwbclient0
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

update-archs:
	sed -i -e "s/libacl1-dev \(([^)]\+)\) \[[^]]\+\]/libacl1-dev \1 [`type-handling any linux-gnu`]/g" \
	       -e "s/libkeyutils-dev \(([^)]\+)\) \[[^]]\+\]/libkeyutils-dev \1 [`type-handling any linux-gnu`]/g" \
		debian/control

.PHONY: build clean binary-indep binary-arch binary install configure patch unpatch update-archs
