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

LINUXFAMILY=sun60iw2

[[ -z $CPUMIN ]] && CPUMIN=416000
[[ -z $CPUMAX ]] && CPUMAX=2002000
GOVERNOR=ondemand

OFFSET=32

case $BRANCH in

	current)
		if [[ $GITEE_SERVER == yes ]]; then
			KERNELSOURCE="https://gitee.com/orangepi-xunlong/orange-pi-5.15-sun60iw2.git"
			MAINLINE_UBOOT_SOURCE='https://gitee.com/orangepi-xunlong/u-boot-orangepi.git'
		fi

		OVERLAY_PREFIX='sun60i-t736'
		[[ "${BOARD}" == "orangepi4pro" ]] && OVERLAY_PREFIX='sun60i-a733'
		KERNELBRANCH="branch:orange-pi-5.15-sun60iw2"
		LINUXCONFIG="linux-sun60iw2-current"
		[[ "${BOARD}" == "orangepi4pro" ]] && LINUXCONFIG="linux-sun60iw2-current-opi4pro"
		BOOTBRANCH='branch:v2018.05-sun60iw2'
		KERNELPATCHDIR=${BOARDFAMILY}-${BRANCH}
		UBOOT_TARGET_MAP=";;dts/${BOARD}-u-boot.dts boot0_sdcard.fex boot0_spinor_a733.fex boot_package.fex boot_package_a733_nvme.fex"
		UBOOT_COMPILER="arm-linux-gnueabi-"
		UBOOT_USE_GCC='> 6.0'
		BOOTENV_FILE='sun60iw2.txt'
		BOOTSCRIPT='boot-sun60iw2.cmd:boot.cmd'
		KERNEL_USE_GCC='> 10.0'
		OFFSET=32
		ATFSOURCE=""
		ATF_COMPILE="no"
		INITRD_ARCH=arm

	;;
esac

