ACC SHELL

Path : /proc/self/root/usr/bin/
File Upload :
Current File : //proc/self/root/usr/bin/smbprngenpdf

#! /bin/bash
# Copyright (c) 2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# $Id: smbprngenpdf,v 1.3 2003/07/30 16:30:40 lmuelle Exp $
#
# Author: Martin Rode, Programmfabrik GmbH, http://www.programmfabrik.de/
#         <martin.rode@programmfabrik.de>
#         Lars Mueller <lmuelle@SuSE.de>, 2003
#
# Based on Martin Rode's work as published in the iX magacine 2003-03, 139 ff.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GPL.
#

BASENAME=$( basename $0)
PATH="/usr/bin:/bin"

# decide if we use stdout or logger for the debug messages
if [ /dev/stdout -ef /dev/null ]; then
	debug_cmd="logger -t ${BASENAME} -- \$*"
else
	debug_cmd="echo \$2"
fi

# set a default debug level
declare -i DEBUG=2
function debug()
{
	# return if the message's debug level is greater than the current
	test $1 -gt ${DEBUG} && return

	eval ${debug_cmd}
	return
}

function backup()
{
	# if no such file exits or no backups are wanted return
	if [ ! -e "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.$1" ] || [ ${BACKUP_AMOUNT} -eq 0 ]; then
		return
	fi
	# check if we must shift the backup files
	declare -i filenum=0 maxfilenum=0
	IFS='
'
	for file in $( find ${DOCUMENT_DEST_DIR} -type f -regex "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-[0-9]+.$1"); do
		rest=${file#${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-}
		filenum=$( echo ${rest%.$1} | egrep "[0-9]+")
		debug 7 "${LINENO} file:<${file}> rest:<${rest}> filenum:<${filenum}>"
		test ${filenum} -gt $maxfilenum && maxfilenum=${filenum}
	done
	debug 5 "${LINENO} backupamount:<${BACKUP_AMOUNT}> maxfilenum:<${maxfilenum}>"
	test ${maxfilenum} -ge ${BACKUP_AMOUNT} && maxfilenum=$[BACKUP_AMOUNT-1]
	while [ ${maxfilenum} -ge 1 ]; do
		test -e "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-${maxfilenum}.$1" && \
			mv "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-${maxfilenum}.$1" "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-$[maxfilenum+1].$1"
		maxfilenum=$[maxfilenum-1]
	done
	mv "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.$1" "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-1.$1"
}

function usage()
{
	cat <<EOM
${BASENAME} is thought to convert a SMB print spool file to a PDF.

By default the destionation directory is a sub directory of the users home
directory, ~/PDF/. Due to privacy the create mode setting of the Samba share is
taken in account while creating the sub diretory and the document file. The sub
directory could be changed with the -D option.

An alternate destionation directory could be set with -p. Then no default sub
directory is set.

As an intermediate step a PostScript file is created. You could decide if this
file should be kept with the -k option.

${BASENAME} options:

    -D      destination sub directory
    -J      job name as provided by the %j macro
    -b      amount of backups kept; default are 10
    -c      print job page count as provided by the %c macro
    -d      set a debug level to write messages to the syslog
    -k      keep the PostScript file
    -p      absolute path to the destination directory
    -h      this text
    -s      spool file name as provided by the %s macro
    -u      user name as provided by the %u macro
    -z      spool file size as provided by the %z macro
EOM
	exit $1
}

# set defaults
declare -i BACKUP_AMOUNT="5"
KEEP_PS_FILE=""
SPOOL_FILE_NAME=""

while getopts :D:J:b:c:d:ihkp:s:u:z: opt ; do
	case ${opt} in
		\:|\?)  case ${OPTARG} in
				D)	debug 1 "-D requires a sub directory"
					;;
				J)	debug 1 "-J requires a job name"
					;;
				b)	debug 1 "-b requires a numerical value for the backup amount"
					;;
				c)	debug 1 "-c requires the ammount of pages"
					;;
				d)	debug 1 "-d requires a numerical debug level"
					;;
				p)	debug 1 "-p requires a directory name"
					;;
				s)	debug 1 "-s requires a spool file"
					;;
				u)	debug 1 "-u requires a user name"
					;;
				z)	debug 1 "-z requires the spool file size"
					;;
				*)	echo "Unknown option: -${OPTARG}"
					;;
			esac
			exit 1
			;;
		D)	DEST_SUB_DIR=${OPTARG}
			;;
		J)	JOB_NAME=${OPTARG}
			;;
		b)	if echo ${OPTARG} | egrep "[^0-9]" >/dev/null; then
				debug 1 "-b requires a numerical value not <${OPTARG}> for the backup amount"
				exit 1
			fi
			BACKUP_AMOUNT=${OPTARG}
			;;
		c)	PAGE_COUNT=${OPTARG}
			;;
		d)	if echo ${OPTARG} | egrep "[^0-9]" >/dev/null; then
				debug 1 "-d requires a numerical debug level not <${OPTARG}>"
				exit 1
			fi
			DEBUG=${OPTARG}
			;;
		k)	KEEP_PS_FILE="1"
			;;
		p)	DOCUMENT_DEST_DIR=${OPTARG}
			;;
		h)	usage
			;;
		s)	SPOOL_FILE_NAME=${OPTARG}
			;;
		u)	USER_NAME=${OPTARG}
			;;
		z)	SPOOL_FILE_SIZE=${OPTARG}
			;;
	esac
