#!/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/>

# Get the current file list of installed kernels
# Returns
#  - $KERNELS [Array]
function getInstalledKernelsFn() {
	KERNELS=($(ls -Al /boot/*.img | awk '{print $9}' | grep -ivE "kernel.img|kernel8.img"));
	return ${#KERNELS[@]};
}

# Get the current version list of installed kernels
# Returns
#  - $KERNELV [Array]
function getInstalledKernelVersionsFn() {
	KERNELS=($(ls -Al /boot/*.img | awk '{print $9}' | grep -ivE "kernel.img|kernel8.img"));
	KERNELV=();
	for VALUE in ${KERNELS[@]}; do
		KERNEL=${VALUE##*/};
		KERNEL=${KERNEL%.*}; 
		KERNEL=$(echo $KERNEL | sed -e 's/kernel//g' | sed -e 's/\_/./g');
		KERNELV+=($KERNEL);
	done
}

# Get the current kernel version
# Returns
#  - $CURRENTKERNEL [String]
function getCurrentKernelVersionFn() {
	CKERNEL=$(ls -Al /boot/kernel*.img | grep -E "kernel.img|kernel8.img" | awk '{print $5}');
	NKERNEL=$(ls -Al /boot/*.img | grep -w $CKERNEL 2>/dev/null | awk '{print $9}' | grep -vE "kernel.img|kernel8.img");
	if [ -z $NKERNEL ]; then
		CURRENTKERNEL=$(uname -r | sed -e 's/+//g');
	else
		NKERNEL=${NKERNEL##*/};
		NKERNEL=${NKERNEL%.*}; 
		CURRENTKERNEL=$(echo $NKERNEL | sed -e 's/kernel//g' | sed -e 's/\_/./g');
	fi
}

# Change the current kernel version
#  $1 [Integer] Array index of new kernel
# Returns
#  - 0: Failed
#  - 1: Success
#  - 2: Already running this kernel version
#  - 3: Kernel version isn't installed
function setKernelVersionFn() {
	KERNEL=$1;

	NKERNEL=$(ls -Al $KERNEL | awk '{print $5}');
	CKERNEL=$(ls -Al /boot/kernel*.img | grep -E "kernel.img|kernel8.img" | awk '{print $5}');
	if [ $NKERNEL -eq $CKERNEL ]; then
		return 2;
	else
		if [ -f $KERNEL ]; then
			cp $KERNEL /boot/kernel.img
			getCurrentKernelVersionFn;
			KERNEL=${KERNEL##*/};
			KERNEL=${KERNEL%.*}; 
			KERNEL=$(echo $KERNEL | sed -e 's/kernel//g' | sed -e 's/\_/./g')
			if [ $KERNEL == $CURRENTKERNEL ]; then
				echo "Updating initramfs, please wait…"
				xbian-update-initramfs "$KERNEL+" >/dev/null
				return 1;
			else
				return 0;
			fi
		else
			return 3;
		fi
	fi 
}

# Parse the overclock values in a menu structure
#  $1 [Array] Current kernel version
#  $2 [Array] Installed kernels
# Returns
#  $KERNELMENU [Array]
#  - * Rows of the kernel menu
function createKernelsMenu() {
	CURRENTKERNEL=$1;
	KERNELS=$2;
	KERNELMENU=()
	I=0;
	for VALUE in ${KERNELS[@]}; do
		I=$(($I+1));
		VALUE=${VALUE##*/};
		VALUE=${VALUE%.*}; 
		VALUE=$(echo $VALUE | sed -e 's/kernel//g' | sed -e 's/\_/./g'); 
		if [ $CURRENTKERNEL == $VALUE ]; then
			KERNELMENU+=($I",\Zn"$VALUE",on");
		else
			KERNELMENU+=($I",\Zn"$VALUE",off");
		fi
	done;
}

initRamFn() {
    case $1 in
        '')
            grep -q ^FORCEINITRAM= /etc/default/xbian-initramfs && grep ^FORCEINITRAM= /etc/default/xbian-initramfs | cut -d '=' -f2 || echo "no"
            ;;
        *)
            eval sed -i 's/FORCEINITRAM=.*/FORCEINITRAM=$1/' /etc/default/xbian-initramfs
            [ -x /etc/xbian-initramfs/initram.switcher.sh ] && /etc/xbian-initramfs/initram.switcher.sh update >/dev/null
            echo "1"
            ;;
    esac
}
