ATF_PLAT="sun50i_a64"
source "${BASH_SOURCE%/*}/include/sunxi64_common.inc"
OVERLAY_PREFIX='sun50i-h5'
[[ -z $CPUMIN ]] && CPUMIN=480000
[[ -z $CPUMAX ]] && CPUMAX=1368000
GOVERNOR=ondemand
ASOUND_STATE='asound.state.sun50iw2-current'

[[ $RELEASE == focal ]] && MODULES_BLACKLIST_CURRENT="lima"

case $BRANCH in
	legacy)

		LINUXFAMILY=sun50iw2

		KERNELBRANCH="branch:orange-pi-3.10-sun50iw2"
		KERNELPATCHDIR=${BOARDFAMILY}-${BRANCH}
		KERNEL_COMPILER="aarch64-linux-gnu-"
		KERNEL_USE_GCC='> 3.0'

		BOOTCONFIG="sun50iw2p1_config"
		BOOTBRANCH='branch:v2014.07-sun50iw2-linux3.10'
		UBOOT_TARGET_MAP=';;u-boot.bin:u-boot.fex
		                  spl;;boot0_sdcard.bin:boot0_sdcard.fex'
		UBOOT_COMPILER="arm-linux-gnueabi-"
		UBOOT_USE_GCC='> 4.0'
		BOOTENV_FILE=sun50iw2-legacy.txt
		BOOTSCRIPT='boot-sun50iw2-legacy.cmd:boot.cmd'
		PACK_UBOOT="yes"
		OFFSET=20
		ATFSOURCE=""
		ATF_COMPILE="no"
		INITRD_ARCH=arm
		ASOUND_STATE='asound.state.sun50iw2-legacy'
		BOOT_FDT_FILE=sun50i-h5-orangepi-${BOARD:8}.dtb
        ;;

	current)

		if [[ $BOARD =~ orangepipch5|orangepioneh5 ]]; then

			KERNELBRANCH="branch:orange-pi-5.10"
			LINUXCONFIG=linux-5.10-sunxi64-current
		fi
	;;
esac

family_tweaks_s()
{

	if [[ -f $SDCARD/lib/systemd/system/ap6212-bluetooth.service ]]; then

		# install and enable Bluetooth
		chroot $SDCARD /bin/bash -c "apt-get -y -qq install rfkill bluetooth bluez bluez-tools >/dev/null 2>&1"
		chroot $SDCARD /bin/bash -c "systemctl --no-reload enable ap6212-bluetooth.service >/dev/null 2>&1"
        fi

        if [[ -f $SDCARD/lib/systemd/system/rtl8723bs-bluetooth.service ]]; then

		# install and enable Bluetooth
		chroot $SDCARD /bin/bash -c "apt-get -y -qq install rfkill bluetooth bluez bluez-tools >/dev/null 2>&1"
		chroot $SDCARD /bin/bash -c "systemctl --no-reload enable rtl8723bs-bluetooth.service >/dev/null 2>&1"
	fi

        if [[ $BOARD == orangepizeroplus2h5 && $BRANCH == legacy ]]; then

		chroot $SDCARD /bin/bash -c "apt-get -y -qq install rfkill bluetooth bluez bluez-tools >/dev/null 2>&1"

		cat <<-EOF > "${SDCARD}"/etc/rc.local
		#!/bin/sh -e
		#
		# rc.local
		#
		# This script is executed at the end of each multiuser runlevel.
		# Make sure that the script will "exit 0" on success or any other
		# value on error.
		#
		# In order to enable or disable this script just change the execution
		# bits.
		#
		# By default this script does nothing.

		/usr/bin/zeroplus2-bt.sh

		exit 0
		EOF
		chmod +x "${SDCARD}"/etc/rc.local
        fi
}

family_tweaks_bsp()
{

	if [[ $BOARD == orangepizeroplus2h5 ]]; then

		install -m 755 $EXTER/packages/blobs/bt/brcm_patchram_plus/brcm_patchram_plus_$ARCH $destination/usr/bin/brcm_patchram_plus

		if [[ $BRANCH == current ]]; then
			cp $EXTER/packages/bsp/sunxi/ap6212-bluetooth.service $destination/lib/systemd/system/
		fi

		if [[ $BRANCH == legacy ]]; then
			cp ${EXTER}/packages/bsp/sunxi/ap6212.conf $destination//etc/modprobe.d/
			install -m 755 $EXTER/packages/bsp/sunxi/zeroplus2-bt-legacy.sh $destination/usr/bin/zeroplus2-bt.sh
		fi
	fi

	if [[ $BOARD == orangepiprime ]]; then

		install -m 755 $EXTER/packages/blobs/bt/hciattach/rtk_hciattach_$ARCH $destination/usr/bin/rtk_hciattach
		cp $EXTER/packages/bsp/sunxi/rtl8723bs-bluetooth.service $destination/lib/systemd/system/
	fi
}