done
debug 10 "${LINENO} s:<${SPOOL_FILE_NAME}> J:<${JOB_NAME}> c:<${PAGE_COUNT}> z:<${SPOOL_FILE_SIZE}> u:<${USER_NAME}>"

# if called without an argument show the help and exit
if [ -z ${SPOOL_FILE_NAME} ]; then
	debug 1 "No spool file provided with -s spool_file_name; use -h for help"
	exit 1
fi

# set DEST_SUB_DIR to default if no DOCUMENT_DEST_DIR was set by -p
if [ -z ${DOCUMENT_DEST_DIR} ] && [ -z ${DEST_SUB_DIR} ]; then
	DEST_SUB_DIR="PDF"
fi

if [ ${DOCUMENT_DEST_DIR} ]; then
	DOCUMENT_DEST_DIR=$( eval echo ${DOCUMENT_DEST_DIR})
	test ${DEST_SUB_DIR} && DOCUMENT_DEST_DIR=${DOCUMENT_DEST_DIR}/${DEST_SUB_DIR}
	debug 5 "${LINENO} u:<${USER_NAME}> s:<${SPOOL_FILE_NAME}> documentdestdir:<${DOCUMENT_DEST_DIR}>"
else
	USER_HOME_DIR=$( eval echo ~${USER_NAME})
	test ${USER_HOME_DIR} || USER_HOME_DIR="/var/tmp"
	DOCUMENT_DEST_DIR=${USER_HOME_DIR}/${DEST_SUB_DIR}
	debug 5 "${LINENO} u:<${USER_NAME}> s:<${SPOOL_FILE_NAME}> homedir:<${USER_HOME_DIR}> dessubdir:<${DEST_SUB_DIR}>"
fi


if [ ${JOB_NAME} ]; then
	echo ${JOB_NAME} | egrep -i "^[A-Za-z0-9_]*://" && \
	DOCUMENT_NAME=${JOB_NAME} || \
	DOCUMENT_NAME=${JOB_NAME%.*}
	debug 7 "${LINENO} j:<${JOB_NAME}> documentname:<${DOCUMENT_NAME}>"
else
	DOCUMENT_NAME=$( egrep -i "^%%Title: " ${SPOOL_FILE_NAME} | \
		head -n 1 | \
		sed -e 's/^%%Title: //g' \
			-e 's/^(//g' \
			-e 's/\(.*\)\..*/\1/g' | \
		tr -d "\r\n")
	DOCUMENT_NAME=$( echo -e -n ${DOCUMENT_NAME})
	debug 7 "${LINENO} documentname:<${DOCUMENT_NAME}>"
fi

# remove leading URL:// and replace laft '/' with '_'
DOCUMENT_NAME=$( echo ${DOCUMENT_NAME} | \
	sed -e "s/^[A-Za-z0-9_]*:\/\///g" -e "s/\/$//g" -e "s/\//_/g")
debug 7 "${LINENO} documentname:<${DOCUMENT_NAME}>"

rc=0
# create destination directory if it not exists
if [ ! -d "${DOCUMENT_DEST_DIR}" ]; then
	mkdir -p "${DOCUMENT_DEST_DIR}" || rc=1
	chown ${USER_NAME}: "${DOCUMENT_DEST_DIR}" || rc=1
	chmod  --reference=${SPOOL_FILE_NAME} "${DOCUMENT_DEST_DIR}" || rc=1
	chmod a+x "${DOCUMENT_DEST_DIR}" || rc=1
	test ${rc} -gt 0 && debug 2 "${LINENO} can't create destdirectory:<${DOCUMENT_DEST_DIR}>"
fi

test  ${KEEP_PS_FILE} && backup ps
# create new file with permissions of the spool file
touch "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"
chmod --reference=${SPOOL_FILE_NAME} "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"
# extract PostScript part from the spool file
awk '/^%!PS-Adobe/,/^%%EOF/' ${SPOOL_FILE_NAME} >"${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"

backup pdf
# create new file with permissions of the spool file
touch "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf"
chmod --reference=${SPOOL_FILE_NAME} "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf"
# create PDF file from PostScript
gs -q -dPARANOIDSAFER -dCompatibilityLevel=1.3 -dNOPAUSE -dBATCH \
	-sDEVICE=pdfwrite -sOutputFile="${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf" \
	-c save pop -f "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"

# handle ps file
if [ ${KEEP_PS_FILE} ]; then
	debug 0 "${LINENO} keep intermediate PostScript file"
else
	rm -f "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"
fi

# handle smbprn spool file
if [ ${DEBUG} -gt 2 ]; then
	debug 3 "${LINENO} s:<${SPOOL_FILE_NAME}> not removed"
else
	rm -f ${SPOOL_FILE_NAME}
fi

ACC SHELL 2018