#!/bin/bash

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

# get default's locale file
DEFLOCDIR="/usr/share/pearl/pearl-aptus-appcenter"
if [ "`cat /etc/default/locale | grep LANG= | grep de`" != "" ]; then
. $DEFLOCDIR/de
elif [ "`cat /etc/default/locale | grep LANG= | grep es`" != "" ]; then
. $DEFLOCDIR/es
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 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=pearl-aptus-appcenter --width=500 --height=300 --center"
TITLE="--always-print-result --dialog-sep --image=pearl-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"
ARCH=`uname -r | grep "amd64" | awk '{print $1}'`
TESTLIQ=`dpkg-query -l | grep linux-image-liquorix*`
if [ -f /usr/bin/pearl-xterm ]; then
	PEARLXTERM="/usr/bin/pearl-xterm"
else
	echo "pearl-xterm is missing... Exiting..."
	exit 1
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$LOCAL16"
	exit 1
fi

# check arm arch
ARCHARM="`uname -m | grep arm`"
if [ "$ARCHARM" != "" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL051"
	exit 1
fi

CHOICES=`$DIALOG $TITLE"$TITLETEXT" $OKEXIT $MENU $TEXT"$LOCAL052" \
NON_PAE "$LOCAL062" \
PAE "$LOCAL063" \
64 "$LOCAL064" \
Exit "$LOCAL056"`

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

if [ "$CHOICE" = "NON_PAE" ]; then
	if [ "$ARCH" != "" ]; then
		$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL057"
		exit 1
	fi
elif [ "$CHOICE" = "PAE" ]; then
	if [ "$ARCH" != "" ]; then
		$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL031"
		exit 1
	fi
elif [ "$CHOICE" = "64" ]; then
	if [ "$ARCH" = "" ]; then
		$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL060"
		exit 1
	fi
fi

if [ "$CHOICE" != "Exit" ]; then
	# install Liquorix keyring
	$PEARLXTERM "apt-get update"
	$PEARLXTERM "apt-get install apt-transport-https -y --force-yes"
	$PEARLXTERM "curl https://liquorix.net/linux-liquorix.pub | apt-key add -"
	
	# 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 http://liquorix.net/debian sid main
deb-src http://liquorix.net/debian sid main
FOO

	$PEARLXTERM "apt-get update"
fi

if [ "$CHOICE" = "NON_PAE" ]; then
	# install i686 NON-PAE Liquorix kernel
	$PEARLXTERM "apt-get install linux-image-liquorix-686 linux-headers-liquorix-686"
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL065"
	exit 0
elif [ "$CHOICE" = "PAE" ]; then
	# install i686 PAE Liquorix kernel
	$PEARLXTERM "apt-get install linux-image-liquorix-686-pae linux-headers-liquorix-686-pae"
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"\n$LOCAL065"
	exit 0
elif [ "$CHOICE" = "64" ]; then
	# install 64 bit Liquorix kernel
	$PEARLXTERM "apt-get 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
