#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
#
ARCH="riscv64"

LINUXCONFIG="linux-starfive2-${BRANCH}"
LINUXFAMILY="starfive2"
IMAGE_PARTITION_TABLE='gpt'
SKIP_EXTERNAL_TOOLCHAINS='yes'

SRC_CMDLINE="console=tty0 console=ttyS0,115200 earlycon rootwait"
SERIALCON="ttyS0"

case "${BRANCH}" in

	current)
		BOOTBRANCH='branch:v2021.10-jh7110'

		KERNELBRANCH="branch:orange-pi-5.15-jh7110"
		KERNELPATCHDIR="starfive2-${BRANCH}"
		UBOOT_TARGET_MAP=";;u-boot-spl.bin.normal.out visionfive2_fw_payload.img"
		;;

esac

#KERNELPATCHDIR="archive/starfive2-${KERNEL_MAJOR_MINOR}" # Don't use symlinks.

uboot_custom_postprocess()
{
	:
}

write_uboot_platform()
{
	:
}

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

jh7110_install_libs()
{
	if [[ ${SELECTED_CONFIGURATION} == desktop && ${DESKTOP_ENVIRONMENT} =~ gnome && ${RELEASE} == sid ]]; then
	local packages_dir="$EXTER/cache/jh7110/"
	local starfive_url="https://github.com/starfive-tech/Debian/releases/download/v0.9.0-engineering-release-wayland"
	local gpu_package="https://github.com/starfive-tech/soft_3rdpart/raw/JH7110_VisionFive2_devel/IMG_GPU/out/img-gpu-powervr-bin-1.17.6210866.tar.gz"
	mkdir -p ${packages_dir}
	wget -qnc -P ${packages_dir} ${starfive_url}/mesa-debs.tar.gz
	wget -qnc -P ${packages_dir} ${starfive_url}/codec.tar.gz
	wget -qnc -P ${packages_dir} ${starfive_url}/ffmpeg-deb.tar.gz
	wget -qnc -P ${packages_dir} ${starfive_url}/gst-omx-deb.tar.gz
	wget -qnc -P ${packages_dir} ${starfive_url}/vlc-deb.tar.gz
	wget -qnc -P ${packages_dir} ${starfive_url}/libsdl2-2.0-0_2.26.1+dfsg-1_riscv64.deb
	wget -qnc -P ${packages_dir} ${starfive_url}/v4l2test_riscv64.deb
	wget -qnc -P ${packages_dir} https://github.com/starfive-tech/Debian/releases/download/v0.12.0-engineering-release-wayland/firefox_105.0_202305_riscv64.deb
	wget -qnc -P ${packages_dir} ${starfive_url}/vlcrc
	wget -qnc -P ${packages_dir} ${gpu_package}

	# gpu
	tar -xvzf ${packages_dir}/mesa-debs.tar.gz -C ${packages_dir} > /dev/null 2>&1
	tar -xvzf ${packages_dir}/img-gpu-powervr-bin-1.17.6210866.tar.gz -C ${packages_dir} > /dev/null 2>&1
	cp -r ${packages_dir}/img-gpu-powervr-bin-1.17.6210866/target/etc/* ${SDCARD}/etc/
	cp -r ${packages_dir}/img-gpu-powervr-bin-1.17.6210866/target/lib/* ${SDCARD}/lib/
	cp -r ${packages_dir}/img-gpu-powervr-bin-1.17.6210866/target/usr/* ${SDCARD}/usr/
	dpkg_install_debs_chroot ${packages_dir}/debs


	LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg -i /libicu71_71.1-3_riscv64.deb" > /dev/null 2>&1
	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y nautilus"
	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y libevent-dev libdbus-glib-1-dev"
	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y libopenal-dev libcdio-paranoia-dev libdc1394-dev libcaca-dev libv4l-dev libpocketsphinx-dev libbs2b-dev liblilv-0-0 librubberband-dev libmysofa-dev libflite1 libass-dev libvidstab-dev libzmq3-dev libzimg-dev libgme-dev libopenmpt-dev libchromaprint-dev librabbitmq-dev libssh-dev libsrt-openssl-dev liba52-0.7.4-dev libhwy1 libjxl0.7"
	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y fonts-freefont-ttf libaribb24-0 libcddb2 libdvbpsi10 libebml5 libixml10 liblirc-client0 liblua5.2-0 libmad0 libmatroska7 libprotobuf-lite32 libqt5x11extras5 libresid-builder0c2a libsdl-image1.2 libsdl1.2debian libsidplay2 libspatialaudio0 libupnp13 libva-wayland2 libvncclient1 libopenmpt-modplug1 libxcb-composite"
	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y qtwayland5"

	dpkg_install_debs_chroot ${packages_dir}
	#dpkg_install_deb_chroot ${packages_dir}/firefox_105.0_202305_riscv64.deb
	#dpkg_install_deb_chroot ${packages_dir}/libsdl2-2.0-0_2.26.1+dfsg-1_riscv64.deb
	tar -xvzf ${packages_dir}/codec.tar.gz -C ${packages_dir} > /dev/null 2>&1
	dpkg_install_debs_chroot ${packages_dir}/codec
	tar -xvzf ${packages_dir}/ffmpeg-deb.tar.gz -C ${packages_dir} > /dev/null 2>&1
	dpkg_install_debs_chroot ${packages_dir}/ffmpeg-deb
	tar -xvzf ${packages_dir}/gst-omx-deb.tar.gz -C ${packages_dir} > /dev/null 2>&1
	dpkg_install_debs_chroot ${packages_dir}/gst-omx-deb
	tar -xvzf ${packages_dir}/vlc-deb.tar.gz -C ${packages_dir} > /dev/null 2>&1
	dpkg_install_debs_chroot ${packages_dir}/vlc-deb
	mkdir -p $SDCARD/home/orangepi/.config/vlc/
	cp ${packages_dir}/vlcrc $SDCARD/home/orangepi/.config/vlc/
	#chown orangepi:orangepi /home/orangepi/.config -R
	rm -rf $SDCARD/etc/unsupported-skip-usrmerge-conversion
	rm -rf $SDCARD/usr/local/bin/openssl

	echo -e "\nWaylandEnable=true" >> ${SDCARD}/etc/gdm3/daemon.conf
	rm -rf ${SDCARD}/root/*.deb
	sed -i 's/# *AutomaticLoginEnable = true/AutomaticLoginEnable = true/' ${SDCARD}/etc/gdm3/daemon.conf
	sed -i 's/# *AutomaticLogin = user1/AutomaticLogin = orangepi/' ${SDCARD}/etc/gdm3/daemon.conf
	cp $EXTER/packages/blobs/desktop/desktop-wallpapers/orangepi-default.png ${SDCARD}/usr/share/backgrounds/warty-final-ubuntu.png
	run_on_sdcard "systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target" > /dev/null 2>&1
	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y blueman"

	fi

	run_on_sdcard "DEBIAN_FRONTEND=noninteractive apt-get install -y v4l-utils mtd-utils bluez"
	echo 'set-card-profile alsa_card.platform-soc_snd-card6 pro-audio' >> ${SDCARD}/etc/pulse/default.pa
	rsync -a --chown=root:root "${EXTER}"/packages/bsp/jh7110/* ${SDCARD}/
}
