#!/usr/bin/make -f

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

DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_MULTIARCH := $(shell dpkg-architecture -qDEB_BUILD_MULTIARCH)

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f2 -d' ')
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_VERSION).$(DIST_RELEASE)
SUBSTVARS_BINARY_VERSION := -Vbinary:Version="$(PKG_VERSION_TAG)"
SUBSTVARS_SOURCE_VERSION := -Vsource:Version="$(PKG_VERSION_TAG)"

# Detect if libncurses5 is available, newer distro won't have it
HAS_LIBNCURSES5 := $(shell dpkg -s libncurses5 >/dev/null 2>&1 && echo yes || echo no)

# Path to libncurses5 package
LIBNCURSES5_DEB_PATH := $(CURDIR)/debian/libncurses5/libncurses5_6.4-4_${DEB_BUILD_ARCH}.deb

# Filename of libcurses inside package
LIBNCURSES5_FILENAME := /lib/$(DEB_BUILD_MULTIARCH)/libncurses.so.5.9
LIBNCURSES5_LINK := /lib/$(DEB_BUILD_MULTIARCH)/libncurses.so.5

# Path to libtinfo5 package
LIBTINFO5_DEB_PATH := $(CURDIR)/debian/libncurses5/libtinfo5_6.4-4_${DEB_BUILD_ARCH}.deb

# Filename of libtino inside package
LIBTINFO5_FILENAME := /lib/$(DEB_BUILD_MULTIARCH)/libtinfo.so.5.9
LIBTINFO5_LINK := /lib/$(DEB_BUILD_MULTIARCH)/libtinfo.so.5

build: build-arch build-indep
build-arch:
build-indep:

clean:
	dh_testdir
	dh_testroot
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	install $(CURDIR)/debian/megacli.wrapper $(CURDIR)/debian/megacli/usr/sbin/megacli
ifeq ($(DEB_BUILD_ARCH),amd64)
	install $(CURDIR)/opt/MegaRAID/MegaCli/MegaCli64 $(CURDIR)/debian/megacli/usr/lib/megacli/megacli.real
	install $(CURDIR)/opt/MegaRAID/MegaCli/libstorelibir-2.so.14.07-0 $(CURDIR)/debian/megacli/usr/lib/megacli/libstorelibir-2.so.14.07-0
endif
ifeq ($(DEB_BUILD_ARCH),i386)
	install $(CURDIR)/opt/MegaRAID/MegaCli/MegaCli $(CURDIR)/debian/megacli/usr/lib/megacli/megacli.real
	# Seem to be x64 only
	#install $(CURDIR)/opt/MegaRAID/MegaCli/libstorelibir-2.so.14.07-0 $(CURDIR)/debian/megacli/usr/lib/megacli/libstorelibir-2.so.14.07-0
endif
ifeq ($(HAS_LIBNCURSES5),no)
	@echo "###"
	@echo "# Applying workaround due to libncurses5 package not being available anymore"
	@echo "###"
	@echo $(LIBNCURSES5_DEB_PATH)
	dpkg-deb --fsys-tarfile $(LIBNCURSES5_DEB_PATH) | tar -xf - --strip-components=3 -C $(CURDIR)/debian/megacli/usr/lib/megacli/ .$(LIBNCURSES5_FILENAME) .$(LIBNCURSES5_LINK)
	dpkg-deb --fsys-tarfile $(LIBTINFO5_DEB_PATH) | tar -xf - --strip-components=3 -C $(CURDIR)/debian/megacli/usr/lib/megacli/ .$(LIBTINFO5_FILENAME) .$(LIBTINFO5_LINK)
endif

binary-indep:
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 8.07.14_MegaCLI.txt
	dh_installdocs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps -l/usr/lib/megacli
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SUBSTVARS_BINARY_VERSION) $(SUBSTVARS_SOURCE_VERSION)
	dh_md5sums
	dh_builddeb

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