#!/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

ifeq ($(DEB_HOST_ARCH), amd64)
  _arc=_x86_64
  ifneq ($(DEB_HOST_ARCH), $(DEB_BUILD_ARCH))
    conf_option = "--host=x86_64-linux-gnu"
  endif
else ifeq ($(DEB_HOST_ARCH), i386)
  _arc=_i686
  ifneq ($(DEB_HOST_ARCH), $(DEB_BUILD_ARCH))
    conf_option = "--host=i686-linux-gnu"
  endif
else ifeq ($(DEB_HOST_ARCH), arm64)
  _arc = _aarch64
  ifneq ($(DEB_HOST_ARCH), $(DEB_BUILD_ARCH))
    conf_option = "--host=aarch64-linux-gnu"
  endif
else ifeq ($(DEB_HOST_ARCH), mips64el)
  _arc = _mips64
  ifneq ($(DEB_HOST_ARCH), $(DEB_BUILD_ARCH))
    conf_option = "--host=mips64el-linux-gnuabi64"
  endif
endif

# define package name
COMMON_PKG=scangearmp2

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	### configure common package
	if [ -z $(nobuild) ] ; then \
		@echo "######## configure scangearmp2"; \
		cd ./scangearmp2; ./autogen.sh --prefix=/usr LDFLAGS="-L`pwd`/../com/libs_bin${_arc}" $(conf_option); \
	fi

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir

	# Add here commands to compile the package.
	#$(MAKE)

	### build common package
	if [ -z $(nobuild) ] ; then \
		@echo "######## compile scangearmp2"; \
		cd ./scangearmp2; make; \
	fi

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	#-$(MAKE) clean
	-cd ./scangearmp2; make clean

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	dh_install -s

	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/bin
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/lib
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/de/LC_MESSAGES
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/fr/LC_MESSAGES
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/ja/LC_MESSAGES
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/zh/LC_MESSAGES
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/share/scangearmp2
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/etc/udev/rules.d
	mkdir -p $(CURDIR)/debian/${COMMON_PKG}/usr/lib/bjlib

	### install common package
	if [ -z $(nobuild) ] ; then \
		cd ./scangearmp2; make install DESTDIR=$(CURDIR)/debian/${COMMON_PKG}; \
	else \
		install -c -m 755 ./com/libs_bin${_arc}/scangearmp2 $(CURDIR)/debian/${COMMON_PKG}/usr/bin; \
		install -c -m 644 ./com/libs_bin${_arc}/de/LC_MESSAGES/scangearmp2.mo $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/de/LC_MESSAGES; \
		install -c -m 644 ./com/libs_bin${_arc}/fr/LC_MESSAGES/scangearmp2.mo $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/fr/LC_MESSAGES; \
		install -c -m 644 ./com/libs_bin${_arc}/ja/LC_MESSAGES/scangearmp2.mo $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/ja/LC_MESSAGES; \
		install -c -m 644 ./com/libs_bin${_arc}/zh/LC_MESSAGES/scangearmp2.mo $(CURDIR)/debian/${COMMON_PKG}/usr/share/locale/zh/LC_MESSAGES; \
		install -c -m 644 ./scangearmp2/scangearmp2.glade $(CURDIR)/debian/${COMMON_PKG}/usr/share/scangearmp2; \
		install -c -m 644 ./scangearmp2/src/canon_mfp2.conf $(CURDIR)/debian/${COMMON_PKG}/usr/lib/bjlib; \
	fi
	### remove .la .a
	rm -f $(CURDIR)/debian/${COMMON_PKG}/usr/lib/*.la $(CURDIR)/debian/${COMMON_PKG}/usr/lib/*.a

	### copy common libraries
	install -c -m 755 ./com/libs_bin${_arc}/*.so.* $(CURDIR)/debian/${COMMON_PKG}/usr/lib
	
	### copy udev rules file
	install -c -m 644 ./scangearmp2/etc/*.rules $(CURDIR)/debian/${COMMON_PKG}/etc/udev/rules.d

	### copy ini file
	install -c -m 666 ./com/ini/canon_mfp2_net.ini $(CURDIR)/debian/${COMMON_PKG}/usr/lib/bjlib

	touch $@

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

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	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 -X.txt
	dh_fixperms --exclude=canon_mfp2_net.ini
#	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 install configure
