#default task

# here there're few properties
_PRODUCT_NAME=utt
_BUILD_DIR=$(_UTT_BIN_DIR)
_UTT_VER=$(shell cat ../common/version.def)
_UTT_REL=$(shell cat ../common/release.def)
_DEB_ROOT=$(shell pwd)/deb_root
_INSTALL_DIR=/usr/local/$(_PRODUCT_NAME)/$(_UTT_VER)-$(_UTT_REL)

.PHONY: default
default:
	# we need some extra configuration files
	make_control
	make_postinst

	# first, we prepare some directory structure
	mkdir -p $(_DEB_ROOT)/DEBIAN
	mkdir -p $(_DEB_ROOT)$(_INSTALL_DIR)
	mkdir -p $(_DEB_ROOT)/usr/share/man/man1
	mkdir -p $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)

	find $(_DEB_ROOT) -type d | xargs chmod 755   # this is necessary on Debian Woody, don't ask me why

	# next, we copy necessary files
	mv ./control $(_DEB_ROOT)/DEBIAN/
	cp ./postinst $(_DEB_ROOT)/DEBIAN/
	cp ./prerm $(_DEB_ROOT)/DEBIAN/
#	cp -r $(_BUILD_DIR)/man/* $(_DEB_ROOT)/usr/share/man/
	cp $(_BUILD_DIR)/COPYRIGHT $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/copyright
#	cp $(_BUILD_DIR)/changelog $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/
#	cp $(_BUILD_DIR)/changelog.Debian $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/


	# next we make man/doc archives
#	gzip --best $(_DEB_ROOT)/usr/share/man/man1/$(_PRODUCT_NAME).1
#	gzip --best $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/changelog
#	gzip --best $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/changelog.Debian
#	tar -cvvf control.tar.gz ${_DEB_ROOT}/DEBIAN/
#	rm -fr ${_DEB_ROOT}/DEBIAN/

	# and binaries
	cp -rv $(_BUILD_DIR)/* $(_DEB_ROOT)$(_INSTALL_DIR)/
#	tar -cvvf data.tar.gz ${_DEB_ROOT}/
#	rm -fr ${_DEB_ROOT}/


	# finally, we buid deb package
	fakeroot dpkg-deb --build $(_DEB_ROOT)
	mv $(_DEB_ROOT).deb $(_PRODUCT_NAME)_$(_UTT_VER)-$(_UTT_REL).all.deb


.PHONY: make_control
make_control:
	echo "Package: $(_PRODUCT_NAME)" > control
	echo "Version: $(_UTT_VER)" >> control
	echo "Section: web" >> control
	echo "Priority: optional" >> control
	echo "Architecture: all" >> control
	echo "Essential: no" >> control

	echo "Depends: " >> control
# here we read this information from file ../common/requirements.def
	#libwww-perl, acme-base (>= 1.2)         <= wymagania pakietowe

	echo "Pre-Depends: perl" >> control

	echo "Maintainer: Adam Mickiewicz University" >> control
	echo "Provides: $(_PRODUCT_NAME)" >> control
	echo -n "Description: " >> control
	cat ../common/description.def >> control

.PHONY: make_postinst
make_postinst:
	echo "#!/bin/sh" > postinst
	echo "$(_INSTALL_DIR)/create_utt_config.pl" >> postinst
	echo "rm -f $(_INSTALL_DIR)/create_utt_config.pl" >> postinst

.PHONY: make_prerm
make_prerm:
	echo "#!/bin/sh" > prerm
