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

case ${MakeClean} in
	[Yy][Ee][Ss])
		make distclean
		rm -rf synosmb/synoconf.h
		;;
esac

case ${CleanOnly} in
	[Yy][Ee][Ss])
		return
		;;
esac

REQUIRES="libsynosdk libsynorecycle synologd libsynoprintergpl libsynofileop libsynoacl libsynohacore libsynonetsdk synocontentsearchutils libplist sqlite3 synospotlightutils libsynosharesnapshot libsynoaccountdb libsynosysnotify"
CPPFLAGS="${CPPFLAGS} -DSYNO $(pkg-config --cflags ${REQUIRES})"
LDFLAGS="${LDFLAGS} $(pkg-config --libs ${REQUIRES})"


# Design which CrossCompile.cacha will be used
if Is64BitPlatform; then
	CrossCompile=synocache/CrossCompile.littleendian64
else
	case $PLATFORM_FAMILY in
	SYNOPLAT_F_PPC)
		echo "powerpc platform, use bigendian config"
		CrossCompile=synocache/CrossCompile.bigendian
		;;
	*)
		## Default assume the little endian
		CrossCompile=synocache/CrossCompile.littleendian
		;;
	esac
fi

if checkPlatformFunction SYNO_PLAT_NO_SYNOTIFY; then
	echo "build with -DSYNOSMB_TRIG_SYNOTIFY"
	CPPFLAGS+=" -DSYNOSMB_TRIG_SYNOTIFY"
fi

if checkPlatformFunction SYNO_PLAT_SPLICE_DMA; then
	echo "build with -DSYNOSMB_SPLICE_DMA"
	CPPFLAGS+=" -DSYNOSMB_SPLICE_DMA"
fi

## Set the build nmber into synoconf.h
echo "#define SYNO_BUILD_NUMBER $DSM_BUILD_NUM" > synosmb/synoconf.h

PAM_DIR="/usr/lib/security"
PRIVATEDIR="/usr/lib/samba"

PRIVATE_DIR="/etc/samba/private"
LOGFILEBASE="/var/log"
CONFIG_DIR="/etc/samba"
env CC=${CC} LD=${LD} STRIP=${STRIP} RANLIB=${RANLIB} NM=${NM} AR=${AR}\
	CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" CPPPATH="${SysRootInclude}" LINKFLAGS="${LDFLAGS}" \
	CPPFLAGS="${CPPFLAGS}" \
	PRIVATE_DIR=${PRIVATE_DIR} LOGFILEBASE=${LOGFILEBASE} \
	SynoLibDir=${SysRootLib} SynoIncludeDir=${SysRootInclude} SynoArch=${BUILD_ARCH} \
	./configure \
		--hostcc=gcc \
		--prefix=/usr \
		--sbindir=/usr/bin \
		--enable-fhs \
		--disable-rpath-install \
		--cross-compile \
		--cross-answers=${CrossCompile} \
		--with-pam \
		--without-utmp \
		--enable-debug \
		--fatal-errors \
		--enable-selftest \
		--with-ads \
		--with-ldap \
		--with-winbind \
		--with-statedir=/var/lib/samba \
		--with-privatedir=${PRIVATE_DIR} \
		--with-lockdir=/var/cache/samba \
		--with-sockets-dir=/run/samba \
		--with-piddir=/run/samba \
		--with-configdir=${CONFIG_DIR} \
		--with-pammodulesdir=${PAM_DIR} \
		--localstatedir=/var \
		--disable-symbol-versions \
		--with-privatelibdir=${PRIVATEDIR} \
		--without-libarchive \
		--enable-spotlight \
		2>&1 | tee config.log

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

make ${MAKE_FLAGS}

# vim: set ft=sh:
# vim: set spell!:
