#!/usr/bin/make -f
# 
# Important modifications (introduction of a saved config.cache to
#	solve build problems) introduced in Samba 2.2.1a-5. These
#	modification were made by Steve Langasek <vorlon@netexpress.net>.
#
#

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

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

# 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

CFLAGS = -g -Wall

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

ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -mthumb
endif

DESTDIR=`pwd`/debian/tmp

conf_args = \
		--cache-file=./config.cache \
		--with-fhs \
		--enable-shared \
		--enable-static \
		--disable-pie \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libdir=/etc/samba \
		--with-privatedir=/etc/samba \
		--with-piddir=/var/run/samba \
		--localstatedir=/var \
		--without-netatalk \
		--without-pam \
		--with-syslog \
		--with-utmp \
		--without-readline \
		--without-pam_smbpass \
		--with-libsmbclient \
		--without-winbind \
		--with-shared-modules=idmap_rid,idmap_ad \
		--without-msdfs \
		--without-automount \
		--without-tdbsam \
		--without-ldap \
		--without-python

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

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  conf_args += --build $(DEB_HOST_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 all nsswitch/libnss_wins.so
ifeq ($(mount_cifs),yes)
	$(MAKE) -C source client/mount.cifs client/umount.cifs
endif

	touch build-stamp

clean:	clean-patched unpatch

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

	# Clean first the Samba package
	-$(MAKE) -C source distclean

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

	# No type-handling in ubuntu main
	#sed -e "s/@libacl@/`type-handling any linux-gnu`/g" \
	#	< debian/control.in > debian/control

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	mkdir -p $(DESTDIR)/usr/share/man $(DESTDIR)/usr/lib/samba \
	         $(DESTDIR)/lib/security $(DESTDIR)/sbin \
	         $(DESTDIR)/usr/lib/cups/backend $(DESTDIR)/usr/share/samba \
	         $(DESTDIR)/etc/dhcp3/dhclient-enter-hooks.d
#	         $(DESTDIR)/usr/lib/python2.4/site-packages/samba

	# Add here commands to install the package into debian/tmp.
	$(MAKE) -C source install DESTDIR=$(DESTDIR)

	# libsmbclient files are not installed by the standard
	#	'make install' - do it manually.
	$(MAKE) -C source installclientlib DESTDIR=$(DESTDIR)
	mv $(DESTDIR)/usr/lib/samba/libsmbclient.so $(DESTDIR)/usr/lib/samba/libsmbclient.so.0.1
	ln -s libsmbclient.so.0.1 $(DESTDIR)/usr/lib/samba/libsmbclient.so.0
	ln -s libsmbclient.so.0.1 $(DESTDIR)/usr/lib/samba/libsmbclient.so

	# 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/

	# Install other stuff not installed by "make install"
	install -m 0755 debian/mksmbpasswd.awk $(DESTDIR)/usr/sbin/mksmbpasswd

	# Install libnss_wins.so, which is not installed by 'make install' either.
	install -m 0644 source/nsswitch/libnss_wins.so \
		$(DESTDIR)/lib/libnss_wins.so.2

ifeq ($(smbfs),yes)
	# Create the symlinks that will allow us to do "mount -t smbfs ..."
	# and "mount -t smb ...". Note that the source/script/installbin.sh
	# tries to create the first symlink, but we have commented
	# that code out and do everything here. We also create
	# symlinks for the man pages.
	ln -sf /usr/bin/smbmount $(DESTDIR)/sbin/mount.smbfs
	ln -sf /usr/bin/smbmount $(DESTDIR)/sbin/mount.smb
	ln -sf smbmount.8 $(DESTDIR)/usr/share/man/man8/mount.smb.8
	ln -sf smbmount.8 $(DESTDIR)/usr/share/man/man8/mount.smbfs.8
endif

ifeq ($(mount_cifs),yes)
	install -m 0755 source/client/mount.cifs $(DESTDIR)/sbin/
	install -m 0755 source/client/umount.cifs $(DESTDIR)/sbin/
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

	# Install man pages for files without man pages in the upstream sources
	install -m 0644 debian/mksmbpasswd.8 $(DESTDIR)/usr/share/man/man8/mksmbpasswd.8

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

	# Install the Python modules
	#
	# Hmmm... need to figure this out. We have lib.linux-i686-2.2
	# and lib.linux-i686-2.4 directories. Using only the stuff from
	# the 2.4 directory for now. peloy.-
	#cp source/build/lib.*/samba/*.so $(DESTDIR)/usr/lib/python2.4/site-packages/
#	cp source/build/lib.*-*-2.4/samba/*.so $(DESTDIR)/usr/lib/python2.4/site-packages/samba/
#	cp source/python/samba/* $(DESTDIR)/usr/lib/python2.4/site-packages/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-*
#	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
#	dh_installmanpages
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
#	dh_perl
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
ifeq ($(smbfs),no)
  DH_EXTRAS=-Nsmbfs
endif

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_installmenu
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
	DH_OPTIONS= dh_installinit -psamba -- "defaults 20 19"
	dh_installcron 
#	dh_installmanpages
#	dh_installinfo
	for pkg in samba smbfs; do \
	  install -m 0644 -D debian/$$pkg.lintian debian/$$pkg/usr/share/lintian/overrides/$$pkg || exit; \
	done
#	dh_undocumented
	dh_installchangelogs -Nlibpam-smbpass
	dh_strip --dbg-package=samba --dbg-package=smbclient
	cp -a debian/smbclient-dbg/* debian/samba-dbg
	rm -rf debian/smbclient-dbg
	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

	# You may want to make some executables suid here.
ifeq ($(smbfs),yes)
	# The smbmnt and smbumount binaries should be setuid-root. This
	#	has security implications because these programs haven't had
	#	a thorough security audit. smbmount _does not_ have to have
	#	the setuid bit set. In fact, it is a security hole.
	#chmod u+s debian/smbfs/usr/bin/smbmnt
	#chmod ug+s debian/smbfs/usr/bin/smbumount
endif
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/var/log/samba/
	chown root.adm debian/samba/var/log/samba/

	dh_installdeb
#	dh_makeshlibs
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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