#!/bin/bash
# Copyright (c) 2000-2022 Synology Inc. All rights reserved.

: "${TmpInstDir:?"TmpInstDir should be given outside"}"

WILL_REMOVE_INST_DIR="${TmpInstDir}_will_remove"

echo "making necessary directories..."
rm -rf "${WILL_REMOVE_INST_DIR}"
mkdir -p "${WILL_REMOVE_INST_DIR}"

env PYTHONHASHSEED=1 \
	./buildtools/bin/waf install --destdir="${WILL_REMOVE_INST_DIR}"

LIB_INSTALL_DIR="${TmpInstDir}/usr/lib"
LIB_SAMBA_INSTALL_DIR="${TmpInstDir}/usr/lib/samba"
BIN_INSTALL_DIR="${TmpInstDir}/usr/bin"

declare -a LIB_LIST=(
	tdb
	tevent
	talloc
	ldb
)

# install lib
mkdir -p "${LIB_INSTALL_DIR}"
mkdir -p "${LIB_SAMBA_INSTALL_DIR}"
for lib in ${LIB_LIST[*]}; do
	cp -a "${WILL_REMOVE_INST_DIR}/usr/lib/lib${lib}.so"* "${LIB_INSTALL_DIR}"
done
cp -a "${WILL_REMOVE_INST_DIR}/usr/lib/samba/libreplace-samba4.so" "${LIB_SAMBA_INSTALL_DIR}"
cp -a "${WILL_REMOVE_INST_DIR}/usr/lib/samba/ldb/" "${LIB_SAMBA_INSTALL_DIR}"
cp -a "${WILL_REMOVE_INST_DIR}/usr/lib/samba/libldb-"* "${LIB_SAMBA_INSTALL_DIR}"

#remove unused lib
rm "${LIB_SAMBA_INSTALL_DIR}/ldb/ildap.so"
rm "${LIB_SAMBA_INSTALL_DIR}/ldb/ldbsamba_extensions.so"

if checkPlatformFunction SYNO_FEA_HYPER_CONVERGED; then
	# create bin dir here, we only install binary if platform match.
	mkdir -p "${BIN_INSTALL_DIR}"
	cp -a "${WILL_REMOVE_INST_DIR}/usr/libexec/ctdb/ctdb_killtcp" "${BIN_INSTALL_DIR}"
fi

rm -rf ${WILL_REMOVE_INST_DIR}

if [[ -z "${NOSTRIP}" ]]; then
	while IFS= read -r -d '' file; do
		if [[ "$(file "${file}")" = *" ELF "*"not stripped"* ]]; then
			${STRIP} "${file}"
		fi
	done < <(find "${TmpInstDir}" -print0)
fi

# vim: ft=sh noexpandtab:
