#!/bin/bash
# Copyright (c) 2000-2015 Synology Inc. All rights reserved.
SYSTEMD_SCRIPT_PATH=${TmpInstDir}/usr/syno/lib/systemd/scripts
SYSTEMD_UNIT_PATH=${TmpInstDir}/usr/lib/systemd/system
USR_LIB_CEPH=${TmpInstDir}/usr/lib/ceph
VAR_LOG_CEPH=${TmpInstDir}/var/log/ceph
ETC_LOGROTATE=${TmpInstDir}/etc/logrotate.d/

remove_no_need_files() {
	local TmpInstDir=$1

	while IFS= read FILE; do
		[ ! -f "$FILE" ] && continue

		target="${TmpInstDir}/${FILE}"
		echo " - deleting no need file: $target"
		rm -r $target || exit 1
	done <<< "
usr/bin/ceph-authtool
usr/bin/ceph-dencoder
usr/bin/ceph-objectstore-tool
usr/bin/ceph-monstore-tool
usr/bin/ceph-kvstore-tool
usr/bin/ceph-fuse
usr/bin/ceph-osdomap-tool
usr/bin/ceph-syn
usr/bin/ceph-conf
usr/bin/rbd-mirror
usr/bin/rbd-fuse
usr/bin/rbd-nbd
usr/bin/rbd-replay
usr/bin/rbd-replay-many
usr/lib/ceph/crypto/libceph_crypto_isal.so
usr/lib/ceph/crypto/libceph_crypto_isal.so.1
usr/lib/ceph/crypto/libceph_crypto_isal.so.1.0.0
usr/share/ceph/id_rsa_drop.ceph.com
usr/share/ceph/id_rsa_drop.ceph.com.pub
usr/share/ceph/known_hosts_drop.ceph.com
usr/share/doc
"
}

always_strip_tools() {
	local TmpInstDir=$1
	while IFS= read TOOL; do
		[ ! -f "$TOOL" ] && continue

		target="${TmpInstDir}/${TOOL}"
		file $target | grep 'not stripped' > /dev/null
		if [ "$?" != "0" ]; then
			echo " - no need strip: $target"
		else
			echo " - strip: $target"
			$STRIP $target || exit 1
		fi
	done <<< "
usr/bin/monmaptool
usr/bin/crushtool
usr/bin/osdmaptool
usr/bin/ceph-bluestore-tool
usr/bin/cephfs-table-tool
usr/bin/cephfs-journal-tool
usr/bin/cephfs-data-scan
usr/bin/rbd
usr/bin/rados
usr/bin/ceph-immutable-object-cache
usr/lib/librbd.so.1.17.0
usr/lib/rados-classes/libcls_rbd.so.1.0.0
usr/lib/rados-classes/libcls_refcount.so.1.0.0
usr/lib/rados-classes/libcls_lua.so.1.0.0
usr/lib/rados-classes/libcls_queue.so.1.0.0
usr/lib/ceph/erasure-code/libec_jerasure.so
usr/lib/ceph/erasure-code/libec_jerasure_sse4.so
usr/lib/ceph/erasure-code/libec_jerasure_sse3.so
usr/lib/ceph/erasure-code/libec_jerasure_generic.so
usr/lib/ceph/erasure-code/libec_isa.so
usr/lib/ceph/erasure-code/libec_lrc.so
usr/lib/ceph/erasure-code/libec_shec.so
usr/lib/ceph/erasure-code/libec_shec_generic.so
usr/lib/ceph/erasure-code/libec_shec_sse3.so
usr/lib/ceph/erasure-code/libec_shec_sse4.so
usr/lib/ceph/compressor/libceph_zlib.so.2.0.0
"
}

if ! checkPlatformFunction SYNO_FEA_HYPER_CONVERGED; then
        SkipThisProject
        return
fi

LOGROTATE_MD5=$(md5sum src/logrotate.conf | awk '{print $1}')
if [ "$LOGROTATE_MD5" != "5a083296cc46a65760bf622d53f6f42f" ]; then
        echo "upstream src/logrotate.conf has been changed ($LOGROTATE_MD5), please review it!!!"
        exit 1
fi

pushd build
make DESTDIR=$TmpInstDir install
popd

rm -rf ${TmpInstDir}${IncludeDir}

[ -d ${VAR_LOG_CEPH} ] || install -d ${VAR_LOG_CEPH}

[ -d ${ETC_LOGROTATE} ] || install -d ${ETC_LOGROTATE}
install -m 644 synology/logrotate/logrotate.conf ${ETC_LOGROTATE}/ceph
[ -d ${SYSTEMD_SCRIPT_PATH} ] || install -d ${SYSTEMD_SCRIPT_PATH}
install -m 700 synology/systemd/ceph-time-wait-sync.sh ${SYSTEMD_SCRIPT_PATH}

remove_no_need_files ${TmpInstDir}
always_strip_tools ${TmpInstDir}

if [ "x$NOSTRIP" != "xNOSTRIP" ]
then
	for filename in $(find $TmpInstDir)
	do
		file $filename | grep 'not stripped' > /dev/null
		if [ "$?" == "0" ]
		then
			$STRIP $filename
		fi
	done
fi
