#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2017 Christopher Bratusek		#
#							#
#########################################################

case ${1} in

	battery)
		if ! which acpi; then
			echo -e "acpi not found, 'systemkit battery' won't work."
			exit 1
		fi

		load=$(acpi -b | sed -e "s/.* \([1-9][0-9]*\)%.*/\1/")
		out="$(acpi -b)"
		state="$(echo "${out}" | awk '{print $3}')"
		case ${state} in
			charging,)	statesign="^" ;;
			discharging,)	statesign="v" ;;
			charged,)	statesign="°" ;;
		esac
		echo "${statesign}${load}"
	;;

	cpuload)
		NICE_IGNORE=20
		t="0"

		while read -r cpu ni; do
			[[ $ni == *-* || $ni -le ${NICE_IGNORE} ]] &&	t="$t + ${cpu}"
			[[ ${cpu%%.*} -eq 0 ]] && break
		done < <(ps -Ao "%cpu= ni="| sort -r)

		cpu=$(echo "$t" | bc)
		[[ ! "${cpu#.}x" = "${cpu}x" ]] && cpu="0${cpu}"
		cpu=${cpu%%.*}

		if [[ ${cpu} -gt 100 ]]; then
			cpu=100
		elif [[ ${cpu} -lt 10 ]]; then
			prepend=00
		elif [[ ${cpu} -lt 100  ]]; then
			prepend=0
		fi

		echo ${prepend}${cpu}
	;;

	externalip)
		if ! which wget; then
			echo -e "wget not found, 'systemkit externalip' won't work."
			exit 1
		fi
		wget -q -O - http://showip.spamt.net/
	;;

	internalip)
		LANG=C /sbin/ifconfig "${2}" | grep 'inet addr:' | \
			cut -d: -f2 | gawk '{ print $1}'
	;;

	usedram)
		temp=$(top -bn1 | gawk '/Mem :/{print "(" $4 "-" $6 ") /1024"}' | bc)
		if [[ ${temp} -lt 10 ]]; then
			echo "000${temp}"
		elif [[ ${temp} -lt 100 ]]; then
			echo "00${temp}"
		elif [[ ${temp} -lt 1000 ]]; then
			echo "0${temp}"
		else	echo "${temp}"
		fi
	;;

	freeram)
		temp=$(top -bn1 | gawk '/Mem :/{print $6 "/1024"}' | bc)
		if [[ ${temp} -lt 10 ]]; then
			echo "000${temp}"
		elif [[ ${temp} -lt 100 ]]; then
			echo "00${temp}"
		elif [[ ${temp} -lt 1000 ]]; then
			echo "0${temp}"
		else	echo "${temp}"
		fi
	;;

	totalram)
		temp=$(top -bn1 | gawk '/Mem :/{print $4 "/1024"}' | bc)
		if [[ ${temp} -lt 10 ]]; then
			echo "000${temp}"
		elif [[ ${temp} -lt 100 ]]; then
			echo "00${temp}"
		elif [[ ${temp} -lt 1000 ]]; then
			echo "0${temp}"
		else	echo "${temp}"
		fi
	;;

	usedram%)	echo "$(top -bn1 | gawk '/Mem :/{print "100*(" $4 "-" $6 ")/" $4}' | bc)%" ;;
	freeram%)	echo "$(top -bn1 | gawk '/Mem :/{print "100 *" $6 "/" $4}' | bc)%" ;;

	dirsize)
		TotalBytes=$(du . | awk '{print $1}')

		if [[ ${TotalBytes} -lt 1024 ]]; then
				echo "${TotalBytes} KB"
		elif [[ ${TotalBytes} -lt 1048576 ]]; then
				echo "$((TotalBytes/1024)) MB"
		elif [[ ${TotalBytes} -lt 1073741824 ]]; then
				echo "$((TotalBytes/1048576)) GB"
		else	echo "$((TotalBytes/1073741824)) TB"
		fi
	;;

	usedspace)	echo "$(df -h | grep -w "${2}" | gawk '{print $3}')B" ;;
	freespace)	echo "$(df -h | grep -w "${2}" | gawk '{print $4}')B" ;;
	totalspace)	echo "$(df -h | grep -w "${2}" | gawk '{print $2}')B" ;;
	usedspace%)	df | grep -w "${2}" | gawk '{print $5}' ;;
	freespace%)	temp=$(df | grep -w "${2}" | gawk '{print $5}')
			echo "$((100-${temp//%/}))%" ;;

	cpu)
		echo -e "CPU:
	Model:$(gawk -F : '/model name/{print $2;exit;}' /proc/cpuinfo)
	MHz  :$(gawk -F : '/cpu MHz/{print $2;exit;}' /proc/cpuinfo)\n"
	;;

	kernel)
		echo -e "Kernel:
	Release: $(uname -r)
	Version: $(uname -v)
	Machine: $(uname -m)\n"
	;;

	partitions)
		echo -e "Partitions:
	device-node type mount used free total
	$(df -h --output=source,fstype,target,used,avail,size | grep "^/dev")\n" | column -t
	;;

	pci)
		if ! which lspci; then
			echo -e "lspci not found, 'systemkit lspci' won't work."
			exit 1
		fi
		echo -e "PCI Devices:
$(lspci -vkmm)\n"
	;;

	usb)
		if ! which lsusb; then
			echo -e "lsusb not found, 'systemkit usb' won't work."
			exit 1
		fi
		echo -e "USB Devices:
$(lsusb -v)\n"
	;;

	bios)
		if ! which dmidecode; then
			echo -e "dmidecode not found, 'systemkit bios' won't work."
			exit 1
		fi

		if [[ ${EUID} == 0 ]]; then 
			echo -e "SMBIOS/DMI Info:
