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

OVERLAY_PREFIX='rk3588'
BOOTDELAY=0

CPUMIN=408000
CPUMAX=2400000

case $BRANCH in

	legacy)

		BOOTBRANCH='branch:v2017.09-rk3588'
		UBOOT_COMPILER="aarch64-linux-gnu-"
		UBOOT_USE_GCC='< 8.0'

		KERNELBRANCH='branch:orange-pi-5.10-rk35xx'
		KERNELPATCHDIR='rockchip-rk3588-legacy'
		LINUXCONFIG="linux-rockchip-rk3588-legacy"
		[[ ${BOARD} == orangepi5pro ]] && LINUXCONFIG="linux-rockchip-rk3588-legacy-opi5pro"
		[[ ${BOARD} =~ orangepi5max|orangepi5ultra ]] && LINUXCONFIG="linux-rockchip-rk3588-legacy-opi5max"
		[[ ${BOARD} == orangepicm5-tablet ]] && LINUXCONFIG="linux-rockchip-rk3588-legacy-opicm5-tablet"

		if [[ "${build_rt_image}" =~ yes ]]; then
			KERNELBRANCH='branch:orange-pi-5.10-rk35xx-rt'
			LINUXCONFIG="linux-rockchip-rk3588-legacy-rt"
			[[ ${BOARD}x =~ orangepi5prox|orangepi5maxx|orangepi5ultrax ]] && LINUXCONFIG="linux-rockchip-rk3588-legacy-rt-opi5pro"
		fi

		KERNEL_USE_GCC='> 10.0'

		;;
	current)

		BOOTBRANCH='branch:v2017.09-rk3588'
		UBOOT_COMPILER="aarch64-linux-gnu-"
		UBOOT_USE_GCC='< 8.0'

		KERNELBRANCH='branch:orange-pi-6.1-rk35xx'
		KERNELPATCHDIR='rockchip-rk3588-current'
		LINUXCONFIG="linux-rockchip-rk3588-current"
		[[ ${BOARD} == orangepicm5-tablet ]] && LINUXCONFIG="linux-rockchip-rk3588-current-opicm5-tablet"
		[[ ${BOARD} =~ orangepi5pro|orangepi5max|orangepi5ultra ]] && LINUXCONFIG="linux-rockchip-rk3588-current-opi5pro"

		KERNEL_USE_GCC='> 10.0'
esac

prepare_boot_configuration

family_tweaks_bsp() {

	install -m 755 $EXTER/packages/bsp/adb/adbd-${ARCH} ${destination}/usr/bin/adbd
	:
}


install_balenaEtcher(){

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

		balena_etcher_deb=balena-etcher-electron_1.7.9+5945ab1f_arm64.deb
		balena_etcher_url="https://github.com/Itai-Nelken/BalenaEtcher-arm/releases/download/v1.7.9/${balena_etcher_deb}"
		balena_etcher=${EXTER}/cache/debs/arm64/${balena_etcher_deb}

		if [[ ! -f ${balena_etcher} ]]; then
			wget -P ${EXTER}/cache/debs/arm64 ${balena_etcher_url}
		fi

		install_deb_chroot ${balena_etcher}
	fi
}

