#! /bin/bash
#
# Test ability to connect to remote IPX Novell server.
#
# Exits:   0 queue on host accepts print jobs
#          1 host $1 or queue $2 not set
#          4 queue does not accept a print job but host responds to a 'ping'
#         10 queue does not accept a print job and ping not executable (no iputils RPM installed?)
#         (11=netcat,12=fuser,13=mktemp,14=sed,15=lp,16=smbclient: see test_remote_smb)
#         17 give up empty-handed because nprint not executable (no ncpfs RPM installed?)
#
# Johannes Meixner <jsmeix@suse.de>, 2000, 2002, 2007, 2008, 2009, 2010, 2011
# Jan Holesovsky <kendy@suse.cz>, 2000
# Jiri Srain <jsrain@suse.cz>, 2002
# $Id: test_remote_novell 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
# Disable bash file name globbing:
set -f

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

# Use the binaries of the operating system (no aliases, functions, /usr/local/):
export NPRINT=$( type -ap nprint | head -n 1 )

# Test whether nprint is executable:
if test -z "$NPRINT"
then # Give up empty-handed when nprint is not executable because only the ping test is meaningless:
     echo -en "\nGiving up empty-handed because 'nprint' not executable (no 'ncpfs' RPM installed?)\n" 1>&2
     exit 17
fi
# Test whether the queue on the server accepts print jobs:
echo -en "\nTesting queue '$QUEUE' on host '$HOST':\n"
if echo -en "\r" | $NPRINT -S "$HOST" -U "$USER" -P "$PASSWORD" -q "$QUEUE" - 2>&1
then echo -en "\nQueue '$QUEUE' on host '$HOST' accepts print jobs\n"
     exit 0
fi

# The nprint test failed:
echo -en "\nQueue '$QUEUE' on host '$HOST' does not accept print jobs (queue may not exist or queueing disabled?)\n\n"

# Test whether ping is executable:
if test -z "$PING"
then # ping is not executable and the nprint test failed:
     echo -en "\n'ping' not executable (no 'iputils' RPM installed?)\n" 1>&2
     exit 10
fi
# Test whether host is accessible:
if $PING -c 1 -w $TIMEOUT $HOST
then # The ping test was successful but the nprint test failed:
     echo -en "\nHost '$HOST' is accessible (responds to a 'ping')\n"
     exit 4
fi

# Both the nprint test and the ping test failed:
echo -en "\nHost '$HOST' unreachable (network issue or wrong host or firewall active?)\n"
exit 2

