]> gitweb.hhaalo.de Git - scantopdf.git/commitdiff
add errlog to scripts
authorBastian Dehn <hhaalo@arcor.de>
Sat, 29 Mar 2025 10:07:22 +0000 (11:07 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 29 Mar 2025 10:48:22 +0000 (11:48 +0100)
build-package
errlog.sh [new file with mode: 0644]
scanbasic.sh
scantopdf
scantopdfbw
scantopdfgray

index 86deccde38b43b02384cea4f2b1dd7c6a09a2f8b..ee2eca66a1452d0db9ecc3cc421538e3cec47ea5 100755 (executable)
@@ -13,14 +13,20 @@ main()
        cp scantopdf build/$PACKAGENAME/usr/local/bin/scantopdf
        sed -i '/@scanbasic.sh@/r scanbasic.sh' build/$PACKAGENAME/usr/local/bin/scantopdf
        sed -i '/@scanbasic.sh@/d' build/$PACKAGENAME/usr/local/bin/scantopdf
+       sed -i '/@errlog.sh@/r errlog.sh' build/$PACKAGENAME/usr/local/bin/scantopdf
+       sed -i '/@errlog.sh@/d' build/$PACKAGENAME/usr/local/bin/scantopdf
 
        cp scantopdfgray build/$PACKAGENAME/usr/local/bin/scantopdfgray
        sed -i '/@scanbasic.sh@/r scanbasic.sh' build/$PACKAGENAME/usr/local/bin/scantopdfgray
        sed -i '/@scanbasic.sh@/d' build/$PACKAGENAME/usr/local/bin/scantopdfgray
-       cp scantopdfbw build/$PACKAGENAME/usr/local/bin/scantopdfbw
+       sed -i '/@errlog.sh@/r errlog.sh' build/$PACKAGENAME/usr/local/bin/scantopdfgray
+       sed -i '/@errlog.sh@/d' build/$PACKAGENAME/usr/local/bin/scantopdfgray
 
+       cp scantopdfbw build/$PACKAGENAME/usr/local/bin/scantopdfbw
        sed -i '/@scanbasic.sh@/r scanbasic.sh' build/$PACKAGENAME/usr/local/bin/scantopdfbw
        sed -i '/@scanbasic.sh@/d' build/$PACKAGENAME/usr/local/bin/scantopdfbw
+       sed -i '/@errlog.sh@/r errlog.sh' build/$PACKAGENAME/usr/local/bin/scantopdfbw
+       sed -i '/@errlog.sh@/d' build/$PACKAGENAME/usr/local/bin/scantopdfbw
        cp generate-trennblatt build/$PACKAGENAME/usr/local/bin/generate-trennblatt
 
        mkdir -p build/$PACKAGENAME/DEBIAN
diff --git a/errlog.sh b/errlog.sh
new file mode 100644 (file)
index 0000000..47986f2
--- /dev/null
+++ b/errlog.sh
@@ -0,0 +1,7 @@
+errlog()
+{
+       local msg="$1"
+
+       echo "ERROR: $msg"
+       return 1
+}
index 08d5c22e4fb06ce89549a96b47e14fe0c8467aaa..6cfbe1ceff76dfefc8b142f4888bdabb24d0af1d 100644 (file)
@@ -3,8 +3,12 @@ optimize()
        local input=$1
        local output=$input-out
 
-       qpdf --linearize $input $output
-       mv $output $input
+       qpdf --linearize $input $output \
+               || errlog "could not linearize $input" \
+               || return 1
+       mv $output $input \
+               || errlog "cloud not rename $ouptut to $input" \
+               || return 1
 }
 
 addocr()
@@ -14,15 +18,19 @@ addocr()
        local error=""
 
        echo $input
