3614157 a year ago
1 contributor
76 lines | 2.42kb
#!/bin/bash

couleur_default="Niveaux de gris"
quantite_default=10
warning_qty=250

declare -A _type=([Couleur]=CMYK [$couleur_default]=Gray)
declare -A _ecoprint=([Non]=Off [Oui]=On)
declare -A _format=([A4]=PF500B [A3]=PF500A)

get_size () {
	eval $(pdfinfo "$f" | awk '/^Page size:/{if ($6 == "pts") printf("mm_x=%d;mm_y=%d", $3*0.352778, $5*0.352778)}')
}
set -x
for f in "$@"; do
	file_type=$(file -bn "$f")
	if [[ $file_type =~ ^(Microsoft Word|OpenDocument Text) ]]; then
            libreoffice --headless --convert-to pdf "/dev/shm/${f##*/}.pdf" "$f"
	    f="/dev/shm/${f##*/}.pdf"
	fi
	file "$f" | grep -q ': PDF document, version' || continue
	sides=$(pdfinfo "$f" | awk '/^Page size:/{print ($3 > $5) ? "short" : "long"}')
	get_size
	while IFS='|' read type format ecoprint qty massicote <<< "$(yad --title "${f##*/}" --form \
		                                        --field='Type:CB' 'Couleur!^Niveaux de gris' \
		                                        --field='Format:CB' 'A3!^A4' \
							--field='EcoPrint:CB' 'Non!^Oui' \
							--field='Quantité:NUM' '1!1..500!1' \
							--field='Massicote-moi ça:CHK' 'FALSE' \
							--field="\n\nStatut imprimante:\n$(/usr/local/bin/printer status):LBL")"; do
		test -z "${qty}${type}" && exit
		if test ${qty:-10} -gt $warning_qty; then
			zenity --warning --text="Attention plus de $warning_qty impressions"
			continue
		fi
		if [[ ! "${f##*/}" =~ ^massicotable- && "$massicote" =~ ^TRUE ]]; then
			/usr/local/bin/massicot "$f"
			f="${f%/*}/massicotable-${f##*/}"
			sides="short"
			get_size
		fi
		if test $format = A3; then
			gs -o "/dev/shm/A3-${f##*/}" \
				-sDEVICE=pdfwrite \
				-sPAPERSIZE=a3 \
				-dFIXEDMEDIA \
				-dPDFFitPage \
				-dCompatibilityLevel=1.5 \
				"$f"
			f="/dev/shm/A3-${f##*/}"
		elif ! test "${mm_x}x${mm_y}" = "210x297" -o "${mm_x}x${mm_y}" = "297x210"; then
			gs -o "/dev/shm/A4-${f##*/}" \
				-sDEVICE=pdfwrite \
				-sPAPERSIZE=a4 \
				-dFIXEDMEDIA \
				-dPDFFitPage \
				-dCompatibilityLevel=1.5 \
				"$f"
			f="/dev/shm/A4-${f##*/}"
		fi
		lp -o InputSlot=${_format[$format]} \
		   -o sides=two-sided-$sides-edge \
		   -o ColorModel=${_type[${type}]} \
		   -o KCEcoprint=${_ecoprint[${ecoprint}]} \
		   -o KCCollate1=On \
		   -n ${qty} \
		   "$f"
		if test $format = A3; then
			rm -f "$f"
		fi
		if [[ $file_type =~ ^(Microsoft Word|OpenDocument Text) ]]; then
			rm -f "$f"
		fi
		break
	done
done