#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

package=tsocks

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

include /usr/share/dpatch/dpatch.make

build: patch build-stamp
build-stamp:
	dh_testdir
	-rm -f config.cache config.status .ChangeLog.swp
	cp /usr/share/misc/config.guess .
	cp /usr/share/misc/config.sub .
	./configure --prefix=/usr \
		--libdir=/usr/lib \
		--mandir=/usr/share/man \
		--with-conf=/etc/tsocks.conf \
		--enable-hostnames \
		#--enable-socksdns \

	$(MAKE) CFLAGS="-O2 -g -Wall"
	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp config.status config.guess
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f `find . -name "*~"`
	-rm -rf debian/$(package) debian/files* core debian/*substvars
	#-rm -rf debian/plus/tsocks.8*
	#-rm -rf debian/conffiles
	dh_clean

install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean -k

	dh_installdirs

	$(MAKE) install prefix=$(CURDIR)/debian/$(package)/usr \
	libdir=$(CURDIR)/debian/$(package)/usr/lib \
	mandir=$(CURDIR)/debian/$(package)/usr/share/man

	install -m 600 tsocks.conf.complex.example $(CURDIR)/debian/$(package)/etc/tsocks.conf
	install validateconf $(CURDIR)/debian/$(package)/usr/bin
	install inspectsocks $(CURDIR)/debian/$(package)/usr/bin
	install saveme $(CURDIR)/debian/$(package)/usr/bin
	#in the upstream now:
	#rm -f $(CURDIR)/debian/$(package)/usr/bin/tsocks
	install -m 755 $(CURDIR)/debian/plus/tsocks $(CURDIR)/debian/$(package)/usr/bin
	touch install-stamp

binary-indep: build install

# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: build install
#	-rm -rf debian/$(package)
	dh_testroot
	dh_testdir

#	dpkg-gencontrol -isp
	dh_undocumented
	dh_installdocs

	#dh_installdebconf

#how to solve that with dh_installman? (buggy manpage replacing)
	#rm -f $(CURDIR)/debian/$(package)/usr/share/man/man8/tsocks.8
	##in the upstream now:
	##sed s:/lib/:/usr/lib/:g $(CURDIR)/tsocks.8 > $(CURDIR)/debian/plus/tsocks.8
	###dh_compress $(CURDIR)/debian/plus/tsocks.8
	###dh_compress $(CURDIR)/debian/plus/tsocks.1
	#install -o root -g root $(CURDIR)/debian/plus/tsocks.8 $(CURDIR)/debian/$(package)/usr/share/man/man8/
	install -o root -g root $(CURDIR)/debian/plus/tsocks.1 $(CURDIR)/debian/$(package)/usr/share/man/man1/
	install -o root -g root $(CURDIR)/debian/plus/saveme.8 $(CURDIR)/debian/$(package)/usr/share/man/man8/
	dh_installman

	dh_installchangelogs ChangeLog upstream
	dh_installchangelogs
	dh_link
	dh_strip

	dh_compress

	dh_fixperms
	dh_makeshlibs

	#create conffiles automagically:
	#find $(CURDIR)/debian/$(package)/etc -type f|sed s:$(CURDIR)/debian/$(package):: >$(CURDIR)/debian/conffiles
	dh_installdeb

	dh_shlibdeps
	dh_gencontrol

	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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