#!/bin/sh # copyright 2002 Julien Tayon . # une intuition: il va falloir tout réécrire (perl probablement). ######### TODO # $mailer = guess mutt / sendmail # write all in perl. # find a way to get a file name from lpd # standalone application ? ######## this definition here implies it is the default debug=1 mail=true lazy=1 ########## The power of shell scripting #echo $* > /tmp/args ########## args # fonctionnement simple en ligne de commande [[ "$1" == "-j" ]] && standalone="1" #end [ ! -z "$debug" ] && echo $LINENO CONTROL=$CONTROL > /tmp/debug # case it is called by lprng then the env var CONTROL is set if [ ! -z "$CONTROL" ]; then # I fear I should not have parsed the options in Control with their positions #the field beginning with P is the user name user=$( echo $CONTROL | tr " " "\n" | grep "^P" | cut -c2- ); #the field beginning with a N is the filename fn=$( echo $CONTROL | tr " " "\n" | grep "^N" | cut -c2- ); fn=$(basename $fn ); fi [ ! -z "$debug" ] && echo $CONTROL user=$user>> /tmp/debug [ ! -z "$debug" ] && logger -p daemon.notice -t alambic -- "$CONTROL" [ ! -z "$debug" ] && echo LINENO infile=$infile>> /tmp/debug i=$# while [ "$i" != 0 ] ; do #Je suis d'accord je pourrais utiliser getopt # mais comme je trouve que c'est élégant je garde [[ $standalone == "1" ]] && [[ "$1" == "-out" ]] && outfile=$2 [[ $standalone == "1" ]] && [[ "$1" == "-in" ]] && infile=$2 [[ "$1" == "-n" ]] && user=$2 ; [[ "$1" == "-h" ]] && host=$2 ; [[ "$1" == "--" ]] && shift && break ; i=$(( $i - 1 )); shift; done [ ! -z "$outfile" ] && mail=false [ ! -z "$debug" ] && echo infile=$infile user=$user>> /tmp/debug [[ $( basename $0 -mail ) == "alambic" ]] && format="pdf" [[ $( basename $0 -mail ) == "epsilon" ]] && format="eps" [[ $( basename $0 -mail ) == "psionic" ]] && format="ps" [ ! -z "$infile" ] && [ -z "$outfile" ] && outfile="$infile.${format}" if [ ! -z "$lazy" ]; then echo $0 | grep mail &> /dev/null && mail=true [ ! -z "$debug" ] && echo $LINENO mail=$mail user=$user >> /tmp/debug fi [ ! -z "$debug" ] && echo $LINENO $USER >> /tmp/debug ########## end args ########## user definition ### futur conf file template="sortie" outvardir="/tmp" subject="impression" message="Résultat de l'impression demandée sur leviathan" maildomain="julbox.net" ########### end definiftion ########### default value [ -z "$dateargs" ] && dateargs=+'%d-%m_%Hh%M' set ${mail:="false"} set ${maildomain:=""} set ${template:="out-"} set ${outvardir:="/var/spool/alambic"} set ${subject:="printing your file"} set ${message:="just printed"} set ${format:="pdf"} set ${messTrait:="Error processing input data (with gs or magicfilter)"} set ${user:=jul} ########### end default value ######## prolog outdir=${outvardir}/${format}/ [ ! -z "$debug" ] && echo $LINENO fn=$fn >> /tmp/debug if [ ! -z "$CONTROL" ]; then echo $fn | grep stdin &> /dev/null && simple=1 [ ! -z "$simple" ] || template=$fn fi [ ! -z "$debug" ] && echo $LINENO template=$template >> /tmp/debug i=$( (ls $outdir$template*$format 2> /dev/null | wc -l) || echo "" ) i=$[ $i + 1 ] j=$( printf "%06d" $i) ; [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug d=$(date $dateargs ) j=$j-$d subject="$subject $d" [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug [ -z "$simple" ] && j="-${i}" outf="${outdir}${template}$j.${format}" [ ! -z "$debug" ] && echo $LINENO outf="$outf" >> /tmp/debug title="$user@$host job $j " [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug [[ $standalone == "1" ]] && outf="$outfile" ########## end prolog [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug ########### begin tough part :Þ [ ! -e $outdir ] && (mkdir $outdir ; chown lp.lp $outdir ; chmod 775 $outdir ) [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug # prévoir l'éxistence d'un cas pour imprimer vers une imprimante réelle. if [ -z "$infile" ]; then /etc/magicfilter/psonly600-filter | gs -sDEVICE=${format}write -dSAFER -dNOPAUSE -dBATCH -sOutputFile="$outf" - || erreurTraitement=1; else cat $infile | /etc/magicfilter/psonly600-filter | gs -sDEVICE=${format}write -dSAFER -dNOPAUSE -dBATCH -sOutputFile="$outf" - || erreurTraitement=1 fi [[ "$mail" == "true" ]] && [ ! -z "$user" ] && if [[ "$user" != "root" ]]; then [ -z maildomain ] && dest=$user || dest=${user}@${maildomain} [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug if [ -z "$erreurTraitement" ] ; then logger -p daemon.notice -t alambic -- "file $outf sent to $dest" [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug echo "$message" | mutt -a "$outf" -x -n -s "$subject" $dest && rm $outf else logger -p daemon.err -t alambic -- $messTrait [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug echo "$messTrait" | mutt -x -n -s "$subject" $dest && rm $outf fi fi ########### end touch part [ ! -z "$debug" ] && echo $LINENO >> /tmp/debug [ ! -z "$debug" ] && chmod 644 /tmp/debug exit 0