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

# Build configuration -- override with environment variables
SHELL := /bin/bash
HADOOP_HOME ?= /usr/lib/hadoop
PYTHON_PATH ?= $(shell command -v python)
# This will pick up a python3 dev environment, which is not yet provided by bigtop 
# PYTHON3_PATH ?= $(shell command -v python3)

BIN_DIR ?= /usr/bin
LIB_DIR ?= /usr/lib/qfs
ETC_DIR ?= /etc/qfs
INCLUDE_DIR ?= /usr/include
DATA_DIR ?= /usr/share/qfs
VAR_DIR ?= /var

SERVICES ?= metaserver chunkserver webui

%:
	dh $@

override_dh_auto_build:
	bash debian/do-component-build \
		--qfs-version=$(QFS_BASE_VERSION) \
		--python=$(PYTHON_PATH)

override_dh_auto_install:
	bash -x debian/install_qfs.sh \
		--source-dir=debian \
		--prefix=debian/tmp \
		--qfs-version=$(QFS_BASE_VERSION) \
		--python=$(PYTHON_PATH) \
		--python-extra='--install-layout=deb' \
		--bin-dir=$(BIN_DIR) \
		--lib-dir=$(LIB_DIR) \
		--etc-dir=$(ETC_DIR) \
		--include-dir=$(INCLUDE_DIR) \
		--data-dir=$(DATA_DIR) \
		--var-dir=$(VAR_DIR)

	for service in $(SERVICES); do \
		initd_dir=debian/qfs-$$service/etc/init.d ; \
		mkdir -p $$initd_dir ; \
		bash debian/init.d.tmpl debian/qfs-$$service.svc deb $$initd_dir/qfs-$$service ; \
	done

override_dh_shlibdeps:
	dh_shlibdeps -l$(shell pwd)/build/release/lib

override_dh_fixperms:
	dh_fixperms
	# executable-not-elf-or-script
	# upstream qfs has executable gifs -- later version fixes but hasn't been
	# released yet
	chmod -c a-x debian/qfs-webui/usr/share/qfs/webui/files/sorttable/*.gif
