#default task

DIR=$(shell pwd)

ifndef UTT_DIST_DIR
	UTT_DIST_DIR=${DIR}
endif

ifndef UTT_DIST_OUTPUT
	UTT_DIST_OUTPUT=${DIR}
endif

# here there're few properties
_PRODUCT_NAME=utt
_UTT_VER=$(shell cat ../common/version.def)
_UTT_REL=$(shell cat ../common/release.def)
_DEB_FROOT=$(DIR)/deb_root
_UTT_DIR=${_DEB_FROOT}/usr/local/$(_PRODUCT_NAME).$(_UTT_VER)-$(_UTT_REL)

.PHONY: default
default: make_control make_postinst make_prerm
	# first, we prepare some directory structure
	mkdir -p $(_DEB_FROOT)/DEBIAN
	mkdir -p $(_UTT_DIR)

	# next, we copy deb package files
	mv ./control $(_DEB_FROOT)/DEBIAN/
	mv ./postinst $(_DEB_FROOT)/DEBIAN/
	mv ./prerm $(_DEB_FROOT)/DEBIAN/
	cd ${_DEB_FROOT} && tar -cvvf control.tar.gz DEBIAN/
	cd ${DIR};
	rm -fr ${_DEB_FROOT}/DEBIAN/

	# we copy all necessery files (binaries)
	cp -r ${UTT_DIST_DIR}/* ${_UTT_DIR}/
	cp ./changelog ${_UTT_DIR}/share/doc/$(_PRODUCT_NAME)/
#	gzip --best $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/changelog
	cp ./changelog.Debian $(_UTT_DIR)/share/doc/$(_PRODUCT_NAME)/
#	gzip --best $(_DEB_ROOT)/usr/share/doc/$(_PRODUCT_NAME)/changelog.Debian
	cp ../files/* ${_UTT_DIR}/share/doc/${_PRODUCT_NAME}/
	cp ../common/utt_make_config.pl ${_UTT_DIR}/bin/
	chmod 755 ${_UTT_DIR}/bin/utt_make_config.pl
	


#	# next we make man/doc archives
#	gzip --best $(_DEB_ROOT)/usr/share/man/man1/$(_PRODUCT_NAME).1

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

	# finally, we buid deb package
	fakeroot dpkg-deb --build $(_DEB_FROOT)
	mv $(_DEB_FROOT).deb $(_PRODUCT_NAME)_$(_UTT_VER)-$(_UTT_REL).all.deb
	rm -rf ${_DEB_FROOT}
	


.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
