#!/bin/bash

CHECK_SCRIPT_NAME='get_x64_psabi_pkg_name.sh'
MIRROR_HOST='kr.mirrors.naho.moe'
SCRIPT_URI="https://${MIRROR_HOST}/naho/tools/${CHECK_SCRIPT_NAME}"
TMP_DIR=$(mktemp -d -t ci-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX)
CHECK_SCRIPT_PATH="${TMP_DIR}/${CHECK_SCRIPT_NAME}"
PKG_NAME_PATH="${TMP_DIR}/pkg_name"


# MARK: Download psabi checking script file.
curl "${SCRIPT_URI}" --output "${CHECK_SCRIPT_PATH}"
chmod +x "${CHECK_SCRIPT_PATH}"
/usr/bin/awk -f "${CHECK_SCRIPT_PATH}" > "${PKG_NAME_PATH}"

# MARK: If user requestd Stable Real-Time version, then add '-rt' in "${PKG_NAME}"
if [ -z "$RELEASE_CHANNEL" ];then
	# MARK: installing Stable Mainline [MAIN]
	echo '[Default] Installing Stable Mainline [MAIN] channel of the Kernel...'
else
	sed -i "s|x64|${RELEASE_CHANNEL}-x64|g" "${PKG_NAME_PATH}"
fi


# MARK: Get final Package Name
PKG_NAME=$(cat "${PKG_NAME_PATH}")

wget -qO - https://dl.xanmod.org/archive.key | sudo gpg --dearmor -o /usr/share/keyrings/xanmod-archive-keyring.gpg && \
echo 'deb [signed-by=/usr/share/keyrings/xanmod-archive-keyring.gpg] https://deb.xanmod.org releases main' | sudo tee /etc/apt/sources.list.d/xanmod-release.list && \
sudo apt update && sudo apt install -y "${PKG_NAME}" && \
echo '' && \
echo '===== You need to reboot for applying the changes. =====' && \
echo ''
