#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=`basename $0`

usage()
{
	echo "
usage: $CMD [options] [host(s)...

options:

  -u	sort using last update timestamp
">&2
	exit 1
}


fmt1=" %-18.18s "
fmt2="%-6.6s %-24.24s %-16.16s %-20.20s\n"
line="----------------------------------------------------"

printf "$fmt1$fmt2" "host" "rel" "kernel" "updated" "status"
printf "$fmt1$fmt2" "$line" "$line" "$line" "$line" "$line"

sortkey=1
sortrev=
hosts=

while [ $# != 0 ]
do
	case $1 in
	  -u)	sortkey=4 ; sortrev="-r" ;;
	  -*)	usage ;;
	  *)	hosts="$hosts $1" ;;
	esac
	shift
done

[ "$hosts" = "" ] && hosts=`ls`


for host in $hosts
do
	[ -d $host ] || continue

	rel=`cat $host/release		2>/dev/null`
	kernel=`cat $host/kernel	2>/dev/null`
	updated=`cat $host/updated	2>/dev/null`
	stat=
	out="$host|$rel|$kernel|$updated"

	if ping -c 1 -w 1 $host 2>/dev/null >/dev/null
	then
		stat="online"
	else
		stat="UNREACHABLE!"
	fi
	case $sortkey in
	   4)	out="$updated|$out" ;;
	   *)	out="$host|$out" ;;
	esac
	echo "$out|$stat"

done | sort $sortrev | while read line
do
	host=`echo "$line" | cut -d'|' -f2`
	rel=`echo "$line" | cut -d'|' -f3`
	kernel=`echo "$line" | cut -d'|' -f4`
	updated=`echo "$line" | cut -d'|' -f5`
	stat=`echo "$line" | cut -d'|' -f6`
	printf "$fmt1$fmt2" "$host" "$rel" "$kernel" "$updated" "$stat"
done


exit 0
