#!/bin/sh
#################################################################################
#
#	File:			pdf-writer
#
#	Description: 	A simple PDF Writer for CUPS 
#
#	Copyright:		(c) 2007  Andrew R (jcoder24 on puppylinux forum)
#	          		(c) 2006  Axel Schmidt,  SUSE LINUX GmbH, Nuernberg, Germany
#				    (c) 2001  Michael Goffioul (kdeprint <at> swing <dot> be)
#
#	License:		GPL
#   2010-05-10 jamesbond - minor patch for fatdog64
#   2010-10-10 kirk - always default save folder to /root/spot/Downloads
#   2010-11-13 kirk - Fatdog uses different hostnames, disable checks.
#   2012-06-15 jamesbond - enable per-user settings again, set DISPLAY to foreground X server
#   2017-03-05 jamesbond - always default save folder to $SPOT_HOME/Downloads
#################################################################################

#------------------------
# BEGIN jcoder24
#------------------------
set_filename()
{
	if [ "$CONVERT_SPACES" = "YES" ]; then
		fname="`basename \"$FILENAME\" | gawk '{ gsub (\"[ ]+\",\"_\",$0); print $0 }'`"
		FILENAME="$PDFPATH/$fname"
		logger "pdf-writer: set_filename fname:$fname Filename:$FILENAME"
	fi
}
#------------------------
# END jcoder24
#------------------------

### jamesbond - add function to get DISPLAY of foreground console
get_DISPLAY() {
	local xline p
	xline=$(ps -o tty=,args= -C X | grep "^tty$(fgconsole)")
	if [ "$xline" ]; then
		DISPLAY=:0
		for p in $xline; do
			case $p in
				*:*) DISPLAY=$p; break ;;
			esac
		done
		export DISPLAY
	fi
}

PS2PDF=`which ps2pdf`
DEFGROUP="users"
DEF_SHARED_DIR="/export/pdfs"
PREFS=".pdfwriter.conf" #jcoder24
JOB=$1
PRTUSER=$2
PRTFILE=$3
JOBOPTIONS=$5	# jamesbond

# Check DEVICE DISCOVERY + test PS2PDF 
#
if [ "$JOB" = "" ]; then
        if [ -f "$PS2PDF" ]; then
                echo "file pdf-writer:/dev/null \"unknown\" \"pdf writer\" " 
                exit 0  
        else
                echo "Error: $0 - ps2pdf is not available!"      
                logger "pdf-writer: ERROR: ps2pdf is not available!"      
                exit 1 
        fi 
fi

logger "pdf-writer started: $1 $2 $3 $4 $5 $6"

# Check number of command line arguments
#
if [ $# -ne 5 -a $# -ne 6 ]; then
        echo "Usage: $0 job-id user title copies options [file]"
        logger "pdf-writer: CRITICAL: Printer stopped !"      
        exit 1
fi

#------------------------
# BEGIN jcoder24
#------------------------

# initialise variables
LOCALE="`locale | grep LANG | cut -f 2 -d =`"
REMOTE_OR_TEST="FALSE"
PUPPY_LINUX=1 #PUPPY_LINUX=`uname -a | grep -ic puppy`  Fatdog uses different host names.

# set path so our script can find all necessary utilites
export PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/X11R7/bin:/bin:/sbin:$PATH
export LD_LIBRARY_PATH="/lib:/lib64:/usr/lib:/usr/lib64:/usr/X11R7/lib:/usr/X11R7/lib64:/root/my-applications/lib:/usr/lib/firefox:/opt/gnome2/lib64:/opt/gnome2/lib" #fatdog

# determine user's home directory - jamesbond for fatdog 600
FOLDER=`awk -F: "/^$PRTUSER/ {print \\$6}" /etc/passwd`
export XAUTHORITY="$FOLDER/.Xauthority"
if [ "$PRTUSER" = "spot" ] ; then #special case for user spot
	SPOT_HOME=$(awk -F: '$1=="spot" {print $6}' /etc/passwd)
	FOLDER="$SPOT_HOME/Downloads"
fi
# if $FOLDER does not exists or user="", we are probably printing remotely
if [ ! -d "$FOLDER" -o -z "$PRTUSER" ]; then
	REMOTE_OR_TEST="TRUE"
	FOLDER=$DEF_SHARED_DIR
fi

# jamesbond - check if job-originating-host-name is anything other than localhost
for p in $JOBOPTIONS; do
	case $p in
		job-originating-host-name=localhost) break ;; # default is local already
		job-originating-host-name=*) REMOTE_OR_TEST="TRUE"; FOLDER=$DEF_SHARED_DIR; break ;;
	esac
