source "${BASH_SOURCE%/*}/include/sunxi_common.inc"

if [[ -z ${OVERLAY_PREFIX} ]]; then
	OVERLAY_PREFIX='sun8i-h3'
fi

[[ -z $CPUMIN ]] && CPUMIN=480000
[[ -z $CPUMAX ]] && CPUMAX=1400000

family_tweaks_s()
{

        if [[ -f $SDCARD/lib/systemd/system/ap6212-bluetooth.service && $BRANCH == current ]]; 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 [[ $BOARD == orangepizeroplus2h3 && $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_s()
{

	if [[ $BOARD == orangepizeroplus2h3 && $BRANCH == current ]]; then

		install -m 755 $EXTER/packages/blobs/bt/brcm_patchram_plus/brcm_patchram_plus_$ARCH $destination/usr/bin/brcm_patchram_plus
		cp $EXTER/packages/bsp/sunxi/ap6212-bluetooth.service $destination/lib/systemd/system/
	fi

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

		install -m 755 $EXTER/packages/blobs/bt/brcm_patchram_plus/brcm_patchram_plus_$ARCH $destination/usr/bin/brcm_patchram_plus
		install -m 755 $EXTER/packages/bsp/sunxi/zeroplus2-bt.sh $destination/usr/bin/
	fi

	if [[ $BRANCH == legacy ]]; then

		arm-linux-gnueabihf-gcc $EXTER/packages/bsp/sunxi-temp/sunxi_tp_temp.c -o $destination/usr/bin/sunxi_tp_temp
		# convert and add fex files
		mkdir -p $destination/boot/bin
		for i in $(ls -w1 $EXTER/config/fex/*.fex | xargs -n1 basename); do
		        fex2bin $EXTER/config/fex/${i%*.fex}.fex $destination/boot/bin/${i%*.fex}.bin
		done
	fi

	# h3disp for sun8i/3.4.x
	if [[ $BRANCH == legacy ]]; then

		install -m 755 $EXTER/packages/bsp/{h3disp,h3consumption,setsystem} $destination/usr/bin
	fi
}
