#!/bin/sh
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Mar. 2004
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
# --------------------------------------------------------------------------
# 
# sends a NetBIOS popup message to an IP address, after figuring out the remote
# host's name
# needs smbclient and nmblookup

if [ $# -ne 3 ]; then
  echo "Usage: $0 <recipient IP> <sender name> <text>"
  exit 1
fi

RECIPIENT="$1"
SENDER="$2"
TEXT="$3"

# try to find out the NetBIOS name of the remote host
remotename=`/usr/bin/nmblookup -A "$RECIPIENT" 2>/dev/null | \
	grep -A 1 "Looking up status of" | tail -n 1 | awk '{print $1;}'`
if [ $? -ne 0 ] || [ -z "$remotename" ]; then
  echo "Unable to query NetBIOS name of remote host !"
  exit 2
fi

if ! echo "$TEXT" | smbclient -M "$remotename" -I "$RECIPIENT" -U "$SENDER" \
	-n "Gibraltar" >/dev/null; then
  echo "Unable to send message to NetBIOS host '$remotename' ($RECIPIENT)"
  exit 3
fi

exit 0
