#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=-all
export CFLAGS =
export CXXFLAGS =
export LDFLAGS =

PNAME        := mrsh
VERSION      := 2.12
INSTALL_PATH := /opt/ohpc/admin/mrsh
SRCDIR       := $(CURDIR)/$(PNAME)-$(VERSION)
PKG          := $(CURDIR)/debian/$(PNAME)-ohpc

%:
	dh $@

override_dh_auto_configure:
	test -s SOURCES/$(PNAME)-$(VERSION).tar.gz || \
		wget -q -O SOURCES/$(PNAME)-$(VERSION).tar.gz \
			https://github.com/chaos/mrsh/archive/$(VERSION).tar.gz
	rm -rf $(SRCDIR)
	tar xf SOURCES/$(PNAME)-$(VERSION).tar.gz
	cd $(SRCDIR) && patch -p1 < $(CURDIR)/SOURCES/mrlogin-Don-t-use-union-wait.patch || true
	cd $(SRCDIR) && patch -p1 < $(CURDIR)/SOURCES/Add-force-to-libtoolize.patch || true
	cd $(SRCDIR) && ./autogen.sh
	cd $(SRCDIR) && cp -f /usr/share/misc/config.guess /usr/share/misc/config.sub config/

override_dh_auto_build:
	cd $(SRCDIR) && ./configure --prefix=$(INSTALL_PATH) && \
		$(MAKE) -j$$(nproc)

override_dh_auto_install:
	cd $(SRCDIR) && $(MAKE) DESTDIR=$(PKG) install
	find $(PKG) -name '*.a' -delete
	find $(PKG) -name '*.la' -delete
	# Create symlinks in /usr/bin
	mkdir -p $(PKG)/usr/bin
	ln -sf $(INSTALL_PATH)/bin/mrcp $(PKG)/usr/bin/mrcp
	ln -sf $(INSTALL_PATH)/bin/mrsh $(PKG)/usr/bin/mrsh
	ln -sf $(INSTALL_PATH)/bin/mrlogin $(PKG)/usr/bin/mrlogin

override_dh_auto_clean:
	rm -rf $(SRCDIR)

override_dh_usrlocal:

override_dh_shlibdeps:
	dh_shlibdeps -l$(PKG)$(INSTALL_PATH)/lib -- --ignore-missing-info || true

override_dh_strip:
	dh_strip --no-automatic-dbgsym || true

override_dh_auto_test:
