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

: "${MakeClean:=yes}"
: "${CleanOnly:=no}"

if [[ ${MakeClean,,} = 'yes' ]]; then
	make distclean
fi

if [[ ${CleanOnly,,} = 'yes' ]]; then
	return
fi

CPPFLAGS="${CPPFLAGS} -DSYNO"
CLUSTER_OPT=""

# Design which CrossCompile.cache will be used
if [[ ${PLATFORM_FAMILY} = "SYNOPLAT_F_X86_64" ]]; then
	CrossCompile=synocache/CrossCompile.x64
elif Is64BitPlatform; then
	CrossCompile=synocache/CrossCompile.littleendian64
else
	CrossCompile=synocache/CrossCompile.littleendian
fi

if checkPlatformFunction SYNO_FEA_HYPER_CONVERGED; then
	echo "build with cluster"
	read -r CLUSTER_OPT <<- EOM
	--with-cluster-support \
	--nonshared-binary=ctdb_killtcp
	EOM
fi

env CC=${CC} LD=${LD} STRIP=${STRIP} RANLIB=${RANLIB} NM=${NM} AR=${AR}\
	CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" CPPPATH="${SysRootInclude}" LINKFLAGS="${LDFLAGS}" \
	CPPFLAGS="${CPPFLAGS}" \
	WAF_NO_PREFORK=1 \
	./configure \
		--cross-compile \
		--cross-answers=${CrossCompile} \
		--disable-rpath-install \
		--disable-symbol-versions \
		--enable-fhs \
		--enable-debug \
		--fatal-errors \
		--hostcc=gcc \
		--localstatedir=/var \
		--sysconfdir=/etc \
		--prefix=/usr \
		--with-system-mitkrb5 \
		--with-static-modules=!FORCED,!DEFAULT \
		--with-shared-modules=!FORCED,!DEFAULT \
		--without-ads \
		--with-ldap \
		--without-winbind \
		--without-pam \
		--without-ad-dc \
		--without-utmp \
		--without-libarchive \
		--without-ldb-lmdb \
		--without-gpgme \
		--without-regedit \
		--without-json \
		--without-acl-support \
		--disable-python \
		${CLUSTER_OPT} \
		;

if [ x"$?" != "x0" ]; then
	echo "Configure Error"
	exit -1
fi

env PYTHONHASHSEED=1 \
	./buildtools/bin/waf build
if ! [[ $? == 0 ]]; then
	echo "Build Error"
	exit 255
fi

exit 0

# vim: set ft=sh spell! noexpandtab:
