From 57dd43cc46107fecaef918a0f4a7d9c2b42e4d37 Mon Sep 17 00:00:00 2001 From: Francesco Minnocci Date: Tue, 21 Jun 2022 12:09:37 +0200 Subject: [PATCH] impagina: Added documentation and a --help argument --- impagina | 82 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/impagina b/impagina index 2ff5337..6e4d219 100755 --- a/impagina +++ b/impagina @@ -1,27 +1,59 @@ #!/bin/sh -# Initialize variables -input="$1" -inputMul4="/tmp/${input%.pdf}-Mul4.pdf" -outputImpaginato="${input%.pdf}-Impaginato.pdf" -empty="/tmp/empty.pdf" -n="$(pdftk "$input" dump_data | grep NumberOfPages | cut -d' ' -f2)" - -# Add empty pages -echo .bp | groff -T pdf > "$empty" - -pdftk F="$input" E="$empty" cat F1-$n $(for x in $(seq 1 $((4-n%4))); do printf 'E1 '; done) output "$inputMul4" - -# Generate alternating page ranges -n=$((n+(4-n%4))) -range= -k=1 -while [ "$k" -lt $((n/2)) ]; do - range="$range$k $((n/2+k)) $((n/2+k+1)) $((k+1)) " - k=$((k+2)) -done - -# Output final pdf -pdftk "$inputMul4" cat $range output "$outputImpaginato" - -rm "$empty" "$inputMul4" +usage() { + _name="$(basename "$0")" + cat << EOF +Utilizzo: ${_name} [nomePDF] + +Esempi: + + ${_name} MeccanicaRazionale.pdf + +Per bug contattare f.minnocci@studenti.unipi.it +I ringraziamenti per l'idea originale vanno a http://poisson.phc.dm.unipi.it/~dilillo +EOF +exit $1 +} + +main() { + # Initialize variables + input="$(realpath "$1")" + basename="$(basename "$1")" + inputMul4="/tmp/${basename%.pdf}-Mul4.pdf" + outputImpaginato="${basename%.pdf}-Impaginato.pdf" + empty="/tmp/empty.pdf" + echo "$input $basename $inputMul4 $outputImpaginato" > ~/log + n="$(pdftk "$input" dump_data | grep NumberOfPages | cut -d' ' -f2)" + + # Add empty pages + echo .bp | groff -T pdf > "$empty" + + pdftk F="$input" E="$empty" cat F1-$n $(for x in $(seq 1 $((4-n%4))); do printf 'E1 '; done) output "$inputMul4" + + # Generate alternating page ranges + n=$((n+(4-n%4))) + range= + k=1 + while [ "$k" -lt $((n/2)) ]; do + range="$range$k $((n/2+k)) $((n/2+k+1)) $((k+1)) " + k=$((k+2)) + done + + # Output final pdf + pdftk "$inputMul4" cat $range output "$outputImpaginato" + + rm "$empty" "$inputMul4" +} + +if [ -z "$1" ] ; then + printf "Non hai specificato nessun file...\n\n" && usage 1 +else + case "$1" in + -h|--help) + usage 0 ;; + -*) + error "Opzione invalida: $1" ;; + *) + main "$@" ;; + esac +fi