#!/bin/bash

# install-liquorix script lets you install the 3th party Liquorix Linux kernel
# Copyright 2015-2025 Paweł "pavroo" Pijanowski <pavroo@onet.eu>
# Licensed under the GPLv2 license see /usr/share/common-licenses/GPL-2
# Created 2015/05/18
# Last update 2025/07/22

# get default's locale file
DEFLOCDIR="/usr/share/sparky/sparky-aptus-appcenter"
if [ "`cat /etc/default/locale | grep LANG= | grep de`" != "" ]; then
. $DEFLOCDIR/de
elif [ "`cat /etc/default/locale | grep LANG= | grep el`" != "" ]; then
. $DEFLOCDIR/el
elif [ "`cat /etc/default/locale | grep LANG= | grep es`" != "" ]; then
	if [ "`cat /etc/default/locale | grep LANG= | grep es_AR`" != "" ]; then
. $DEFLOCDIR/es_AR
	else
. $DEFLOCDIR/es_ES
	fi
elif [ "`cat /etc/default/locale | grep LANG= | grep fi`" != "" ]; then
. $DEFLOCDIR/fi
elif [ "`cat /etc/default/locale | grep LANG= | grep fr`" != "" ]; then
. $DEFLOCDIR/fr
elif [ "`cat /etc/default/locale | grep LANG= | grep hu`" != "" ]; then
. $DEFLOCDIR/hu
elif [ "`cat /etc/default/locale | grep LANG= | grep id_ID`" != "" ]; then
. $DEFLOCDIR/id_ID
elif [ "`cat /etc/default/locale | grep LANG= | grep it`" != "" ]; then
. $DEFLOCDIR/it
elif [ "`cat /etc/default/locale | grep LANG= | grep pl`" != "" ]; then
. $DEFLOCDIR/pl
elif [ "`cat /etc/default/locale | grep LANG= | grep pt_BR`" != "" ]; then
. $DEFLOCDIR/pt_BR
elif [ "`cat /etc/default/locale | grep LANG= | grep pt_PT`" != "" ]; then
. $DEFLOCDIR/pt_PT
elif [ "`cat /etc/default/locale | grep LANG= | grep ru`" != "" ]; then
. $DEFLOCDIR/ru
elif [ "`cat /etc/default/locale | grep LANG= | grep uk`" != "" ]; then
. $DEFLOCDIR/uk
elif [ "`cat /etc/default/locale | grep LANG= | grep zh_CN`" != "" ]; then
. $DEFLOCDIR/zh_CN
else
. $DEFLOCDIR/en
fi

testroot="`whoami`"
if [ "$testroot" != "root" ]; then
	echo " Must be root... Exiting"
	exit 1
fi

DIALOG="yad --window-icon=sparky-aptus-appcenter --width=500 --height=300 --center"
TITLE="--always-print-result --dialog-sep --image=sparky-aptus-appcenter --title="
TEXT="--text="
MENU="--list --column=$LOCAL049 --column=$LOCAL050"
OKEXIT=" --button=Ok:0 --button=$LOCAL001:1"
MSGBOX="--button=Ok:0"
TITLETEXT="APTus AppCenter"
ARCH64=`uname -r | grep "amd64" | awk '{print $1}'`
ARCHX64=`uname -r | grep "x64" | awk '{print $1}'`
if [ "$ARCH64" != "" ]; then
	ARCH="OK"
elif [ "$ARCHX64" != "" ]; then
	ARCH="OK"
else
	ARCH=""
fi
TESTLIQ=`dpkg-query -l | grep linux-image-liquorix*`
if [ -f /usr/bin/sparky-xterm ]; then
	SPARKYXTERM="/usr/bin/sparky-xterm"
else
	echo "sparky-xterm is missing... Exiting..."
	exit 1
fi
CHECKNALA=`which nala`
if [ "$CHECKNALA" != "" ]; then
	UPDATE="nala update"
	INSTALL="nala install"
else
	UPDATE="apt-get update"
	INSTALL="apt-get install"
fi

# check is the Liquorix kernel installed already
if [ "$TESTLIQ" != "" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL061"
	exit 1
fi

# check connection
PINGTEST=$(ping -c 1 debian.org | grep [0-9])
if [ "$PINGTEST" = "" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL016"
	exit 1
fi

# check arm arch
if [ "$ARCH" = "" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL051"
	exit 1
fi

if [ "$ARCH" != "" ]; then
# 64bit
CHOICES=`$DIALOG $TITLE"$TITLETEXT" $OKEXIT $MENU $TEXT"$LOCAL052" \
64 "$LOCAL064" \
Exit "$LOCAL056"`
else
# 32bit
$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL004"
exit 1
fi

if [ "$?" = "0" ]; then
	CHOICE=`echo $CHOICES | cut -d "|" -f 1`
else
	exit 0
fi

if [ "$CHOICE" = "64" ]; then
	if [ "$ARCH" = "" ]; then
		$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL060"
		exit 1
	fi
fi

if [ "$CHOICE" != "Exit" ]; then
	# install Liquorix keyring
	if [ ! -f /etc/apt/trusted.gpg.d/liquorix-keyring.gpg ]; then
		$SPARKYXTERM "wget -O - https://liquorix.net/liquorix-keyring.gpg | tee /etc/apt/trusted.gpg.d/liquorix-keyring.gpg"
	fi
	# check the Liquorix list
	if [ -f /etc/apt/sources.list.d/liquorix.list ]; then
		rm -f /etc/apt/sources.list.d/liquorix.list
	fi
	# create new list
cat > /etc/apt/sources.list.d/liquorix.list <<FOO
# Liquorix kernel
deb [signed-by=/etc/apt/trusted.gpg.d/liquorix-keyring.gpg] http://liquorix.net/debian trixie main
deb-src [signed-by=/etc/apt/trusted.gpg.d/liquorix-keyring.gpg] http://liquorix.net/debian trixie main
FOO

fi

if [ "$CHOICE" = "64" ]; then
	# install 64 bit Liquorix kernel
	$SPARKYXTERM "$UPDATE && $INSTALL linux-image-liquorix-amd64 linux-headers-liquorix-amd64"
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL065"
	exit 0
elif [ "$CHOICE" = "Exit" ]; then
	exit 1
else
	exit 1
fi

exit 0
