#! /bin/bash
#
# Test ability to connect to remote IPP 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 ?)
#         10 ping not executable (no iputils RPM installed?)
#         11 netcat not executable (no netcat RPM installed?)
#         (12=fuser,13=mktemp,14=sed: see listen_remote_ipp)
#         15 lp not executable (no cups-client RPM installed?)
# The program head is 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_ipp 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 NETCAT=$( type -ap netcat | head -n 1 )
[ -z "$NETCAT" ] && { echo -en "\nnetcat not executable\n" 1>&2 ; exit 11 ; }
export LP=$( type -ap lp | head -n 1 )
[ -z "$LP" ] && { echo -en "\nlp not executable\n" 1>&2 ; exit 15 ; }

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 ; }
TIMEOUT="$3"
[ -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 connection is possible to port 631 (ipp) on the remote host
$NETCAT -w $TIMEOUT -z $HOST 631 || { echo -en "\nNo connection possible to port 631 (IPP) on host $HOST\n" ; exit 3 ; }

# test whether the queue on the server accepts print jobs
echo -en "\r" | $LP -d $QUEUE -h $HOST 2>&1
[ "$?" = "0" ] && { echo -en "\nQueue $QUEUE on host $HOST accepts print jobs\n" ; exit 0 ; }
echo -en "\nQueue $QUEUE on host $HOST does not accept print jobs\n"
exit 4