$(dmidecode -q)\n"
		fi
	;;

	load1)	LANG=C uptime | sed -e 's/.*average://;s/[[:space:]]//g' | gawk -F , '{print $1}' ;;
	load5)	LANG=C uptime | sed -e 's/.*average://;s/[[:space:]]//g' | gawk -F , '{print $2}' ;;
	load15)	LANG=C uptime | sed -e 's/.*average://;s/[[:space:]]//g' | gawk -F , '{print $3}' ;;

	tty)
		TTY=$(tty)
		echo "${TTY:5}"
	;;
	
	uptime)
		uptime=$(</proc/uptime)
		timeused=${uptime%%.*}

		if (( timeused > 86400 )); then
		((
			daysused=timeused/86400,
			hoursused=timeused/3600-daysused*24,
			minutesused=timeused/60-hoursused*60-daysused*60*24,
			secondsused=timeused-minutesused*60-hoursused*3600-daysused*3600*24
		))
			if (( hoursused < 10 )); then
				hoursused=0${hoursused}
			fi
			if (( minutesused < 10 )); then
				minutesused=0${minutesused}
			fi
			if (( secondsused < 10 )); then
				secondsused=0${secondsused}
			fi
			output="${daysused}d ${hoursused}h:${minutesused}m:${secondsused}s"
		elif (( timeused < 10 )); then
			output="0d 00h:00m:0$(timeused)s"
		elif (( timeused < 60 )); then
			output="0d 00h:00m:${timeused}s"
		elif (( timeused < 3600 )); then
		((
			minutesused=timeused/60,
			secondsused=timeused-minutesused*60
		))
			if (( minutesused < 10 )); then
				minutesused=0${minutesused}
			fi
			if (( secondsused < 10 )); then
				secondsused=0${secondsused}
			fi
			output="0d 00h:${minutesused}m:${secondsused}s"
		elif (( timeused < 86400 )); then
		((
			hoursused=timeused/3600,
			minutesused=timeused/60-hoursused*60,
			secondsused=timeused-minutesused*60-hoursused*3600
		))
			if (( hoursused < 10 )); then
				hoursused=0${hoursused}
			fi
			if (( minutesused < 10 )); then
				minutesused=0${minutesused}
			fi
			if (( secondsused < 10 )); then
				secondsused=0${secondsused}
			fi
			output="0d ${hoursused}h:${minutesused}m:${secondsused}s"
		fi

		echo "$output"
	;;

	processes)
		procs=$(ps ax | wc -l | awk '{print $1}')

		if [[ ${procs} -lt 10 ]]; then
			echo "000${procs}"
		elif [[ ${procs} -lt 100 ]]; then
			echo "00${procs}"
		elif [[ ${procs} -lt 1000 ]]; then
			echo "0${procs}"
		fi
	;;

	countoverallfiles)	echo $(($(find "${PWD}" -maxdepth 1 | wc -l )-1)) ;;
	countoveralldirs)	echo $(($(find "${PWD}" -maxdepth 1 -type d | wc -l )-1)) ;;
	countoverallitems)	echo $(($(find "${PWD}" -maxdepth 1 -type f | wc -l )-1)) ;;
	countvisiblefiles)	echo $(($(find "${PWD}" -maxdepth 1 -type f -name ".*" -prune -o -print | wc -l)-1)) ;;
	countvisibledirs)	echo $(($(find "${PWD}" -maxdepth 1 -type d -name ".*" -prune -o -print | wc -l)-1)) ;;
	countvisibleitems)	echo $(($(find "${PWD}" -maxdepth 1 -name ".*" -prune -o -print| wc -l)-1)) ;;

	*)
		bashstyle-help -a "Christopher Roy Bratusek" -e "nano@jpberlin.de" -h "http://www.nanolx.org/"\
			-l "GNU GPL v3" -n "systemkit" -s "show various system information" -v "${BSNG_VERSION}" -y "${BSNG_YEAR}"\
			-o "battery:|show battery load state using acpi"\
			-o "cpuload:|show cpu load"\
			-o "externalip:|show your PCs external ip"\
			-o "internalip:interface|show interface's internal ip"\
			-o "usedram:|used RAM"\
			-o "usedram%:|used RAM in %"\
			-o "freeram:|free RAM"\
			-o "freeram%:|free RAM in %"\
			-o "totalram:|total RAM"\
			-o "dirsize:|size of all items in current dir"\
			-o "usedspace:mount|used space on mountpoint"\
			-o "freespace:mount|free space on mountpoint"\
			-o "totalspace:mount|total space on mountpoint"\
			-o "usedspace%:mount|used space on mountpoint in %"\
			-o "freespace%:mount|free space on mountpoint in %"\
			-o "cpu:|show cpu information"\
			-o "kernel:|show kernel information"\
			-o "partitons:|show mounted partitions information"\
			-o "pci:|show pci device information"\
			-o "usb:|show usb device information"\
			-o "bios:|show bios information [only root]"\
			-o "load1:|show load average for 1 minute"\
			-o "load5:|show load average for 5 minutes"\
			-o "load15:|show load average for 15 minutes"\
			-o "tty:|show current TTY device node"\
			-o "uptime:|show uptime in pretty printed format"\
			-o "countoverallfiles:[directory]|count all files in directory (or pwd)"\
			-o "countoveralldirs:[directory]|count all directories in directory (or pwd)"\
			-o "countoverallitems:[directory]|count all items in directory (or pwd)"\
			-o "countvisiblefiles:[directory]|count visible files in directory (or pwd)"\
			-o "countvisibledirs:[directory]|count visible directories in directory (or pwd)"\
			-o "countvisibleitems:[directory]|count visible items in directory (or pwd)"
	;;

esac
