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

case ${MakeClean} in
	[Yy][Ee][Ss])
		make distclean
		;;
esac

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

# CC_FOR_BUILD: "CC" toolchain for build machine (chroot)
#	It should be host's cc, not cross compiler $CC.
#	The reasons why we use $CC are described below:
#
#	locktest and rpcgen use $CC_FOR_BUILD for compiler
#	so they should use $CFLAGS_FOR_BUILD for cflags.
#
#	BUT actually they use $CFLAGS which are flags for cross compiler and
#	this error causes build failed in our system.
#
#	The correct way to fix it is modified the Makefile.am
#	and redo the autotools. There's a patch for this:
#	https://patchwork.kernel.org/patch/2327291/
#
#	But we don't want to redo the autotools and we won't use testlock and rpcgen.
#	So here we just use cross compiler to build it as our workaround.
CC_FOR_BUILD="${CC}"

ConfigOpt+=" --with-sysroot=${SysRootPrefix}"
ConfigOpt+=" --prefix=/usr"

# Use Kerberos v5 installation in $SysRootPrefix
ConfigOpt+=" --with-krb5=${SysRootPrefix}"

# with-gssglue: use gssapi, libgssglue
#	let Kerberos have better compatibility
ConfigOpt+=" --with-gssglue"

# enable-ipv6: DSM NFS support ipv6
ConfigOpt+=" --enable-ipv6"

# enable-tirpc: '--enable-ipv6' requires TIRPC support
# with-tirpcinclude: give include path clearly
ConfigOpt+=" --enable-tirpc"
ConfigOpt+=" --with-tirpcinclude=${SysRootInclude}/tirpc"

# enable nfsv4 and ldap
ConfigOpt+=" --enable-nfsv4"
ConfigOpt+=" --enable-ldap"

# disable-nfsv41: do not build blkmapd of pNFS client
#	blkmapd need glibc scsi/scsi.h, will wrongly get kernel scsi/scsi.h in tool chain
ConfigOpt+=" --disable-nfsv41"

# enable-svcgss: build svcgssd for kerberos
ConfigOpt+=" --enable-svcgss"

# enable-gss: build gssd for kerberos
ConfigOpt+=" --enable-gss"

# disable-uuid: avoid buggy libblkid.
ConfigOpt+=" --disable-uuid"

# enable-mount: build mount.nfs
#	mount is busybox version, we need to install mount.nfs into system
ConfigOpt+=" --enable-mount"

# disable-mountconfig: do not use mount config file that auto fills options
ConfigOpt+=" --disable-mountconfig"

# disable-nfsdcltrack: disable NFSv4 clientid tracking programs
ConfigOpt+=" --disable-nfsdcltrack"

# diable-caps: Disable Linux capabilities support
#	<Build System> #688 - fix random build failed of nfs-utils-1.2.x
ConfigOpt+=" --disable-caps"

# disable-kprefix: do not need k in "rpc.knfsd"
ConfigOpt+=" --disable-kprefix"

# The following option use default value
# disable-libmount-mount
# disable-osdlogin
# enable-largefile
# disable-gums
# enable-shared
# disable-share to avoid dynamic link error
# enable-static
# enable-fast-install
# with-statedir="/var/lib/nfs"
# with-aix-soname=aix

LDFLAGS+=" -llber"

env CC="${CC}" \
	CFLAGS="${CFLAGS}" \
	CPP="${CC} -E" \
	CPPFLAGS="${CFLAGS}" \
	CXX="${CXX}" \
	CXXFLAGS="${CFLAGS}" \
	LDFLAGS="${LDFLAGS}" \
	CC_FOR_BUILD="${CC_FOR_BUILD}" \
	LDFLAGS_FOR_BUILD="${LDFLAGS_FOR_BUILD}" \
	CFLAGS_FOR_BUILD="${CFLAGS_FOR_BUILD}" \
	CPPFLAGS_FOR_BUILD="${CPPFLAGS_FOR_BUILD}" \
	CXXFLAGS_FOR_BUILD="${CXXFLAGS_FOR_BUILD}" \
	LD="${LD}" \
	AR="${AR}" \
	STRIP="${STRIP}" \
	RANLIB="${RANLIB}" \
	OBJDUMP="${ToolChainPrefix}objdump" \
	NM="${NM}" \
	./configure ${ConfigOpt}

# Build nfs-utils first
make $MAKE_FLAGS

# Build shared library for our projects
cd synology
make $MAKE_FLAGS