#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif


# SEARPC_SOURCE_DIR and CCNET_SOURCE_DIR is set by build-deb.py
searpc_dir = $(LIBSEARPC_SOURCE_DIR)
ccnet_dir = $(CCNET_SOURCE_DIR)
seafile_client_dir = $(SEAFILE_CLIENT_SOURCE_DIR)
destdir = $(CURDIR)/debian/seafile
install_prefix = $(destdir)/usr

build:
	dh_testdir
	mkdir -p $(install_prefix)
	# build and install libsearpc so it can be used by ccnet and seafile
	cd $(searpc_dir) && ./configure $(CROSS) --prefix=/usr --disable-compile-demo CFLAGS="$(CFLAGS)" && make && make install DESTDIR=$(destdir)

	# Fix the problem of DESTDIR and libtool
	sed -i -e "s;/usr/lib/libsearpc-json-glib;$(destdir)/usr/lib/libsearpc-json-glib;g" $(install_prefix)/lib/libsearpc.la

	# build and install ccnet so it can be used by seafile
	cd $(ccnet_dir) && ./configure $(CROSS) --prefix=/usr --disable-compile-demo CFLAGS="$(CFLAGS)" && make && make install DESTDIR=$(destdir)

	./configure --prefix=/usr CFLAGS="$(CFLAGS)" && make && make install DESTDIR=$(destdir)

	# build and install seafile-client
	cd $(seafile_client_dir) && cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release . && make -j2 && make install DESTDIR=$(destdir)

	# remove development files
	rm -rf $(install_prefix)/include
	rm -rf $(install_prefix)/lib/pkgconfig
	rm -rf $(install_prefix)/lib/*.a
	rm -rf $(install_prefix)/lib/*.la
	rm -f $(install_prefix)/bin/searpc-codegen.py
	rm -f $(install_prefix)/bin/ccnet-servtool

	rm -f $(install_prefix)/bin/seafile-web
	rm -rf $(install_prefix)/share/seafile

clean:
	dh_clean

# Build architecture-independent files here.
binary-indep:
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch:
	dh_testroot
	dh_installchangelogs
	dh_installdocs
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_python
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installman
#	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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