#!/bin/sh

if [ ! -n "$REMOTE_PRINT_HOST" ] ; then
    echo "\$REMOTE_PRINT_HOST is not set." >&2
    exit 1
fi

if [ ! -n "$REMOTE_PRINT_PATH" ] ; then
    echo "\$REMOTE_PRINT_PATH is not set." >&2
    exit 1
fi

if [ $# -lt 1 ] ; then
    echo "$0 file-to-print lp-options*" >&2
    exit 1
fi

FILE=$1
shift
BASE_FILE_NAME=$(basename ${FILE})

if [ ! -e "$FILE" ] ; then
    echo "$FILE does not exist." >&2
    exit 1
fi

scp ${FILE} "${REMOTE_PRINT_HOST}:${REMOTE_PRINT_PATH}${BASE_FILE_NAME}"

if [ $? -eq 0 ] ; then
    ssh "$REMOTE_PRINT_HOST" -t "lp $@ ${REMOTE_PRINT_PATH}${BASE_FILE_NAME}"
else
    echo "File Transfer Failed." >&2
    exit 1
fi

if [ $? -eq 0 ] ; then
    ssh "${REMOTE_PRINT_HOST}" -t "rm ${REMOTE_PRINT_PATH}${BASE_FILE_NAME}"
fi
