ARCH="riscv64"

LINUXCONFIG="linux-ky-${BRANCH}"
LINUXFAMILY="ky"

SERIALCON="ttyS0"
BOOTSCRIPT='boot-ky.cmd:boot.cmd'
BOOTENV_FILE='ky.txt'
KERNEL_COMPILER='riscv64-unknown-linux-gnu-'
UBOOT_COMPILER='riscv64-unknown-linux-gnu-'
OVERLAY_PREFIX='x1'
OFFSET=30

case "${BRANCH}" in

	current)
		BOOTBRANCH='branch:v2022.10-ky'

		KERNELBRANCH="branch:orange-pi-6.6-ky"
		KERNELPATCHDIR="ky-${BRANCH}"
		UBOOT_TARGET_MAP=";;bootinfo_sd.bin bootinfo_emmc.bin bootinfo_spinor.bin FSBL.bin u-boot-env-default.bin u-boot-opensbi.itb"
		;;

esac


uboot_custom_postprocess()
{
	:
}

write_uboot_platform()
{
	if [[ -b "${2}boot0" ]]; then
		echo 0 > /sys/block/${2##*/}boot0/force_ro
		dd if=${1}/bootinfo_emmc.bin of=${2}boot0 >/dev/null 2>&1 && sync
		dd if=${1}/FSBL.bin of=${2}boot0 seek=512 bs=1 >/dev/null 2>&1 && sync
		echo 1 > /sys/block/${2##*/}boot0/force_ro
	fi

	dd if=$1/bootinfo_sd.bin of=$2 seek=0 conv=notrunc status=none >/dev/null 2>&1
	dd if=$1/FSBL.bin of=$2 seek=256 conv=notrunc status=none >/dev/null 2>&1
	dd if=$1/u-boot-env-default.bin of=$2 seek=768 conv=notrunc status=none >/dev/null 2>&1
	dd if=$1/u-boot-opensbi.itb of=$2 seek=1664 conv=notrunc status=none >/dev/null 2>&1
}

write_uboot_platform_mtd()
{
	if [[ -b /dev/mtdblock0 ]]; then
		dd if=$1/bootinfo_spinor.bin of=/dev/mtdblock0 >/dev/null 2>&1 && sync
		dd if=$1/FSBL.bin of=/dev/mtdblock2 seek=0 bs=1 >/dev/null 2>&1 && sync
		dd if=$1/u-boot-env-default.bin of=/dev/mtdblock3 seek=0 bs=1 >/dev/null 2>&1 && sync
		dd if=$1/u-boot-opensbi.itb of=/dev/mtdblock5 seek=0 bs=1K >/dev/null 2>&1 && sync
	fi
}

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

	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y gdisk dnsmasq util-linux-extra smartmontools can-utils v4l-utils cmake python3-pip"
	if [[ ${DESKTOP_ENVIRONMENT} == gnome ]]; then
		run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y gnome-screenshot tightvncserver gnome-bluetooth"
		sed -i 's/# *AutomaticLoginEnable = true/AutomaticLoginEnable = true/' ${SDCARD}/etc/gdm3/custom.conf
		sed -i 's/# *AutomaticLogin = user1/AutomaticLogin = orangepi/' ${SDCARD}/etc/gdm3/custom.conf
		echo -e "\n[Install]\nWantedBy=multi-user.target" >> ${SDCARD}/lib/systemd/system/gdm3.service
		#cp $EXTER/packages/blobs/desktop/desktop-wallpapers/orangepi-default.png ${SDCARD}/usr/share/backgrounds/warty-final-ubuntu.png
	fi

	if [[ $RELEASE == noble && ${SELECTED_CONFIGURATION} == desktop ]]; then
		local packages_to_install
		packages_to_install_common=("ddk" "camera" "mpp")
		packages_to_install=("mesa" "glmark2" "gst" "ffmpeg" "mpv")
		packages_to_install+=("libv4l" "chromium" "npu" "docker" "adbd" "gnome")

		run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y vlc fonts-liberation libu2f-udev xdg-utils"

		for package in ${packages_to_install_common[@]}; do
			dpkg_install_debs_chroot $EXTER/cache/sources/ky_packages/common/$package
		done

		for package in ${packages_to_install[@]}; do
			dpkg_install_debs_chroot $EXTER/cache/sources/ky_packages/$RELEASE/$package
		done

		rm $SDCARD/root/*.deb >/dev/null 2>&1
	fi

	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
		echo "set-default-sink HDMI-Playback" >> ${SDCARD}/etc/pulse/default.pa
	fi

	if [[ "${BOARD}x" == orangepirv2x ]]; 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

	chroot "${SDCARD}" /bin/bash -c "sudo usermod -aG docker ${OPI_USERNAME}"
	install_wiringop
}
