#!/bin/sh


usage() {
    cat<<EOF
Send events to the whereami service

-d, --daemon         The pid of the whereami service to notify. If not
                     specified the process list will be searched for any
                     matching service daemon

-p, --update-peers   Tell the whereami service to update it's peers list

EOF
}


update_peers() {
    [ "${1}" ] || {
        echo 'No daemon found'
        return 1
    }
    [ -e /proc/"${1}" ] || {
        echo "No running daemon found ${daemon_pid}"
        return 1
    }
    echo "Whereami service pid is : ${daemon_pid}"
    kill -USR1  ${1}
}


#
# A tool to send notifications to the ohiwhereami service
#
DAEMON_NAME="ohiwhereamid"
daemon_pid="$(pgrep ${DAEMON_NAME})"

SHORTOPTS="hpd:"
LONGOPTS="update-peers,daemon-pid:,help"
ARGS=$(getopt --shell sh --options $SHORTOPTS \
              --longoptions $LONGOPTS \
              --name $0 -- "$@" )
eval set -- "$ARGS"

while true; do
    case $1 in
        -h|--help)      usage; exit 0 ;;
        -p|--update-peers) action="update-peers" ;;
        -d|--daemon-pid) shift; daemon_pid="${1}" ;;
        --) shift; break ;;
        *) shift; break ;;
    esac
    shift
done

case $action in
    "update-peers" )
        update_peers ${daemon_pid}
    ;;
    *)
        break
esac


exit 0