family_tweaks_s() {

	if [[ ${SELECTED_CONFIGURATION} == desktop && ${DESKTOP_ENVIRONMENT} == kde-plasma ]]; then

		rsync -a --chown=root:root --exclude=etc/pulse "${EXTER}"/packages/bsp/rk3588/* ${SDCARD}/

		cat > "${SDCARD}"/etc/profile.d/kde_env.sh <<- __EOF__
		#!/bin/bash

		export KWIN_COMPOSE=X
		__EOF__

		cat > "${SDCARD}"/home/orangepi/.config/kwalletrc <<- __EOF__
		[Wallet]
		Enabled=false
		__EOF__

		if [[ ${RELEASE} == bullseye ]]; then
			cat > "${SDCARD}"/home/orangepi/.config/kwinrc <<- __EOF__
			[Compositing]
			Backend=XRender
			OpenGLIsUnsafe=false
			__EOF__

			cat <<-EOF >>"${SDCARD}"/home/orangepi/.config/kglobalshortcutsrc
			[org.kde.konsole.desktop]
			NewTab=,,Open a New Tab
			NewWindow=,,Open a New Window
			_k_friendly_name=Konsole
			_launch=Ctrl+Alt+T,,Konsole
			EOF

			cat <<-EOF >>"${SDCARD}"/home/orangepi/.config/plasma-org.kde.plasma.desktop-appletsrc
			[Containments][2][Applets][5][Configuration][General]
			launchers=applications:systemsettings.desktop,applications:org.kde.discover.desktop,preferred://filemanager,preferred://browser,applications:org.kde.konsole.desktop

			[Containments][2][Applets][5][Configuration][General]
			launchers=applications:systemsettings.desktop,applications:org.kde.discover.desktop,preferred://filemanager,preferred://browser,applications:org.kde.konsole.desktop,applications:chromium-browser.desktop
			EOF
		fi
	else
		rsync -a --chown=root:root "${EXTER}"/packages/bsp/rk3588/* ${SDCARD}/
	fi

	if [[ ${BOARD} =~ orangepi5plus|orangepiaimax|orangepi5max|orangepi900|orangepi5ultra ]]; then

		if [[ ${SELECTED_CONFIGURATION} == desktop && ${DESKTOP_ENVIRONMENT} == kde-plasma ]]; then
			rsync -a --chown=root:root --exclude=etc/pulse "${EXTER}"/packages/bsp/orangepi5plus/* ${SDCARD}/
		else
			rsync -a --chown=root:root "${EXTER}"/packages/bsp/orangepi5plus/* ${SDCARD}/
		fi
		cp $EXTER/cache/sources/linuxpg "${SDCARD}"/opt/ -rfa
	fi

	if [[ ${BOARD} =~ orangepicm5 ]]; then

		cp $EXTER/cache/sources/linuxpg "${SDCARD}"/opt/ -rfa
	fi

	[[ ${RELEASE} == jammy ]] && rm ${SDCARD}/etc/profile.d/orangepi-ssh-title.sh # for adbd

	if [[ -f "${SDCARD}"/etc/profile.d/im-config_wayland.sh && ${DESKTOP_ENVIRONMENT} =~ xfce|kde-plasma ]]; then
		chroot $SDCARD /bin/bash -c "rm /etc/profile.d/im-config_wayland.sh" # for adbd
	fi

	# enable additional services
	chroot "${SDCARD}" /bin/bash -c "systemctl --no-reload enable usbdevice.service >/dev/null 2>&1"
	#[[ $BOARD == orangepi5plus ]] && chroot "${SDCARD}" /bin/bash -c "systemctl --no-reload enable pwm-fan.service >/dev/null 2>&1"

	sed "s/^SIZE=.*/SIZE=200M/" -i "${SDCARD}"/etc/default/orangepi-ramlog
	chroot "${SDCARD}" /bin/bash -c "ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime"
	chroot "${SDCARD}" /bin/bash -c "dpkg-reconfigure -f noninteractive tzdata" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1

	chroot $SDCARD /bin/bash -c "apt-get -y -qq install usb-modeswitch tree dnsmasq v4l-utils swig python3-dev python3-setuptools bluez libncurses-dev" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1

	[[ ${BOARD} =~ orangepiaimax ]] && install_310b-npu-driver
	install_wiringop
	install_balenaEtcher

	# Board audio specific changes
	if [[ ${BOARD}x =~ orangepi5x|orangepi5bx|orangepicm5-tabletx|orangepi5prox ]]; then

		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi0-sound", ENV{SOUND_DESCRIPTION}="HDMI0 Audio"' > ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-dp0-sound", ENV{SOUND_DESCRIPTION}="DP0 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-es8388-sound", ENV{SOUND_DESCRIPTION}="ES8388 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules

	elif [[ ${BOARD}x =~ orangepicm5x ]]; then

		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi0-sound", ENV{SOUND_DESCRIPTION}="HDMI0 Audio"' > ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-dp0-sound", ENV{SOUND_DESCRIPTION}="DP0 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules

	elif [[ "${BOARD}x" =~ orangepi5plusx|orangepiaimaxx|orangepi900x ]]; then

		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi0-sound", ENV{SOUND_DESCRIPTION}="HDMI0 Audio"' > ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi1-sound", ENV{SOUND_DESCRIPTION}="HDMI1 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmiin-sound", ENV{SOUND_DESCRIPTION}="HDMI-In Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-dp0-sound", ENV{SOUND_DESCRIPTION}="DP0 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-es8388-sound", ENV{SOUND_DESCRIPTION}="ES8388 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules

	elif [[ "${BOARD}x" == orangepi5maxx ]]; then

		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi0-sound", ENV{SOUND_DESCRIPTION}="HDMI0 Audio"' > ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi1-sound", ENV{SOUND_DESCRIPTION}="HDMI1 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-es8388-sound", ENV{SOUND_DESCRIPTION}="ES8388 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules

	elif [[ "${BOARD}x" == orangepi5ultrax ]]; then

		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmi1-sound", ENV{SOUND_DESCRIPTION}="HDMI Audio"' > ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-hdmiin-sound", ENV{SOUND_DESCRIPTION}="HDMI-In Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
		echo 'SUBSYSTEM=="sound", ENV{ID_PATH}=="platform-es8388-sound", ENV{SOUND_DESCRIPTION}="ES8388 Audio"' >> ${SDCARD}/etc/udev/rules.d/90-naming-audios.rules
	fi

	# Realtek 8811CU/8821CU usb modeswitch support
	if [[ -f ${SDCARD}/lib/udev/rules.d/40-usb_modeswitch.rules ]]; then
		cp ${SDCARD}/lib/udev/rules.d/40-usb_modeswitch.rules ${SDCARD}/etc/udev/rules.d/40-usb_modeswitch.rules
		sed '/LABEL="modeswitch_rules_end"/d' -i ${SDCARD}/etc/udev/rules.d/40-usb_modeswitch.rules
		cat >> ${SDCARD}/etc/udev/rules.d/40-usb_modeswitch.rules <<-EOF
		# Realtek 8821CU Wifi AC USB
		ATTR{idVendor}=="0bda", ATTR{idProduct}=="1a2b", RUN+="/usr/sbin/usb_modeswitch -K -v 0bda -p 1a2b"
		LABEL="modeswitch_rules_end"
		EOF
	fi

	if [[ "${BOARD}x" == orangepi5ultrax ]]; then
		cp $EXTER/cache/sources/orangepi-firmware-git/nvram_ap6611s.txt-${BOARD} ${SDCARD}/lib/firmware/nvram_ap6611s.txt

	elif [[ "${BOARD}x" == orangepicm5-tabletx ]]; then
		cp $EXTER/cache/sources/orangepi-firmware-git/nvram_ap6256.txt-${BOARD} ${SDCARD}/lib/firmware/nvram_ap6256.txt
	fi
}
