#!/bin/bash
#
#Copyright 2012 - 2016 CurlyMo & mkreisl <development@xbian.org>
#
#This file is part of XBian - XBMC on the Raspberry Pi.
#
#XBian is free software: you can redistribute it and/or modify it under the
#terms of the GNU General Public License as published by the Free Software
#Foundation, either version 3 of the License, or (at your option) any later
#version.
#
#XBian is distributed in the hope that it will be useful, but WITHOUT ANY
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#details.
#
#You should have received a copy of the GNU General Public License along
#with XBian. If not, see <http://www.gnu.org/licenses/>

#|------------------------------------|
#|          Global variables          |
#|------------------------------------|

# The arguments this module accepts
ARGUMENTS=(select update list initramfs capability);

#|------------------------------------|
#|          Include files             |
#|------------------------------------|

source $BASEPATH/modules/kernel/functions
if [ $GUIMODE -eq 1 ]; then
	source $BASEPATH/modules/kernel/dialogs
fi
source $BASEPATH/functions/array

#|------------------------------------|
#|           Main program             |
#|------------------------------------|

# Executes the GUI version of this module
function showGUIFn() {
	getInstalledKernelsFn;
	if [ $? -gt 1 ]; then
		getCurrentKernelVersionFn;
		createKernelsMenu $CURRENTKERNEL "${KERNELS[@]}";
		showKernelVersionsDlg;
		if [ $? -eq 0 ]; then
			setKernelVersionFn "${KERNELS[$(($RETURN-1))]}"
			RETURN=$?;
			if [ $RETURN -eq 1 ]; then
				showKernelSwitchedSuccesfullDlg;
				askForRebootFn;
			elif [ $RETURN -eq 2 ]; then
				showKernelAlreadyRunningDlg;
			else
				showErrorDlg;
			fi
		fi
	 else
		showKernelSingleInstalledDlg;
	 fi
}

# Executes the command line version of this module
#  $1 Argument [String]
#  $2 Version or Architecture [String]
# Returns
#  *Update [Integer]
#  -  1: Success
#  -  0: Failed
#  - -1: Wrong number of arguments
#  - -2: Kernel already running
#  - -3: Kernel version doesn't exist
#  *Select [String]
#  - * Current kernel version
#  *List [Array]
#  - * Installed kernel versions
#  *Capability
#  - * System ist 64bit capable: 0/1
#  - * System is running|having already 64bit Kernel: 0/1
#  - * Kernel package required to change architecture: linux-image-bcm2836/7
#  - * ZFS module package required to change architecture: linux-zfs-bcm2836/7 or -

function showCMDFn() {
	case $1 in
		"select")
			if [ $# -eq 1 ]; then
				getCurrentKernelVersionFn;
				echo $CURRENTKERNEL;
			else
				shift
				case "$1" in
					'armv7l')  v=0 ;;
					'aarch64') v=1 ;;
					*)         v='' ;;
				esac
				if [ -n "$v" ]; then
					if grep -q 'arm_64bit' /boot/config.txt; then
						sed -i "s/.*arm_64bit.*/arm_64bit=$v/g" /boot/config.txt
					else
						echo "arm_64bit=$v" >> /boot/config.txt
					fi
					echo 1
				else
					echo 0
				fi
			fi
		;;
		"update")
			shift;
			if [ $# -eq 1 ]; then
				getInstalledKernelsFn;
				VERSION=$(echo $1 | sed -e 's/\./\_/g');
				if [ -f "/boot/kernel$VERSION.img" ]; then
					searchArrayFn "/boot/kernel$VERSION.img" "${KERNELS[@]}" 
					RETURN=$?
					setKernelVersionFn ${KERNELS[$RETURN]};
					RETURN=$?
					if [ $RETURN -eq 2 ]; then
						echo -2;
					elif [ $RETURN -eq 1 ]; then
						askForRebootFn;
						echo 1;
					elif [ $RETURN -eq 3 ]; then
						echo -3;
					else
						echo 0;
					fi
				else
					echo -3;
				fi
			else
				echo -1;
			fi
		;;
		"list")
			shift;
			getInstalledKernelVersionsFn;
			if [ ${#KERNELV[@]} -gt 0 ]; then
				for VALUE in ${KERNELV[@]}; do
					echo $VALUE;
				done
			else
				getCurrentKernelVersionFn;
				echo $CURRENTKERNEL;
			fi
		;;
		initramfs)
			initRamFn $2
		;;
		capability)
			lscpu | grep -qE "Cortex-A53|Cortex-A72" && echo 1 || echo 0
			if [ "$(uname -m)" = aarch64 ] || dpkg -l | dpkg -l | grep -E "^hi.[ \t]linux-image-bcm2837|^ii.[ \t]linux-image-bcm2837"; then
				echo 1
				toPackage=bcm2836
				fromPackage=bcm2837
			else
				echo 0
				toPackage=bcm2837
				fromPackage=bcm2836
			fi
			echo "linux-image-$toPackage"
			dpkg-query -s linux-zfs-$fromPackage 2>/dev/null | grep -q "Status: install" && echo linux-zfs-$toPackage || echo '-'
		;;

	esac 
}