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

case ${MakeClean} in
	[Yy][Ee][Ss])
		if [ -f Makefile.conf ] && [ ! -f "syno_gpl_build" ]; then
			make clean
		fi
		;;
esac

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

SupportCards="usb-audio"

case "$BUILD_TARGET" in
	X64|BROMOLOW|GRANTLEY|CEDARVIEW|AVOTON|DOCKERX64|BRASWELL|APOLLOLAKE|BROADWELL|GEMINILAKE|GEMINILAKENK|\
	HISILICON_HI3535|ALPINE|KVMX64|KVMX64SOFS|KVMX64V2|MARVELL_ARMADA38X|REALTEK_RTD1296|DENVERTON|HISILICON_HI3536|\
	BROADWELLNK|MARVELL_ARMADA37XX|PURLEY|COFFEELAKE|NEXTKVMX64|REALTEK_RTD1619|SKYLAKED|V1000|V1000NK|V1000SOFS|\
	KVMCLOUD|EPYC7002|EPYC7002SOFS|ICELAKED|REALTEK_RTD1619B|REALTEK_RTD1619B_MANGO|R1000|R1000NK|BROADWELLNKV2|RYZEN5K|GEMINILAKEVS|EPYC7003NTB|RAPTORLAKES|EPYC7003|RAPTORLAKEU)
		echo "This platform($BUILD_TARGET) use kernel built-in alsa driver"
		return
		;;
	PPC_853X)
		. $ScriptsDir/include/platform.ppc853x
		;;
	PPC_QORIQ)
		. $ScriptsDir/include/platform.qoriq
		;;
	MARVELL_88F6281)
		. $ScriptsDir/include/platform.6281
		CFLAGS+=" -D__LINUX_ARM_ARCH__=5"
		;;
	MARVELL_ARMADAXP)
		. $ScriptsDir/include/platform.armadaxp
		CFLAGS+=" -D__LINUX_ARM_ARCH__=7"
		;;
	MARVELL_ARMADA370)
		. $ScriptsDir/include/platform.armada370
		CFLAGS+=" -D__LINUX_ARM_ARCH__=7"
		;;
	MARVELL_ARMADA375)
		. $ScriptsDir/include/platform.armada375
		CFLAGS+=" -D__LINUX_ARM_ARCH__=7"
		;;
	EVANSPORT)
		. $ScriptsDir/include/platform.evansport
		;;
	MINDSPEED_COMCERTO2K)
		. $ScriptsDir/include/platform.comcerto2k
		CFLAGS+=" -D__LINUX_ARM_ARCH__=7"
		;;
	*)
		echo "This platform($BUILD_TARGET) is not supported."
		return
		;;
esac

KernelDir="$KSRC"

eval `grep "^VERSION" ${KernelDir}/Makefile | sed 's, ,,g'`
eval `grep "^PATCHLEVEL" ${KernelDir}/Makefile | sed 's, ,,g'`
eval `grep "^SUBLEVEL" ${KernelDir}/Makefile | sed 's, ,,g'`

KMAJORVERSION=${VERSION}
KMINORVERSION=${PATCHLEVEL}
KSUBVERSION=${SUBLEVEL}
ALSA_KAVERSION="${KMAJORVERSION}:${KMINORVERSION}:${KSUBVERSION}:"

env CC="${CC}" CFLAGS="${CFLAGS}" LD="${LD}" AR=${AR} STRIP=${STRIP} RANLIB=${RANLIB} NM=${NM} ALSA_KAVERSION=${ALSA_KAVERSION} \
./configure.syno \
	--with-cross=`dirname ${ToolChainPrefix}`/ \
	--with-kernel=${KernelDir} \
	--with-build=${KernelDir} \
	--without-redhat \
	--without-suse \
	--with-oss=yes \
	--with-cards=${SupportCards} \
	--with-card-options=all \
	${ConfigOpt}

make  CC="${CC}" LD="${LD}" AR=${AR} STRIP=${STRIP} RANLIB=${RANLIB} NM=${NM} ${MAKE_FLAGS}
