#! /bin/bash
#
# Test ability to connect to remote SMB server.
#
# Exits:   0 doesn't seem to have a problem
#          1 remote host $1 or queue $2 not set
#          2 the remote host $1 is unreachable
#          3 no connection possible to port 631 on host $1 (no cups server running ?)
#          4 queue does not accept a print job (queue does not exist or queueing disabled ?)
#
# Johannes Meixner <jsmeix@suse.de>, 2000, 2002
# Jan Holesovsky <kendy@suse.cz>, 2000
# Jiri Srain <jsrain@suse.cz>, 2002
# $Id: test_remote_smb 19346 2004-09-14 10:30:53Z jsrain $

#set -x
MY_NAME=${0##*/}
WORKGROUP=$1
HOST=$2
QUEUE=$3
[ -z "$HOST" -o -z "$QUEUE" ] && { echo -en "\nUsage:\n$MY_NAME WORKGROUP HOST QUEUE [USER] [PASS] [TIMEOUT]\n" 1>&2 ; exit 1 ; }
USER=$4
PASSWORD=$5
TIMEOUT="$6"

[ -z "$TIMEOUT" ] && TIMEOUT=10

# test whether the remote host is accessible
ping -c 1 -w $TIMEOUT $HOST || { echo -en "\nHost $HOST unreachable\n" ; exit 2 ; }

# test the queue and server
echo -e "\nTesting $QUEUE on $WORKGROUP $HOST:"
test -z $PASSWORD && PASSWORD="-N"
echo -en "\r" | smbclient "//$HOST/$QUEUE" "$PASSWORD" -c "print -" -U "$USER" -W "$WORKGROUP"

RES=$?

if [ $RES -eq 0 ];
 then
    exit 0;
else
    exit 4;
fi

