#!/bin/bash
######################################################################
#                                                                    #
#  Copyright (c) 2001, 2025 NoMachine, http://www.nomachine.com.     #
#                                                                    #
#  All rights reserved.                                              #
#                                                                    #
######################################################################

if [ $# = 0 ]
then
  echo "network $(basename $0) \"Unknown\" \"NX Virtual Printing Protocol (nxprint)\""
  exit 0
fi

if [ $# -lt 5 ]
then
  echo "Usage: $0 job-id user title copies options [file]" 1>&2
  exit 4
fi

DEBUG="DEBUG"

echo "$DEBUG: nxprint executed with parameters: $@" 1>&2

JOBID=$1
USER=$2
TITLE=$3
COPIES=$4
OPTIONS=$5

FILE="nxprint-$PRINTER-$JOBID"

if [ "x$TMPDIR" == "x" ]
then
  PSFILE="/tmp/$FILE.ps"
else
  PSFILE="$TMPDIR/$FILE.ps"
fi

echo "$DEBUG: saving temporary postscript file as: $PSFILE" 1>&2

cat - > "$PSFILE"

if test ! -f "$PSFILE";
then
  echo "ERROR: Failed to save a temporary postscript file: $PSFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

NXSESSIONID=`cat "$PSFILE" | grep "%%NXSessionId"| awk -F: '{ print $2 }' | sed '$!d'`
if [ "x$NXSESSIONID" == "x" ]
then
  echo "ERROR: Variable NXSessionId not found in postscript file: $PSFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

NXPRINTER=`cat "$PSFILE" | grep "%%NXPrinter"| awk -F# '{ print $2 }' | sed '$!d'`

echo "$DEBUG: NXPRINTER variable: $NXPRINTER" 1>&2
if [ "x$NXPRINTER" == "x" ]
then
  echo "ERROR: Variable NXPrinter not found in postscript file: $PSFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

NXPATH=`cat "$PSFILE" | grep "%%NXSessionPath"| awk -F: '{ print $2 }' | sed 's/\\\\/\\\\\\\\/g' | sed '$!d'`
echo "$DEBUG: NXPATH variable: $NXPATH" 1>&2
if [ "x$NXPATH" == "x" ]
then
  echo "ERROR: Variable NXPath not found in postscript file: $PSFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

NXUSER=`cat "$PSFILE" | grep "%%NXSessionUser"| awk -F: '{ print $2 }' | sed 's/\\\\/\\\\\\\\/g' | sed '$!d'`
echo "$DEBUG: NXUSER variable: $NXUSER" 1>&2
if [ "x$NXUSER" == "x" ]
then
  echo "ERROR: Variable NXUser not found in postscript file: $PSFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

USER="$NXUSER"
NXPRINTTMP="$NXPATH"

echo "$DEBUG: NX print TMP directory: $NXPRINTTMP" 1>&2
if test ! -d "$NXPRINTTMP";
then
  echo "ERROR: Temporary directory doesn't exist." 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

PDFFILE="$NXPRINTTMP/$FILE.pdf"

echo "$DEBUG: Output PDF file: $PDFFILE" 1>&2

GSEXE=`which gs`

if [ "x$GSEXE" = "x" ]
then
  echo "$DEBUG: Ghostscript not found. Using pstopdf" 1>&2
  OUT=`pstopdf "$PSFILE" -o "$PDFFILE"`
else
  echo "DEBUG:  Found Ghostscript at $GSEXE" 1>&2
fi

echo "$DEBUG: Ghostscript command: $GSEXE -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=\"$PDFFILE\" -dAutoRotatePages=/None -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -c .setpdfwrite -f \"$PSFILE\"" 1>&2
OUT=`"$GSEXE" -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile="$PDFFILE" -dAutoRotatePages=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -c -f "$PSFILE" 1>&2`

if [ $? != 0 ]
then
  echo "ERROR: Ghostscript conversion failed. Exit code was $?." 1>&2
  echo "ERROR: Ghostscript stdout/stderr: $OUT" 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

echo "$DEBUG: Ghostscript conversion successful." 1>&2

rm -f "$PSFILE"
if [ $? != 0 ]
then
  echo "WARNING: Failed to remove temporary postscript file: $PSFILE " 1>&2
fi

chown $USER "$PDFFILE"

if [ $? != 0 ]
then
  echo "ERROR: Failed to change owner of temporary pdf file: $PDFFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

chmod 600 "$PDFFILE"

if [ $? != 0 ]
then
  echo "ERROR: Failed to change permissions of temporary pdf file: $PDFFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

ENV="NXPRINT_PRINTERNAME=\"$NXPRINTER\" NXPRINT_PAGECOPIES=\"$COPIES\""

QUALITY=`echo "$OPTIONS" | grep -o 'Quality=[a-zA-Z0-9]*' | sed -e 's/Quality=//'`
if [ "x$QUALITY" != "x" ] && [ "x$QUALITY" != "x$OPTIONS" ]
then
  ENV="$ENV NXPRINT_PRINTQUALITY=\"$QUALITY\""
fi

RESOLUTION=`echo "$OPTIONS" | grep -o 'Resolution=[a-zA-Z0-9]*' | sed -e 's/Resolution=//'`
if [ "x$RESOLUTION" != "x" ] && [ "x$RESOLUTION" != "x$OPTIONS" ]
then
  ENV="$ENV NXPRINT_PRINTRESOLUTION=\"$RESOLUTION\""
fi

PAGESIZE=`echo "$OPTIONS" | grep -o 'PageSize=[a-zA-Z0-9]*' | sed -e 's/PageSize=//'`
if [ "x$PAGESIZE" != "x" ] && [ "x$PAGESIZE" != "x$OPTIONS" ]
then
  ENV="$ENV NXPRINT_PAGESIZE=\"$PAGESIZE\""
fi

MEDIATYPE=`echo "$OPTIONS" | grep -o 'MediaType=[a-zA-Z0-9]*' | sed -e 's/MediaType=//'`
if [ "x$MEDIATYPE" != "x" ] && [ "x$MEDIATYPE" != "x$OPTIONS" ]
then
  ENV="$ENV NXPRINT_MEDIATYPE=\"$MEDIATYPE\""
fi

INPUTSLOT=`echo "$OPTIONS" | grep -o 'InputSlot=[a-zA-Z0-9]*' | sed -e 's/InputSlot=//'`
if [ "x$INPUTSLOT" != "x" ] && [ "x$INPUTSLOT" != "x$OPTIONS" ]
then
  ENV="$ENV NXPRINT_INPUTSLOT=\"$INPUTSLOT\""
fi

echo "$DEBUG: Environment variables: $ENV" 1>&2


ENVFILE="$NXPRINTTMP/$FILE.env"

OUT3=`echo "$ENV" > "$ENVFILE"`

chown $USER "$ENVFILE"
if [ $? != 0 ]
then
  echo "ERROR: Failed to change owner of temporary postscript file: $ENVFILE " 1>&2
  echo "STATE: +timed-out" 1>&2
  exit 1
fi

echo "$DEBUG: Printing done" 1>&2
exit 0