done

# get user settings from preferences file
PREF_FILE="$FOLDER/$PREFS"
if [ ! -f "$PREF_FILE" ];then
	MRUD="$FOLDER"
	NOTIFY_ON="YES"
	NOTIFY_TIME="4000"
	NOTIFY_MSG="FULL"
	USE_XWINDOWS="YES"
	CONVERT_SPACES="NO"
	SHARED_DIR=$DEF_SHARED_DIR
	
	echo "MRUD:$MRUD" > $PREF_FILE
	echo "NOTIFY_ON:$NOTIFY_ON" >> $PREF_FILE
	echo "NOTIFY_TIME:$NOTIFY_TIME" >> $PREF_FILE
	echo "NOTIFY_MSG:$NOTIFY_MSG" >> $PREF_FILE
	echo "USE_XWINDOWS:$USE_XWINDOWS" >> $PREF_FILE
	echo "CONVERT_SPACES:$CONVERT_SPACES" >> $PREF_FILE
	echo "SHARED_DIR:$SHARED_DIR" >> $PREF_FILE
	
    chown $PRTUSER:$DEFGROUP $PREF_FILE
    chmod 0664 $PREF_FILE    
else
	MRUD="`grep MRUD $PREF_FILE | cut -f 2 -d :`"
	NOTIFY_ON="`grep NOTIFY_ON $PREF_FILE | cut -f 2 -d :`"
	NOTIFY_TIME="`grep NOTIFY_TIME $PREF_FILE | cut -f 2 -d :`"
	NOTIFY_MSG="`grep NOTIFY_MSG $PREF_FILE | cut -f 2 -d :`"
	USE_XWINDOWS="`grep USE_XWINDOWS $PREF_FILE | cut -f 2 -d :`"
	CONVERT_SPACES="`grep CONVERT_SPACES $PREF_FILE | cut -f 2 -d :`"
	SHARED_DIR="`grep SHARED_DIR $PREF_FILE | cut -f 2 -d :`"
fi

# determine default filename based on the one was passed in to cups
DEF_FILE=`echo $PRTFILE | gawk '{ if (toupper($0) ~ /STDIN/) { print "" } else { gsub ("[^ A-Za-z0-9_.-]+","",$0); print $0 }}'`

if [ "$MRUD" = "/" ]; then
	[ ! -z "$DEF_FILE" ] && DEF_FILE="$DEF_FILE.pdf"
else
	[ ! -z "$DEF_FILE" ] && DEF_FILE="/$DEF_FILE.pdf"
fi

# jamesbond - check display if not remote, if no display don't use X windows
[ "$REMOTE_OR_TEST" != "TRUE" ] && get_DISPLAY 
[ -z $DISPLAY ] && USE_XWINDOWS="NO"

# determine filename when running in normal|xwindows mode
if [ "$USE_XWINDOWS" != "NO" -a "$REMOTE_OR_TEST" = "FALSE" ]; then

	#if we are are not using puppylinux and ~/.Xauthority does not exists, exit gracefully with warning
	if [ "$REMOTE_OR_TEST" != "TRUE" -a ! -f "$XAUTHORITY" -a $PUPPY_LINUX -eq 0 ]; then
		logger "pdf-writer: WARNING: $XAUTHORITY not found! Switch to console mode!"
		exit 0
	fi

	# fix problem with unsupported/missing locales
	if [ ! -z "$LOCALE" -a ! -d "/usr/lib/locale/$LOCALE" ]; then
		export LANG="C"
		export LC_ALL="C"
	fi

	LOOP="TRUE"
	until [ $LOOP = "FALSE" ]; do
		#get filename from user
		FILENAME="$(Xdialog --stdout --title "PDF-Writer - Save As..." --fselect "$MRUD$DEF_FILE" 28 60)"

		#if there is a gtk error, issue warning and exit 1 (stop printer depending on options set by user)
		if [ $? -eq 255 ]; then
			logger "pdf-writer: WARNING: gtk error! Troubleshoot or switch to console mode!"
			exit 1

		#if empty filename, the user cancelled
		elif [ -z "$FILENAME" ]; then
			FILENAME=""
			break

		#remove .pdf extension if supplied
		else
			FILENAME="`echo $FILENAME | gawk '{gsub (/\.pdf$|\.PDF$/,"",$0); print $0'}`"
		fi

		#remove any illegal characters entered by user
		FILENAME=`echo $FILENAME | gawk '{ if (toupper($0) ~ /STDIN/) { print "" } else { gsub ("[^ A-Za-z0-9/_.-]+","",$0); print $0 ".pdf"}}'`

		PDFPATH="`dirname \"$FILENAME\"`" 
		set_filename

		#if selected file exists ask user if to overwrite
		if [ -f "$FILENAME" ]; then 
			Xdialog --title "File exists" --yesno "Overwrite file?" 6 40
			if [ $? -eq 0 ]; then LOOP="FALSE"; fi
		else
			LOOP="FALSE"
		fi
	done

	#if user choses not to save file, exit gracefully
	if [ -z "$FILENAME" -a "$NOTIFY_ON" = "YES" ]; then
		Xdialog --left --title "PDF-Writer" --infobox "Print Job Cancelled" 6 30 4000
		logger "pdf-writer: INFO: JOB $JOB aborted by $PRTUSER!"      
		exit 0
	fi

