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

show_uptime_help ()
{

	white=$(bs_ng_echo $white)
	orange=$(bs_ng_echo $orange)
	green=$(bs_ng_echo $green)

	echo -e "\n${white}Usage:\n"
	echo -e "${orange}show_uptime${white} |${green} ! no options !\n"
	tput sgr0

}

echotime () {
	timeused=$1

	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"
}

show_uptime () {

	case $1 in

		*help )
			show_uptime_help
		;;

		* )
			uptime=$(</proc/uptime)
			echotime ${uptime%%.*}
		;;

	esac

}
