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

LINUXFAMILY=sun50iw9

[[ -z $CPUMIN ]] && CPUMIN=480000
[[ -z $CPUMAX ]] && CPUMAX=1512000
GOVERNOR=ondemand

ASOUND_STATE=asound.state.sun50iw9-${BRANCH}

case $BRANCH in

        legacy)

		KERNELBRANCH="branch:orange-pi-4.9-sun50iw9"
		BOOTBRANCH='branch:v2018.05-sun50iw9'

		KERNELPATCHDIR=${BOARDFAMILY}-${BRANCH}
		UBOOT_TARGET_MAP=";;dts/${BOARD}-u-boot.dts boot0_sdcard.fex boot_package.fex"
		UBOOT_COMPILER="arm-linux-gnueabi-"
		UBOOT_USE_GCC='> 6.0'
		BOOTENV_FILE=sun50iw9-default.txt
		BOOTSCRIPT='boot-sun50iw9.cmd:boot.cmd'
		KERNEL_USE_GCC='> 10.0'
		OFFSET=20
		ATFSOURCE=""
		ATF_COMPILE="no"
		INITRD_ARCH=arm

        ;;

	current)

		OVERLAY_PREFIX='sun50i-h616'
		KERNELBRANCH="branch:orange-pi-5.4-sun50iw9"
		#BOOTBRANCH='branch:u-boot-2018'
		[[ $BOARD =~ orangepizero2 ]] && BOOTBRANCH='branch:v2018.05-sun50iw9'
		[[ $BOARD =~ orangepizero3|orangepir1b|orangepizero2w ]] && BOOTBRANCH='branch:v2018.05-h618'

		KERNELPATCHDIR=${BOARDFAMILY}-${BRANCH}
		UBOOT_TARGET_MAP=";;dts/${BOARD}-u-boot.dts boot0_sdcard.fex boot_package.fex"
		UBOOT_COMPILER="arm-linux-gnueabi-"
		UBOOT_USE_GCC='> 6.0'
		BOOTENV_FILE=sun50iw9-default.txt
		BOOTSCRIPT='boot-sun50iw9.cmd:boot.cmd'
		KERNEL_USE_GCC='> 10.0'
		OFFSET=20
		ATFSOURCE=""
		ATF_COMPILE="no"
		INITRD_ARCH=arm

	;;

	next)

		OVERLAY_PREFIX='sun50i-h616'
		ATFSOURCE='https://github.com/ARM-software/arm-trusted-firmware'
		ATFBRANCH='commit:4b9be5abfa8b1a7c7e00776da01768d3358e648a'
		ATF_PLAT="sun50i_h616";
		ATF_TARGET_MAP='PLAT=sun50i_h616 DEBUG=1 bl31;;build/sun50i_h616/debug/bl31.bin'
		UBOOT_TARGET_MAP=';;u-boot-sunxi-with-spl.bin'
		[[ $BOARD =~ orangepizero2 ]] && BOOTBRANCH='branch:v2021.10-sunxi'
		BOOTSCRIPT='boot-sun50iw9-next.cmd:boot.cmd'
		if [[ $BOARD =~ orangepizero3|orangepir1b|orangepizero2w ]]; then
			BOOTBRANCH='branch:v2024.01'
		fi

		## For Linux5.16.y
		#KERNELBRANCH="branch:orange-pi-5.16-sunxi64"
		#LINUXCONFIG="linux-5.16-sun50iw9-current"

		## For Linu6.1.y
		KERNELBRANCH="branch:orange-pi-6.1-sun50iw9"
		LINUXCONFIG="linux-6.1-sun50iw9-next"
	;;

esac

