#!/bin/bash

LANG=es_ES.UTF-8
TERM=xterm
export LANG 
export TERM
HORA=`date +%r`
UPTIME=`uptime`
VER=`lliurex-version -n`
SB_PATH="/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"

detectmem () {
	hw_mem=0
	free_mem=0
	human=1024
	mem_info=$(</proc/meminfo)
	mem_info=$(echo $(echo $(mem_info=${mem_info// /}; echo ${mem_info//kB/})))
	for m in $mem_info; do
		if [[ ${m//:*} = MemTotal ]]; then
			memtotal=${m//*:}
		fi

		if [[ ${m//:*} = MemFree ]]; then
			memfree=${m//*:}
		fi

		if [[ ${m//:*} = Buffers ]]; then
			membuffer=${m//*:}
		fi

		if [[ ${m//:*} = Cached ]]; then
			memcached=${m//*:}
		fi
	done

	usedmem="$(((($memtotal - $memfree) - $membuffer - $memcached) / $human))"
	totalmem="$(($memtotal / $human))"
	mem="${usedmem}MiB / ${totalmem}MiB"
}

detectcpu () {
	REGEXP="-r"
	cpu=$(awk 'BEGIN{FS=":"} /model name/ { print $2; exit }' /proc/cpuinfo | sed 's/ @/\n/' | head -1)
	cpun=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l)

	if [ -z "$cpu" ]; then
		cpu=$(awk 'BEGIN{FS=":"} /Hardware/ { print $2; exit }' /proc/cpuinfo)
	fi

	if [ -z "$cpu" ]; then
		cpu=$(awk 'BEGIN{FS=":"} /^cpu/ { gsub(/  +/," ",$2); print $2; exit}' /proc/cpuinfo | sed 's/, altivec supported//;s/^ //')
		if [[ $cpu =~ ^(PPC)*9.+ ]]; then
			model="IBM PowerPC G5 "
		elif [[ $cpu =~ 740/750 ]]; then
			model="IBM PowerPC G3 "
		elif [[ $cpu =~ ^74.+ ]]; then
			model="Motorola PowerPC G4 "
		elif [[ "$(cat /proc/cpuinfo)" =~ "BCM2708" ]]; then
			model="Broadcom BCM2835 ARM1176JZF-S"
		else
			model="IBM PowerPC G3 "
		fi
		cpu="${model}${cpu}"
	fi

	loc="/sys/devices/system/cpu/cpu0/cpufreq"

	if [ -f ${loc}/bios_limit ];then
		cpu_mhz=$(awk '{print $1/1000}' "${loc}/bios_limit")
	elif [ -f $loc/scaling_max_freq ];then
		cpu_mhz=$(awk '{print $1/1000}' "${loc}/scaling_max_freq")
	else
		cpu_mhz=$(awk -F':' '/cpu MHz/{ print int($2+.5) }' /proc/cpuinfo | head -n 1)
	fi

	if [ -n "$cpu_mhz" ];then
		if [ $(echo $cpu_mhz | cut -d. -f1) -gt 999 ];then
			cpu_ghz=$(awk '{print $1/1000}' <<< "${cpu_mhz}")
			cpu="$cpu @ ${cpu_ghz}GHz"
		else
			cpu="$cpu @ ${cpu_mhz}MHz"
		fi
	fi

	if [[ "${cpun}" -gt "1" ]]; then
			cpu=$(sed $REGEXP 's/\([tT][mM]\)|\([Rr]\)|[pP]rocessor//g' <<< "${cpun}x ${cpu}" | xargs)
	else
			cpu=$(sed $REGEXP 's/\([tT][mM]\)|\([Rr]\)|[pP]rocessor//g' <<< "${cpu}" | xargs)
	fi
}


lliurex-version > /tmp/test-mod.txt
echo $LTSP_FATCLIENT > /tmp/test-mod-fat.txt
if [ `grep server /tmp/test-mod.txt -c` != 0 ]; then
	if [ `echo x$DISPLAY | cut -d : -f 1` = 'x' ]; then
		SOY=Servidor
	else
		SOY=Ligero
	fi
else
	if [ `grep client /tmp/test-mod.txt -c ` != 0 ]; then
		#EJECUTA=`echo $LTSP_FATCLIENT | grep true| wc -l`
		EJECUTA=$(grep "true" /tmp/test-mod-fat.txt -c)
		if [ "$EJECUTA" != "0" ];then
			SOY=Semiligero
		else
			SOY=Pesado
		fi
	else
		SOY=Desktop
	fi
fi

if [ `uname -m | grep "x86_64" -c` == 0 ]; then
	ARQ="32 Bits"
else
	ARQ="64 Bits"
fi

if [ -e $SB_PATH ]; then
	BOOT_MODE="UEFI"
else
	BOOT_MODE="Legacy"
fi

detectmem
detectcpu

echo 
printf "\t\u2022 Sabor: $SOY\n"
printf "\t\u2022 Versión: $VER\n"
printf "\t\u2022 Arquitectura: $ARQ\n"
printf "\t\u2022 Cpu: $cpu\n"
printf "\t\u2022 Ram: $mem\n"
printf "\t\u2022 Arranque: $BOOT_MODE\n"

if [ "$BOOT_MODE" = "UEFI" ]; then
	SB_STATUS=`od -j4 -tx1 -An $SB_PATH`

	if [ "$SB_STATUS" = "01" ]; then
		SB_STATUS="Activado"
	else
		SB_STATUS="Desactivado"
	fi

	printf "\t\u2022 Secure Boot: $SB_STATUS\n"
fi

if [ "$SOY" = "Server" ]; then

	IMG=`n4d-client call LmdImageManager.getImageList`
	ORD=`n4d-client call LlxBootManager.getBootOrder`
	IMG2=`echo $IMG|sed 's/\[\|\]//g'|sed 's/"//g'|sed 's/,/ /g'|sed 's/.json//g'`
	ORD2=`echo $ORD|sed 's/\[\|\]//g'|sed 's/"//g'|sed 's/,/ /g'|sed "s/'//g"`

	DF=`df -h /net|tail -1|awk '{print $5}'`
	printf "\t\u2022 Espacio ocupado en /net: $DF\n"
	printf "\t\u2022 Imágenes LTSP disponibles: $IMG2\n"
	printf "\t\u2022 Orden de arranque menu pxe: $ORD2\n"
	echo 
fi

printf "\t\u2022 Uptime: $UPTIME\n\n"
