#!/usr/bin/make -f

package=tin
version=1.30b
debian=4

build:
	$(checkdir)
	cd src && $(MAKE) $(FLAGS) linux
	touch build

clean:
	$(checkdir)
	-rm -f build
	-$(MAKE) $(FLAGS) clobber
	-rm -rf debian-tmp debian-tmp.deb 

binary:		checkroot build
	-rm -rf debian-tmp
	install -m 755 -o root -g root -d debian-tmp/{DEBIAN,etc,usr}
	install -m 755 -o root -g root -d debian-tmp/usr/{bin,man,doc}
	install -m 755 -o root -g root -d debian-tmp/usr/man/man1
	install -m 755 -o root -g root -d debian-tmp/usr/doc/news
        # Install the binaries
	cd src && install -m 755 -o root -g root tin ../debian-tmp/usr/bin/.
	ln -s tin debian-tmp/usr/bin/rtin
	cd doc && install -m 644 -o root -g root tin.1 ../debian-tmp/usr/man/man1/.
	cd doc && install -m 644 -o root -g root intro.txt \
		../debian-tmp/usr/doc/news/tin-intro
	install -m 644 -o root -g root debian.tinhelp.text \
		debian-tmp/usr/doc/news/tin-readme

	sed -e '2s/=/$(version)/; 3s/=/$(debian)/' \
		debian.control > debian-tmp/DEBIAN/control
	cp debian.postinst debian-tmp/DEBIAN/postinst
	chmod +x debian-tmp/DEBIAN/postinst
	chown -R root.root debian-tmp
	chmod -R u+w,go-w,a+r,ug-s debian-tmp
	find debian-tmp -perm +100 | xargs chmod a+x
	dpkg --build debian-tmp
	mv debian-tmp.deb ../$(package)-$(version)-$(debian).deb
	-rm -rf debian-tmp

define checkdir
	test -f INSTALL
endef

source:		clean
	chmod +x debian.rules
	cd .. && \
	tar cf $(package)-$(version)-$(debian).tar $(package)-$(version) && \
	gzip -9vf $(package)-$(version)-$(debian).tar
 
diff:		clean
	cd .. && \
	(diff -ruN $(package)-$(version).orig $(package)-$(version) \
	 >$(package)-$(version)-$(debian).diff; [ $$? = 1 ]) && \
	gzip -9vf $(package)-$(version)-$(debian).diff

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary source diff clean checkroot
