#!/usr/bin/make -f

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -*- makefile -*-

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

%:
	dh $@

hadoop_version=${HADOOP_BASE_VERSION}

ifeq (${DEB_BUILD_ARCH},amd64)
  native_dir=Linux-amd64-64
endif
ifeq (${DEB_BUILD_ARCH},i386)
  native_dir=Linux-i386-32
endif

override_dh_auto_build:
	env HADOOP_VERSION=${hadoop_version} HADOOP_ARCH=${native_dir} \
        bash debian/do-component-build -Divy.home=${HOME}/.ivy2

hadoop_svcs=hdfs-namenode hdfs-secondarynamenode hdfs-datanode hdfs-zkfc hdfs-journalnode hdfs-dfsrouter \
            yarn-resourcemanager yarn-nodemanager yarn-proxyserver yarn-timelineserver yarn-router \
            mapreduce-historyserver httpfs kms

$(hadoop_svcs): debian/init.d.tmpl
	bash $< debian/hadoop-$@.svc deb debian/hadoop-$@.init
	cp debian/$(firstword $(subst -, ,$@)).default debian/tmp/etc/default/hadoop-$@
	echo /etc/default/hadoop-$@ >> debian/hadoop-$@.install
	# FIXME: workaround for BIGTOP-105
	[ -f debian/hadoop-$@.postinst ] || cp debian/hadoop.daemon.postinst.tpl debian/hadoop-$@.postinst
	sed -i -e "s|@HADOOP_DAEMON@|$@|" debian/hadoop-$@.postinst

override_dh_auto_install:
	env HADOOP_VERSION=${hadoop_version} \
	bash debian/install_hadoop.sh \
	  --prefix=debian/tmp/ \
	  --distro-dir=debian \
	  --build-dir=${CURDIR}/build \
	  --system-lib-dir=/usr/lib/ \
	  --system-include-dir=/usr/include \
	  --doc-dir=/usr/share/doc/hadoop-doc \
	  --man-dir=/usr/share/man
	# Forcing Zookeeper dependency to be on the packaged jar
	ln -sf /usr/lib/zookeeper/zookeeper.jar debian/tmp/usr/lib/hadoop/lib/zookeeper-[[:digit:]]*.jar
	ln -sf /usr/lib/zookeeper/zookeeper-jute.jar debian/tmp/usr/lib/hadoop/lib/zookeeper-jute-[[:digit:]]*.jar
	# Workaround for BIGTOP-583
	rm -f debian/tmp/usr/lib/hadoop-*/lib/slf4j-log4j12-*.jar
	# FIXME: BIGTOP-463
	# mkdir -p debian/tmp/etc/default
	# Refactored from install-arch
	cp debian/hadoop-fuse.default debian/tmp/etc/default/hadoop-fuse
	mkdir -p debian/tmp/etc/security/limits.d
	cp debian/hdfs.conf debian/yarn.conf debian/mapreduce.conf debian/tmp/etc/security/limits.d

override_dh_install: $(hadoop_svcs)
	dh_install
	# Drop misc fuse_dfs directories
	rm -Rf debian/hadoop/usr/lib/hadoop/bin/fuse_dfs
	rm -Rf debian/hadoop/usr/lib/hadoop/contrib/fuse-dfs
	rm -Rf debian/hadoop/usr/lib/hadoop/hdfs/contrib/fuse-dfs

override_dh_strip:
	dh_strip --no-automatic-dbgsym

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

override_dh_strip_nondeterminism:
