#! /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
#          4 queue does not accept a print job (queue does not exist or queueing disabled ?)
#         10 ping not executable (no iputils RPM installed?)
#         (11=netcat,12=fuser,13=mktemp,14=sed,15=lp: see test_remote_ipp)
#         16 smbclient not executable (no samba-client RPM installed?)
# The programs head, mkfifo, sleep, tr, rm are in the coreutils RPM and therefore assumed to exist.
#
# Johannes Meixner <jsmeix@suse.de>, 2000, 2002, 2007, 2008
# Jan Holesovsky <kendy@suse.cz>, 2000
# Jiri Srain <jsrain@suse.cz>, 2002
# $Id: test_remote_smb 43943 2008-01-28 13:38:58Z mzugec $

#set -x

# Make sure to have a clean environment:
export PATH="/sbin:/usr/sbin:/usr/bin:/bin"
export LC_ALL="POSIX"
export LANG="POSIX"
umask 022

# Use the binaries of the operating system (no aliases, functions, /usr/local/):
export PING=$( type -ap ping | head -n 1 )
[ -z "$PING" ] && { echo -en "\nping not executable\n" 1>&2 ; exit 10 ; }
export SMBCLIENT=$( type -ap smbclient | head -n 1 )
[ -z "$SMBCLIENT" ] && { echo -en "\nsmbclient not executable\n" 1>&2 ; exit 16 ; }

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 whether the queue on the server accepts print jobs
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"
[ "$?" = "0" ] && { echo -en "\nShare $QUEUE on $WORKGROUP/$HOST accepts print jobs\n" ; exit 0 ; }
echo -en "\nShare $QUEUE on $WORKGROUP/$HOST does not accept print jobs\n"
exit 4

