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

COMPAT_WIRELESS_VERSION="$(< VERSION)"

COMPAT_WIRELESS_VERSION_MAJOR=`echo $COMPAT_WIRELESS_VERSION | sed -e 's/^v*//' | cut -d. -f1`
COMPAT_WIRELESS_VERSION_MINOR=`echo $COMPAT_WIRELESS_VERSION | cut -d. -f2`

cd ${COMPAT_WIRELESS_VERSION}
case "$BUILD_TARGET" in
	EVANSPORT|MARVELL_ARMADAXP|MARVELL_ARMADA370|MARVELL_ARMADA375|MINDSPEED_COMCERTO2K|HISILICON_HI3535) # little-endian platforms
		# build staging rtl8712 wireless_modules
		RTL8712_SELECT=rtl8712
		SAVE_RTL8712_MODULES=save-rtl8712-modules
		;;
	PPC_QORIQ|PPC_853X|TI_816X|MARVELL_88F6281) # big-endian platforms
		# build Realtek official rtl8712 wireless_modules
		RTL8712_SELECT=ortl8712
		SAVE_RTL8712_MODULES=save-offical-rtl8712-modules
		;;
	*) # platforms not kernel 3.x and 2.6.32 should build backports
		SkipThisProject
		return
		;;
esac

case "$BUILD_TARGET" in
	MARVELL_ARMADAXP|MARVELL_ARMADA370|MARVELL_ARMADA375|MINDSPEED_COMCERTO2K)
		CFLAGS=""
		;;
esac

export KLIB_BUILD="$KSRC"
export ARCH=${ARCH}

case "$BUILD_TARGET" in
	X64|BROMOLOW|GRANTLEY|CEDARVIEW|AVOTON|BROMOLOWESM|BAYTRAIL|KVMX64|NEXTKVMX64)
		export CROSS_COMPILE=${ToolChainPrefix64}
		KERNEL_CC=${CC64}
		CFLAGS="-I/usr/local/x86_64-linux-gnu/include"
		;;
	*)
		export CROSS_COMPILE=${ToolChainPrefix}
		KERNEL_CC=${CC}
		;;
esac

. include/linux/syno_compat_wireless_def.config
if [ "$BUILD_TARGET" = "PPC_853X" ]; then
	export syno_flags="${syno_flags} -DSYNO_FIX_PPC853X_TRAMPOLINE_SETUP"
fi
export CFLAGS="${CFLAGS} ${syno_flags}"

if [ ${COMPAT_WIRELESS_VERSION_MAJOR} -eq 3 -a ${COMPAT_WIRELESS_VERSION_MINOR} -ge 10 ]; then
	cp -f synoconfigs/$BUILD_TARGET .config

	/usr/bin/make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ${MAKE_FLAGS}

	make -f Makefile.syno save-wireless-modules
	make -f Makefile.syno save-bt-modules
	case "$BUILD_TARGET" in
		X64|BROMOLOW|GRANTLEY|CEDARVIEW|EVANSPORT|MARVELL_ARMADAXP|MARVELL_ARMADA370|MARVELL_ARMADA375|AVOTON|MINDSPEED_COMCERTO2K|BROMOLOWESM|BAYTRAIL|HISILICON_HI3535|KVMX64|NEXTKVMX64)  # little-endian platforms
			# replace rtl8712 wireless module with staging version
			# The original driver is totally unusable for little-endian platforms
			make -f Makefile.syno save-staging-rtl8712-module
		;;
		PPC_QORIQ|MARVELL_88F6281) # linux-2.6.32
			make -f Makefile.syno save-official-rtl8712-module
		;;
		BROADCOM_NORTHSTARPLUS)
		;;
	esac

	# 		 x64 	|  non-x64
	# 8192-staging 	 o	|  o
	# 8192-official  x	|  o
	case "$BUILD_TARGET" in
		X64|BROMOLOW|GRANTLEY|CEDARVIEW|AVOTON|KVMX64|NEXTKVMX64)  # x64
			;;
		*)
			make -f Makefile.syno save-official-rtl8192-module
			;;
	esac

	local driver_list="all"
	case "$BUILD_TARGET" in
		PPC_853X|PPC_QORIQ)
			# remove: rtl8812au rtl8821au
			driver_list="rt2x00 rtl8192cu rtl8187 rtl8188eu rtl8712 carl9170 ath9k zd1211rw"
			;;
		*)
			;;
	esac

	./scripts/synoscript create-table "${driver_list}"
	./scripts/synoscript create-bt-table "all"

	cd ../
	exit
fi

./scripts/synoscript restore

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

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

# build rt2x00 module
./scripts/driver-select rt2x00
/usr/bin/make ${MAKE_FLAGS} CC="${KERNEL_CC}"
/usr/bin/make ${MAKE_FLAGS} save-rt2x00-modules
/usr/bin/make ${MAKE_FLAGS} save-common-modules

# build rtlwifi modules
./scripts/synoscript restore
./scripts/driver-select rtlwifi
make ${MAKE_FLAGS} CC="${KERNEL_CC}"
make ${MAKE_FLAGS} save-rtlwifi-modules

# build rtl818x modules
./scripts/synoscript restore
./scripts/driver-select rtl818x
make ${MAKE_FLAGS} CC="${KERNEL_CC}"
make ${MAKE_FLAGS} save-rtl818x-modules

# build ath9k wireless_modules
./scripts/synoscript restore
./scripts/driver-select ath9k
make ${MAKE_FLAGS} CC="${KERNEL_CC}"
make ${MAKE_FLAGS} save-ath9k-modules

# build carl9170 wireless_modules
./scripts/synoscript restore
./scripts/driver-select carl9170
/usr/bin/make ${MAKE_FLAGS} CC="${KERNEL_CC}"
/usr/bin/make ${MAKE_FLAGS} save-carl9170-modules

# build rtl8712 wireless_modules
./scripts/synoscript restore
./scripts/driver-select ${RTL8712_SELECT}
/usr/bin/make ${MAKE_FLAGS} CC="${KERNEL_CC}"
/usr/bin/make ${MAKE_FLAGS} ${SAVE_RTL8712_MODULES}

# build official rtl8192 wireless_modules
./scripts/synoscript restore
./scripts/driver-select rtl8192
/usr/bin/make ${MAKE_FLAGS} CC="${KERNEL_CC}"
/usr/bin/make ${MAKE_FLAGS} save-offical-rtl8192-modules

# build zd1211rw wireless_modules
./scripts/synoscript restore
./scripts/driver-select zd1211rw
/usr/bin/make ${MAKE_FLAGS} CC="${KERNEL_CC}"
/usr/bin/make ${MAKE_FLAGS} save-zd1211rw-modules

#build bt modules
./scripts/synoscript restore
./scripts/driver-select bt
/usr/bin/make ${MAKE_FLAGS} CC="${KERNEL_CC}"
/usr/bin/make ${MAKE_FLAGS} save-bt-modules

./scripts/synoscript delete
./scripts/synoscript create-table "all"
./scripts/synoscript create-bt-table "all"

cd ../