-       error=$(ocrmypdf -l deu $input $output 2>&1)
+       error=$(ocrmypdf -l deu $input $output 2>&1) \
+               || errlog "could not add ocr on $input" \
+               || return 1
 
        if [ -n "$(echo $error | grep offset)" ]; then
                printf "%s: %s\n" $input $error >> $HOME/addocr-error.log
        fi
 
        if [ -f $output ]; then
-               mv $output $input
-               optimize $input
+               mv $output $input \
+                       || errlog "could not rename $output to $input" \
+                       || return 1
+               optimize $input || return 1
        fi
 }
 
@@ -31,13 +39,19 @@ createonepdf()
        local output=$1
 
        for i in scanned*.tiff; do
-               tiff2pdf $i -o ${i/.tiff/.pdf}
-               rm $i
+               tiff2pdf $i -o ${i/.tiff/.pdf} \
+                       || errlog "could not convert tiff to pdf" \
+                       || return 1
+               rm $i || errlog "could not delete $i" || return 1
        done
 
-       pdftk scanned*.pdf output $output
-       rm scanned*.pdf
-       addocr $output
+       pdftk scanned*.pdf output $output \
+               || errlog "could not convert into single $output" \
+               || return 1
+       rm scanned*.pdf \
+               || errlog "could not remove scanned pdfs" \
+               || return 1
+       addocr $output || return 1
 }
 
 createpdf()
@@ -57,13 +71,15 @@ createpdf()
                pdfs=$(echo $pdfs "scanned$countno.pdf")
        done
 
-       pdftk $pdfs output $basename$(printf "%03d" $start).pdf
+       pdftk $pdfs output $basename$(printf "%03d" $start).pdf \
+               || errlog "could not create pdf" \
+               || return 1
 
        for i in $pdfs; do
-               rm $i
+               rm $i || errlog "could not delete $i" || return 1
        done
 
-       addocr $basename$(printf "%03d" $start).pdf
+       addocr $basename$(printf "%03d" $start).pdf || return 1
 }
 
 detectsplit()
@@ -71,7 +87,9 @@ detectsplit()
        local pdf=$1
        local trenn=
 
-       trenn=$(zbarimg --raw --quiet $pdf)
+       trenn=$(zbarimg --raw --quiet $pdf) \
+               || errlog "could not read cut page" \
+               || return 1
        if [ "$trenn" == "Trennblatt" ]; then
                echo "true"
                return
@@ -90,12 +108,15 @@ createmultipdfs()
 
        for i in scanned*.tiff; do
                pdf=${i/.tiff/.pdf}
-               tiff2pdf $i -o $pdf
+               tiff2pdf $i -o $pdf \
+                       || errlog "could not convert $i to $pdf" \
+                       || return 1
                trenn=$(detectsplit $pdf)
                rm $i
                if [ "$trenn" == "true" ]; then
                        rm $pdf
-                       createpdf $startcount $((endcount - 1)) $basename
+                       createpdf $startcount $((endcount - 1)) $basename \
+                               || return 1
                        startcount=$((endcount + 1))
                fi
                ((endcount++))
index 768800fe1d018f5bf5823c2d7f5f6f180df2439f..1e9e0eecc2cf6f4ad19d40d4880372c2016b3e1f 100755 (executable)
--- a/scantopdf
+++ b/scantopdf
@@ -1,23 +1,25 @@
 #!/bin/bash
 