#Determine filename when running in console mode
else
	if [ -z "$DEF_FILE" ]; then #filename = stdin
		FILENAME="/`date +%Y%m%d-%H%M-%S`.pdf"
	else
		FILENAME="$DEF_FILE"
	fi
	
	if [ "$REMOTE_OR_TEST" = "TRUE" ]; then
		if [ ! -d "$SHARED_DIR" ]; then
			mkdir -p "$SHARED_DIR"
			chown root:$DEFGROUP "$SHARED_DIR"
		fi
		
		FILENAME="$SHARED_DIR$FILENAME"
	else
		FILENAME="$MRUD$FILENAME"
	fi

	PDFPATH="`dirname \"$FILENAME\"`" 
	set_filename
		
	#If file exists, append timestamp
	if [ -f "$FILENAME" ]; then
		FILENAME="`echo $FILENAME | gawk '{ print substr($0,0,length($0)-4) }'`"
		FILENAME="$FILENAME-`date +%Y%m%d-%H%M-%S`.pdf"
	fi

	logger "pdf-writer: REMOTE_OR_TEST: Printed $FILENAME"
fi

#------------------------
# END jcoder24
#------------------------

# Check write status 
#
if [ ! -d "$PDFPATH" -o ! -w "$PDFPATH" ]; then
        logger "pdf-writer: ERROR: directory \"$PDFPATH\" not writeable"
        exit 1
fi

# Run ps2pdf (ghostscript)
#
if [ $# -eq 6 ]; then
        $PS2PDF $6 "$FILENAME" #>& /dev/null #jcoder24 enclosed filename in "" and commented out redirection
else
        $PS2PDF - "$FILENAME" #>& /dev/null #jcoder24 enclosed filename in "" and commented out redirection
fi

# Chown + set permissions for the user 
# Note: this will not work if CUPS runs as lp
if [ "$PRTUSER" != "" ]; then
        chown $PRTUSER:$DEFGROUP "$FILENAME" #jcoder24 enclosed filename in ""
        chmod 644 "$FILENAME"  #jcoder24 enclosed filename in ""        
fi

#------------------------
# BEGIN jcoder24
#------------------------

# update settings with 'new' MRUD settings
if [ "$USE_XWINDOWS" = "YES" ]; then
	cp $PREF_FILE $PREF_FILE.bak
	gawk -F: -v pdfpath="$PDFPATH" '{ if (/MRUD/) {print $1 ":" pdfpath} else {print $0} }' $PREF_FILE.bak > $PREF_FILE
	rm $PREF_FILE.bak
fi

# notify user of job completion if turned on
if [ "$NOTIFY_ON" = "YES" -a "$USE_XWINDOWS" = "YES" -a "$REMOTE_OR_TEST" != "TRUE" ]; then
	if [ "$NOTIFY_MSG" = "FULL" ]; then
		Xdialog --left --title "PDF-Writer" --infobox "Created: $FILENAME" 6 0 $NOTIFY_TIME
	else
		Xdialog --left --title "PDF-Writer" --infobox "Created: `basename \"$FILENAME\"`" 6 0 $NOTIFY_TIME
	fi
fi
#------------------------
# END jcoder24
#------------------------

exit 0

#
#########################  CUPS pdf-writer ends here  ########################
#