family_tweaks_s()
{
	if [[ ${DESKTOP_ENVIRONMENT} == gnome ]]; then
		run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y gnome-screenshot tightvncserver gnome-bluetooth"
		cp $EXTER/packages/blobs/desktop/desktop-wallpapers/orangepi-default.png ${SDCARD}/usr/share/backgrounds/warty-final-ubuntu.png
	fi

	if [[ $RELEASE == bookworm && ${DESKTOP_ENVIRONMENT} == gnome ]]; then
		sed -i "s/user-session.*/user-session=gnome-wayland/" ${SDCARD}/etc/lightdm/lightdm.conf.d/22-orangepi-autologin.conf
		run_on_sdcard "systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target" > /dev/null 2>&1
	fi

	[[ $RELEASE == jammy && ${DESKTOP_ENVIRONMENT} == xfce ]] && chroot $SDCARD /bin/bash -c "apt-get -y -qq install ocl-icd-libopencl1" >/dev/null 2>&1

	#if [[ "${BOARD}x" == orangepi4ax ]]; then
	#	nvram=$EXTER/cache/sources/orangepi-firmware-git
	#	[[ -f ${nvram}/nvram_ap6256.txt-$BOARD ]] && cp ${nvram}/nvram_ap6256.txt-${BOARD} ${SDCARD}/lib/firmware/nvram_ap6256.txt
	#fi

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

	chroot $SDCARD /bin/bash -c "apt-get -y -qq install libopencv-dev mtd-utils 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

		echo "load-module module-alsa-sink device=hw:0,0 sink_name=HDMI-Playback sink_properties=\"device.description='HDMI Audio'\"" >> ${SDCARD}/etc/pulse/default.pa
		echo "load-module module-alsa-sink device=hw:1,0 sink_name=AudioCodec-Playback sink_properties=\"device.description='Audio Codec'\"" >> ${SDCARD}/etc/pulse/default.pa

		chroot $SDCARD /bin/bash -c "apt-get -y -qq install vlc mpv" >/dev/null 2>&1
	fi

	if [[ ${BRANCH} == current ]]; then
		SERIALCON="ttyS0"
	fi

	rsync -a --chown=root:root "${EXTER}"/packages/bsp/sun60iw2/* ${SDCARD}/
	rsync -a --chown=root:root $EXTER/cache/sources/sun60iw2_packages/npu/* ${SDCARD}/

	if [[ $RELEASE =~ bullseye && ${SELECTED_CONFIGURATION} == desktop ]]; then
		local packages_to_install=("xserver" "glmark2" "libcedarc" "gst-omx")
		chroot $SDCARD /bin/bash -c "apt-get -y -qq install gstreamer1.0-x gstreamer1.0-plugins-bad" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
		for package in ${packages_to_install[@]}; do
			rsync -a --chown=root:root $EXTER/cache/sources/sun60iw2_packages/$RELEASE/$package/* ${SDCARD}/
		done

		#if [[ $RELEASE == jammy ]]; then
		#	chroot $SDCARD /bin/bash -c "apt-get -y -qq install glmark2 glmark2-wayland" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
		#elif [[ $RELEASE == bookworm ]]; then
		#	chroot $SDCARD /bin/bash -c "apt-get -y -qq install glmark2-x11 glmark2-wayland" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
		#fi

	fi

	install_wiringop
}

family_tweaks_bsp()
{
	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/blobs/bt/hciattach/hciattach_opi_${ARCH} $destination/usr/bin/hciattach_opi
}

uboot_custom_postprocess()
{

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

		rm dts/*.dts sys_config ${BOARD}-u-boot-${BRANCH}.dtb -r 
		export PATH=${EXTER}/packages/pack-uboot/tools/:$PATH
		cp ${EXTER}/packages/pack-uboot/${BOARDFAMILY}/bin/* . -r
		[[ "${BOARD}" == "orangepi4pro" ]] && cp ${EXTER}/packages/pack-uboot/${BOARDFAMILY}/bin/boot0_sdcard_a733.fex ./boot0_sdcard.fex
		#[[ "${BOARD}" == "orangepi4pro" ]] && cp ${EXTER}/packages/pack-uboot/${BOARDFAMILY}/bin/boot0_sdcard.fex-orangepi4pro-4G ./boot0_sdcard.fex
		cp sys_config/sys_config.fex sys_config.fex
		cp u-boot.bin u-boot.fex
		cp dts/u-boot-${BRANCH}.dts dts/${BOARD}-u-boot.dts

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

		busybox unix2dos sys_config.fex
		$EXTER/packages/pack-uboot/tools/script sys_config.fex
		cp ${BOARD}-u-boot.dtb sunxi.fex
		$EXTER/packages/pack-uboot/tools/update_dtb sunxi.fex 4096

		$EXTER/packages/pack-uboot/tools/update_uboot -no_merge u-boot.fex sys_config.bin
		update_uboot -no_merge u-boot.bin sys_config.bin

		#pack boot package
		busybox unix2dos boot_package.cfg
		$EXTER/packages/pack-uboot/tools/dragonsecboot -pack boot_package.cfg

		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
			dd if=boot0_sdcard.fex of=u-boot-${BOARD}-merged.bin bs=8k seek=1 conv=fsync
			dd if=boot_package.fex of=u-boot-${BOARD}-merged.bin bs=8k seek=2050 conv=fsync
			mv u-boot-${BOARD}-merged.bin ${DEB_STORAGE}/u-boot/
		fi
	fi
}

write_uboot_platform_mtd()
{
	if [[ -b /dev/mtdblock0 ]]; then
		flash_erase /dev/mtd0 0 50
		size=$(stat --format="%s" $1/boot0_spinor_a733.fex)
		mtd_debug write /dev/mtd0 0 $size $1/boot0_spinor_a733.fex
		size=$(stat --format="%s" $1/boot_package.fex)
		mtd_debug write /dev/mtd0 262144 $size $1/boot_package_a733_nvme.fex
		sync
	fi
}