+@errlog.sh@
+
 @scanbasic.sh@
 
 main()
 {
-       if [ -z "$1" ]; then
-               echo "ERROR: no page count"
-               exit 1
-       fi
-
-       if [ -z "$2" ]; then
-               echo "ERROR: no pdf file name"
-               exit 1
-       fi
-
        local pagecount=$1
        local output=$2
        local basename=$3
 
+       if [ -z "$pagecount" ]; then
+               errlog "no page count"
+               return 1
+       fi
+
+       if [ -z "$output" ]; then
+               errlog "no pdf file name"
+               return 1
+       fi
+
        scanimage --source=DocumentFeeder \
                --mode=Color \
                --resolution=300 \
@@ -26,19 +28,16 @@ main()
                --format=tiff \
                --batch-count=$pagecount \
                --batch-increment=1 \
-               --batch=scanned%03d.tiff
-
-       if [ $? -gt 0 ]; then
-               echo "ERROR: scan error"
-               exit 1
-       fi
+               --batch=scanned%03d.tiff \
+               || errlog "scan error" \
+               || return 1
 
        if [ "$output" == "multi" ]; then
-               createmultipdfs $pagecount $basename
+               createmultipdfs $pagecount $basename || return 1
                exit 0
        fi
 
-       createonepdf $output
+       createonepdf $output || return 1
 }
 
 main $*
index c25dfedad6b8f62b7241d38fe8e29f4548b52195..570508e5d095d6590c78efadd0851ed79ca0219a 100755 (executable)
@@ -1,23 +1,25 @@
 #!/bin/bash
 
+@errlog.sh@
+
 @scanbasic.sh@
 
 main()
 {
-       if [ -z "$1" ]; then
-               echo "ERROR: no page count"
-               exit 1
-       fi
-
-       if [ -z "$2" ]; then
-               echo "ERROR: no pdf file name"
-               exit 1
-       fi
-
        local pagecount=$1
        local output=$2
        local basename=$3
 
+       if [ -z "$pagecount" ]; then
+               errlog "no page count"
+               return 1
+       fi
+
+       if [ -z "$output" ]; then
+               errlog "no pdf file name"
+               return 1
+       fi
+
        scanimage --source=DocumentFeeder \
                --mode=BW \
                --resolution=300 \
@@ -26,19 +28,16 @@ main()
                --format=tiff \
                --batch-count=$pagecount \
                --batch-increment=1 \
-               --batch=scanned%03d.tiff
-
-       if [ $? -gt 0 ]; then
-               echo "ERROR: scan error"
-               exit 1
-       fi
+               --batch=scanned%03d.tiff \
+               || errlog "scan error" \
+               || return 1
 
        if [ "$output" == "multi" ]; then
-               createmultipdfs $pagecount $basename
-               exit 0
+               createmultipdfs $pagecount $basename || return 1
+               return
        fi
 
-       createonepdf $output
+       createonepdf $output || return 1
 }
 
 main $*
index 224fb5e19e8ea289b50a77863d865bcf334eeeab..5b6a6adeaa3b526c74d973fda61798a99ad8ea7e 100755 (executable)
@@ -1,23 +1,25 @@
 #!/bin/bash
 
+@errlog.sh@
+
 @scanbasic.sh@
 
 main()
 {
-       if [ -z "$1" ]; then
-               echo "ERROR: no page count"
-               exit 1
-       fi
-
-       if [ -z "$2" ]; then
-               echo "ERROR: no pdf file name"
-               exit 1
-       fi
-
        local pagecount=$1
        local output=$2
        local basename=$3
 
+       if [ -z "$pagecount" ]; then
+               errlog "no page count"
+               return 1
+       fi
+
+       if [ -z "$output" ]; then
+               errlog "no pdf file name"
+               return 1
+       fi
+
        scanimage --source=DocumentFeeder \
                --mode=Gray \
                --resolution=300 \
@@ -26,19 +28,16 @@ main()
                --format=tiff \
                --batch-count=$pagecount \
                --batch-increment=1 \
-               --batch=scanned%03d.tiff
-
-       if [ $? -gt 0 ]; then
-               echo "ERROR: scan error"
-               exit 1
-       fi
+               --batch=scanned%03d.tiff \
+               || errlog "scan error" \
+               || return 1
 
        if [ "$output" == "multi" ]; then
-               createmultipdfs $pagecount $basename
-               exit 0
+               createmultipdfs $pagecount $basename || return 1
+               return
        fi
 
-       createonepdf $output
+       createonepdf $output || return 1
 }
 
 main $*