#! /bin/sh
# Ping a host - helper
#
# This program is a part of Uptime package.
#

# __author__ = "Oleg Broytman <phd@phdru.name>"
# __copyright__ = "Copyright (C) 2003-2013 PhiloSoft Design"
# __license__ = "GNU GPL"


usage() {
   echo "Usage: isalive [-v] host"
   exit 1
}

host=
verbose=no

[ "$1" ] || usage
if [ "$1" = "-v" ]; then
   verbose=yes
   shift
fi
if [ "$1" ]; then
   host="$1"
else
   usage
fi


# Ping, test result and report status
/bin/ping -n -c 5 "$host" |
   /usr/bin/awk '/received/ {x = $4} END {exit(x)}'
ALIVE=$?

if [ "$verbose" = "yes" ]; then
   [ "$ALIVE" \!= 0 ] && echo "$host is alive ($ALIVE)" || echo "$host is dead ($ALIVE)"
fi

exit $ALIVE