family_tweaks_s()
{
	rsync -a --chown=root:root "${EXTER}"/packages/bsp/overlays_arm64/* ${SDCARD}/

	chroot $SDCARD /bin/bash -c "apt-get -y -qq install rfkill bluetooth bluez bluez-tools ir-keytable tree lirc v4l-utils dnsmasq" >/dev/null 2>&1

	if [[ $BUILD_DESKTOP == yes ]]; then

		sed -i "s/auto-profiles = yes/auto-profiles = no/" ${SDCARD}/usr/share/pulseaudio/alsa-mixer/profile-sets/default.conf

		if [[ $BRANCH =~ legacy ]]; then
			echo "load-module module-alsa-sink device=hw:0,0 sink_name=AudioCodec-Playback sink_properties=\"device.description='Audio Codec'\"" >> ${SDCARD}/etc/pulse/default.pa
			echo "load-module module-alsa-sink device=hw:1,0 sink_name=HDMI-Playback sink_properties=\"device.description='HDMI Audio'\"" >> ${SDCARD}/etc/pulse/default.pa
		elif [[ $BRANCH =~ current|next ]]; then
			echo "load-module module-alsa-sink device=hw:0,0 sink_name=AudioCodec-Playback sink_properties=\"device.description='Audio Codec'\"" >> ${SDCARD}/etc/pulse/default.pa
			echo "load-module module-alsa-sink device=hw:2,0 sink_name=HDMI-Playback sink_properties=\"device.description='HDMI Audio'\"" >> ${SDCARD}/etc/pulse/default.pa
		fi
	fi

	if [[ ${BRANCH} == current ]]; then
		sed -i 's/^debug_uart.*/debug_uart=ttyAS0/' "${SDCARD}"/boot/orangepiEnv.txt
		SERIALCON="ttyAS0"
	fi

	install_wiringop
}

family_tweaks_bsp()
{

	install -m 755 $EXTER/packages/blobs/bt/hciattach/hciattach_opi_${ARCH} $destination/usr/bin/hciattach_opi

	if [[ ${BOARD} == orangepizero2w ]]; then
		install -m 755 $EXTER/packages/bsp/h618/sun50i-h618-lradc-keys-${BRANCH}.dts $destination/usr/src/sun50i-h618-lradc-keys.dts
	fi

}

uboot_custom_postprocess()
{

	if [[ ${BRANCH} =~ legacy|current && $(dpkg --print-architecture) == amd64 ]]; then

		rm dts/*.dts sys_config ${BOARD}-u-boot-${BRANCH}.dtb -r >/dev/null 2>&1
		export PATH=${EXTER}/packages/pack-uboot/${BOARDFAMILY}/tools/:$PATH
		cp ${EXTER}/packages/pack-uboot/${BOARDFAMILY}/bin/* . -r
		cp sys_config/sys_config_${BOARD}.fex sys_config.fex
		cp u-boot.bin u-boot.fex
		if [[ $KERNELBRANCH == "branch:orange-pi-5.4-sun50iw9" ]]; then
			mv monitor.fex-linux5.4 monitor.fex
			mv boot_package.cfg-linux5.4 boot_package.cfg
			mv boot0_sdcard.fex-linux5.4 boot0_sdcard.fex
		fi
		cp dts/${BOARD}-u-boot-${BRANCH}.dts dts/${BOARD}-u-boot.dts

		# make u-boot dtb
		$EXTER/packages/pack-uboot/${BOARDFAMILY}/tools/dtc -p 2048 -W no-unit_address_vs_reg -@ -O dtb -o ${BOARD}-u-boot.dtb -b 0 dts/${BOARD}-u-boot-${BRANCH}.dts >/dev/null 2>&1
		[[ ! -f ${BOARD}-u-boot.dtb ]] && exit_with_error "dts compilation failed"

		busybox unix2dos sys_config.fex
		$EXTER/packages/pack-uboot/${BOARDFAMILY}/tools/script sys_config.fex >/dev/null 2>&1
		cp ${BOARD}-u-boot.dtb sunxi.fex
		$EXTER/packages/pack-uboot/${BOARDFAMILY}/tools/update_dtb sunxi.fex 4096 >/dev/null 2>&1

		$EXTER/packages/pack-uboot/${BOARDFAMILY}/tools/update_uboot -no_merge u-boot.fex sys_config.bin >/dev/null 2>&1
		update_uboot -no_merge u-boot.bin sys_config.bin >/dev/null 2>&1

		#pack boot package
		busybox unix2dos boot_package.cfg
		$EXTER/packages/pack-uboot/${BOARDFAMILY}/tools/dragonsecboot -pack boot_package.cfg >/dev/null 2>&1

		if [[ ${MERGE_UBOOT} == yes ]]; then

			display_alert "Merge u-boot" "u-boot-${BOARD}-merged.bin" "info"
			dd if=/dev/zero of=u-boot-${BOARD}-merged.bin bs=1M count=20 > /dev/null 2>&1
			dd if=boot0_sdcard.fex of=u-boot-${BOARD}-merged.bin bs=8k seek=1 conv=fsync > /dev/null 2>&1
			dd if=boot_package.fex of=u-boot-${BOARD}-merged.bin bs=8k seek=2050 conv=fsync > /dev/null 2>&1
			mv u-boot-${BOARD}-merged.bin ${DEB_STORAGE}/u-boot/
		fi
	fi
}
