#!/bin/bash

# install-pearl-kernel script lets you install the latest stable,
# home roasted Linux kernel
# Copyright 2016-2020 Paweł "pavroo" Pijanowski <pavroo@onet.eu>
# Licensed under the GPLv2 license see /usr/share/common-licenses/GPL-2
# Created 2016/04/06
# Last update 2020/10/05

# get default's locale file
DEFLOCDIR="/usr/share/pearl/pearl-aptus"
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
. $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 ja`" != "" ]; then
. $DEFLOCDIR/ja
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=prefernces-other --width=450 --height=200 --center"
TITLE="--always-print-result --dialog-sep --image=preferences-other --title="
TEXT="--text="
MENU="--list --column=$LOCAL21 --column=$LOCAL22"
OKEXIT=" --button=Ok:0 --button=$LOCAL24:1"
MSGBOX="--button=Ok:0"
TITLETEXT="$LOCAL60"
ARCH=`uname -r | grep "amd64" | awk '{print $1}'`
TESTPEARL=`dpkg-query -l | grep linux-image-pearl*`
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 Pearl kernel installed already
if [ "$TESTPEARL" != "" ]; then
	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL61"
	exit 0
fi

# check connection
PINGTEST=$(ping -c 1 pearllinux.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$LOCAL66"
	exit 1
fi

mainmenu () {
if [ "$ARCH" != "" ]; then
	amd64menu
else
	paemenu
fi
}

paemenu () {
$DIALOG $TITLE"$TITLETEXT" $OKEXIT $TEXT"$LOCAL63\n\n$LOCAL91"

if [ "$?" != "0" ]; then
	#$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL37"
	exit 1
else

	# preper the pearl unstable list
	if [ -f /etc/apt/sources.list.d/pearl-unstable.list ]; then
		rm -f /etc/apt/sources.list.d/pearl-unstable.list
	fi

cat > /etc/apt/sources.list.d/pearl-unstable.list <<FOO
# pearl unstable repository
deb https://repo.pearllinux.org/ unstable main
deb-src https://repo.pearllinux.org/ unstable main
FOO

	$PEARLXTERM "apt-get update"

	# install i686 PAE Pearl kernel and headers
	$PEARLXTERM "apt-get install linux-image-pearl-686-pae linux-headers-pearl-686-pae"

	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL65"
	exit 0
fi
}

amd64menu () {
$DIALOG $TITLE"$TITLETEXT" $OKEXIT $TEXT"$LOCAL64\n\n$LOCAL91"

if [ "$?" != "0" ]; then
	#$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL37"
	exit 1
else

	# preper the pearl unstable list
	if [ -f /etc/apt/sources.list.d/pearl-unstable.list ]; then
		rm -f /etc/apt/sources.list.d/pearl-unstable.list
	fi

cat > /etc/apt/sources.list.d/pearl-unstable.list <<FOO
# pearl unstable repository
deb https://repo.pearllinux.org/ unstable main
deb-src https://repo.pearllinux.org/ unstable main
FOO

	$PEARLXTERM "apt-get update"

	# install 64 bit Pearl kernel and headers
	$PEARLXTERM "apt-get install linux-image-pearl-amd64 linux-headers-pearl-amd64"

	$DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT"$LOCAL65"
	exit 0
fi
}

mainmenu
