From: Bastian Dehn Date: Sat, 5 Jul 2025 17:36:55 +0000 (+0200) Subject: change project structur X-Git-Tag: v1.1.0~1^2~8 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=ceae57fc8dd350cc0de1510cdc42341139c7cc96;p=scantopdf.git change project structur --- diff --git a/build-package b/build-package index 964ea40..86c602a 100755 --- a/build-package +++ b/build-package @@ -1,42 +1,64 @@ #!/bin/bash -main() +errlog() +{ + local lastexit=$? + local msg="$1" + + echo "❌ ERROR: $msg" + echo "🛑 EXIT $lastexit" + return $lastexit +} + +build_package() { local VERSION="1.0.7" local BUILDVERSION="1" local ARCHITECTURE="amd64" local PACKAGENAME="scantopdf-$VERSION-$BUILDVERSION-$ARCHITECTURE" + local EXECUTABLES="scantopdf \ + scantopdfgray \ + scantopdfbw \ + generate-trennblatt" + + echo "ℹ️ INFO: build package $PACKAGENAME" - echo "⭐ START: build-package" mkdir -p build mkdir -p build/$PACKAGENAME/usr/local/bin - 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 - 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 + local executable= + for executable in $EXECUTABLES; do + cp src/$executable build/$PACKAGENAME/usr/local/bin/$executable + sed -i "/@scanbasic.sh@/r src/scanbasic.sh" \ + build/$PACKAGENAME/usr/local/bin/$executable + sed -i "/@scanbasic.sh@/d" \ + build/$PACKAGENAME/usr/local/bin/$executable + done mkdir -p build/$PACKAGENAME/DEBIAN - cp control build/$PACKAGENAME/DEBIAN/control + cp src/control build/$PACKAGENAME/DEBIAN/control sed -i "s/\$VERSION/$VERSION/" build/$PACKAGENAME/DEBIAN/control cd build - dpkg --build $PACKAGENAME +} + +run_tests() +{ + echo "ℹ️ INFO: run tests" + ./tests/scanbasic.bats +} + +main() +{ + echo "⭐ START: build-package" + + [ -n "$SKIP_TESTS" ] \ + || run_tests \ + || errlog "tests failed" \ + || return $? + build_package + echo "✅ SUCCESS: build-package" } diff --git a/control b/control deleted file mode 100644 index 803d8e5..0000000 --- a/control +++ /dev/null @@ -1,6 +0,0 @@ -Package: scantopdf -Version: $VERSION -Architecture: amd64 -Maintainer: Bastian Dehn -Depends: sane (>= 1.0.14), qpdf (>= 11.3.0), pdftk (>= 2.02), libtiff-tools (>= 4.5.0), ocrmypdf (>= 14.0.1), tesseract-ocr-deu (>= 4.1.0), zbar-tools (>= 0.23.92), qrencode (>= 4.1.), librsvg2-bin (>= 2.54.7) -Description: scan to pdf with ocr scripts for sane-kds-s2000w-net diff --git a/generate-trennblatt b/generate-trennblatt deleted file mode 100755 index 558c56e..0000000 --- a/generate-trennblatt +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -errlog() -{ - local lastexit=$? - local msg="$1" - - echo "❌ ERROR $msg" - echo "🛑 EXIT $lastexit" - return $lastexit -} - -main() -{ - echo "⭐ START: generate-trennblatt" - - command -v qrencode > /dev/null \ - || errlog "command qrencode not found" \ - || return $? - command -v rsvg-convert > /dev/null \ - || errlog "command rsvg-convert not found" \ - || return $? - - echo "Trennblatt" \ - | qrencode --level=H \ - --type=SVG \ - --output=- \ - | rsvg-convert --format=pdf \ - --output=trennblatt-qr.pdf - - echo "✅ SUCCESS: generate-trennblatt" -} - -main diff --git a/scanbasic.bats b/scanbasic.bats deleted file mode 100755 index 6ffb3f0..0000000 --- a/scanbasic.bats +++ /dev/null @@ -1,317 +0,0 @@ -#!/usr/bin/bats - -tiff2pdf() { - echo "$tiff2pdf_return" - return "$tiff2pdf_exit" -} - -ocrmypdf() { - echo "$ocrmypdf_return" - return "$ocrmypdf_exit" -} - -qpdf() { - echo "$qpdf_return" - return "$qpdf_exit" -} - -mv() { - echo "$mv_return" - return "$mv_exit" -} - -rm() { - echo "$rm_return" - return "$rm_exit" -} - -pdftk() { - echo "$pdftk_return" - return "$pdftk_exit" -} - -zbarimg() { - echo "$zbarimg_return" - return "$zbarimg_exit" -} - -scanimage() { - echo "$scanimage_return" - return "$scanimage_exit" -} - -setup() { - load "/usr/lib/bats/bats-assert/load" - load "/usr/lib/bats/bats-support/load" - - source ./scanbasic.sh - - export -f qpdf - qpdf_return= - qpdf_exit=0 - - export -f mv - mv_return= - mv_exit=0 - - export -f ocrmypdf - ocrmypdf_return= - ocrmypdf_exit=0 - - export -f tiff2pdf - tiff2pdf_return= - tiff2pdf_exit=0 - - export -f rm - rm_return= - rm_exit=0 - - export -f pdftk - pdftk_return= - pdftk_exit=0 - - export -f zbarimg - zbarimg_return= - zbarimg_exit=0 - - export -f scanimage - scanimage_return= - scanimage_exit=0 -} - -teardown() { - export -n qpdf - export -n mv - export -n ocrmypdf - export -n tiff2pdf - export -n rm - export -n pdftk - export -n zbarimg - export -n scanimage -} - -@test "should failure optimize input parameter is missing" { - run optimize - - assert_failure - assert_line --index 0 "❌ ERROR: missing input" -} - -@test "should failure optimize when qpdf failes" { - qpdf_exit=1 - - run optimize "testfile" - - assert_failure - assert_line --index 0 "❌ ERROR: could not linearize testfile" -} - -@test "should failure optimize when mv fails" { - mv_exit=1 - - run optimize "testfile" - - assert_failure - assert_line --index 0 \ - "❌ ERROR: could not rename testfile-out to testfile" -} - -@test "should success optimize" { - run optimize "testfile" - - assert_success -} - -@test "should failure addocr missing input parameter" { - run addocr - - assert_failure - assert_line --index 0 "❌ ERROR: missing input" -} - -@test "should failure addocr ocrmypdf fails" { - ocrmypdf_exit=1 - - run addocr "testfile" - - assert_failure - assert_line --index 0 "ℹ️ INFO: adding ocr on file testfile..." - assert_line --index 1 "❌ ERROR: could not add ocr on testfile" -} - -@test "should success addocr" { - run addocr "testfile" - - assert_success - assert_line --index 0 "ℹ️ INFO: adding ocr on file testfile..." -} - -@test "should failure createonepdf missing output parameter" { - run createonepdf - - assert_failure - assert_line --index 0 "❌ ERROR: missing output" -} - -@test "should failure createonepdf tiff2pdf fails" { - tiff2pdf_exit=1 - - run createonepdf "testfile" - - assert_failure - assert_line --index 0 "❌ ERROR: could not convert tiff to pdf" -} - -@test "should failure createonepdf delete testfile fails" { - rm_exit=1 - - run createonepdf "testfile" - - assert_failure - assert_line --index 0 "❌ ERROR: could not delete scanned*.tiff" -} - -@test "should failure createonepdf pdftk fails" { - pdftk_exit=1 - - run createonepdf "testfile" - - assert_failure - assert_line --index 0 "❌ ERROR: could not convert into single testfile" -} - -@test "should success createonepdf" { - run createonepdf "testfile" - - assert_success -} - -@test "should failure createpdf missing start parameter" { - run createpdf - - assert_failure - assert_line --index 0 "❌ ERROR: missing start" -} - -@test "should failure createpdf missing end parameter" { - run createpdf "1" - - assert_failure - assert_line --index 0 "❌ ERROR: missing end" -} - -@test "should failure createpdf pdftk fails" { - pdftk_exit=1 - - run createpdf "1" "2" - - assert_failure - assert_line --index 0 "❌ ERROR: could not create pdf" -} - -@test "should failure pdf delete fails" { - rm_exit=1 - - run createpdf "1" "2" - - assert_failure - assert_line --index 0 "❌ ERROR: could not delete scanned001.pdf" -} - -@test "should success createpdf" { - run createpdf "1" "2" - - assert_success -} - -@test "should failure detectsplit missing parameter" { - run detectsplit - - assert_failure - assert_line --index 0 "❌ ERROR: missing pdf" -} - -@test "should failure detectsplit zbarimg fails" { - zbarimg_exit=1 - - run detectsplit "testfile.pdf" - - assert_success - assert_line --index 0 "false" -} - -@test "should success and false detectsplit zbarimg nothing find" { - run detectsplit "testfile.pdf" - - assert_success - assert_line --index 0 "false" -} - -@test "should success and true detectsplit zbarimg find trennblatt" { - zbarimg_return="Trennblatt" - - run detectsplit "testfile.pdf" - - assert_success - assert_line --index 0 "true" -} - -@test "should failure createmultipdfs tiff2pdf fails" { - tiff2pdf_exit=1 - - run createmultipdfs - - assert_failure - assert_line --index 0 "❌ ERROR: could not convert scanned*.tiff to scanned*.pdf" -} - -@test "should failure createmultipdfs rm tiff fails" { - rm_exit=1 - - run createmultipdfs - - assert_failure - assert_line --index 0 "❌ ERROR: could not delete scanned*.tiff" -} - -@test "should success createmutlipdfs" { - run createmultipdfs - - assert_success -} - -@test "should failure scantopdf missing mode parameter" { - run scantopdf - - assert_failure - assert_line --index 0 "❌ ERROR: missing mode" -} - -@test "should failure scantopdf missing pagecount parameter" { - run scantopdf "Color" - - assert_failure - assert_line --index 0 "❌ ERROR: missing pagecount" -} - -@test "should failure scantopdf missing output parameter" { - run scantopdf "Color" "5" - - assert_failure - assert_line --index 0 "❌ ERROR: missing output" -} - -@test "should failure scantopdf scanimage fails" { - scanimage_exit=1 - - run scantopdf "Color" "1" "test.pdf" - - assert_failure - assert_line --index 0 "❌ ERROR: scan error" -} - -@test "should success scantopdf" { - run scantopdf "Color" "1" "test.pdf" - - assert_success -} diff --git a/scanbasic.sh b/scanbasic.sh deleted file mode 100644 index 06f5eb5..0000000 --- a/scanbasic.sh +++ /dev/null @@ -1,232 +0,0 @@ -errlog() -{ - local lastexit=$? - local msg="$1" - - echo "❌ ERROR: $msg" - echo "🛑 EXIT $lastexit" - return $lastexit -} - -optimize() -{ - local input="$1" - local output="$input-out" - - command -v qpdf > /dev/null \ - || errlog "command qpdf not found" \ - || return $? - [ -n "$input" ] \ - || errlog "missing input" \ - || return $? - qpdf --linearize $input $output \ - || errlog "could not linearize $input" \ - || return $? - mv $output $input \ - || errlog "could not rename $output to $input" \ - || return $? -} - -addocr() -{ - local input="$1" - local output="$input-out" - - command -v ocrmypdf > /dev/null \ - || errlog "command ocrmypdf not found" \ - || return $? - [ -n "$input" ] \ - || errlog "missing input" \ - || return $? - - echo "ℹ️ INFO: adding ocr on file $input..." - ocrmypdf --quiet --language deu $input $output \ - || errlog "could not add ocr on $input" \ - || return $? - - [ ! -f $output ] && return 0 - - mv $output $input \ - || errlog "could not rename $output to $input" \ - || return $? - optimize $input \ - || return $? -} - -createonepdf() -{ - local output=$1 - - command -v tiff2pdf > /dev/null \ - || errlog "command tiff2pdf not found" \ - || return $? - command -v pdftk > /dev/null \ - || errlog "command pdftk not found" \ - || return $? - [ -n "$output" ] \ - || errlog "missing output" \ - || return $? - - local output_pdf= - local tiff= - for tiff in scanned*.tiff; do - output_pdf=${tiff/.tiff/.pdf} - tiff2pdf $tiff -o $output_pdf \ - || errlog "could not convert tiff to pdf" \ - || return $? - rm $tiff \ - || errlog "could not delete $tiff" \ - || return $? - done - - pdftk scanned*.pdf output $output \ - || errlog "could not convert into single $output" \ - || return $? - - rm scanned*.pdf \ - || errlog "could not remove scanned pdfs" \ - || return $? - - addocr $output \ - || return $? -} - -createpdf() -{ - local start="$1" - local end="$2" - local basename="$3" - local countno= - local pdfs=() - basename=${basename:="out"} - - command -v pdftk > /dev/null \ - || errlog "command pdftk not found" \ - || return $? - [ -n "$start" ] \ - || errlog "missing start" \ - || return $? - [ -n "$end" ] \ - || errlog "missing end" \ - || return $? - - local i= - for i in $(seq $start $end); do - countno=$(printf "%03d" $i) - pdfs=$(echo $pdfs "scanned$countno.pdf") - done - - pdftk $pdfs output "$basename$(printf "%03d" $start).pdf" \ - || errlog "could not create pdf" \ - || return 1 - - for pdf in $pdfs; do - rm "$pdf" \ - || errlog "could not delete $pdf" \ - || return 1 - done - - addocr "$basename$(printf "%03d" $start).pdf" \ - || return $? -} - -detectsplit() -{ - local pdf=$1 - local trenn= - - command -v zbarimg > /dev/null \ - || errlog "command zbarimg not found" \ - || return $? - [ -n "$pdf" ] \ - || errlog "missing pdf" \ - || return $? - - trenn=$(zbarimg --raw --quiet $pdf) - - [ "$trenn" == "Trennblatt" ] \ - && echo "true" \ - && return 0 - - echo "false" -} - -createmultipdfs() -{ - local basename="multi" - local startcount=1 - local endcount=1 - - command -v tiff2pdf > /dev/null \ - || errlog "command tiff2pdf not found" \ - || return $? - [ -n "$basename" ] \ - || errlog "missing basename" \ - || return $? - - local pdf= - local tiff= - for tiff in scanned*.tiff; do - pdf=${tiff/.tiff/.pdf} - tiff2pdf $tiff -o $pdf \ - || errlog "could not convert $tiff to $pdf" \ - || return $? - trenn=$(detectsplit $pdf) \ - || return $? - rm $tiff \ - || errlog "could not delete $tiff" \ - || return $? - - if [ "$trenn" == "true" ]; then - rm $pdf \ - || errlog "could not delete $pdf" \ - || return $? - createpdf "$startcount" \ - "$((endcount - 1))" \ - "$basename" \ - || return $? - startcount=$((endcount + 1)) - fi - - ((endcount++)) - done -} - -scantopdf() { - local mode="$1" - local pagecount="$2" - local output="$3" - - command -v scanimage > /dev/null \ - || errlog "command scanimage not found" \ - || return $? - [ -n "$mode" ] \ - || errlog "missing mode" \ - || return $? - [ -n "$pagecount" ] \ - || errlog "missing pagecount" \ - || return $? - [ -n "$output" ] \ - || errlog "missing output" \ - || return $? - - scanimage --source=DocumentFeeder \ - --mode=$mode \ - --resolution=300 \ - --scanside=Duplex \ - --skip-blank-pages=yes \ - --format=tiff \ - --batch-count=$pagecount \ - --batch-increment=1 \ - --batch=scanned%03d.tiff \ - || errlog "scan error" \ - || return $? - - if [ "$output" == "multi" ]; then - createmultipdfs || return $? - return - fi - - createonepdf "$output" \ - || return $? -} diff --git a/scantopdf b/scantopdf deleted file mode 100755 index 4412985..0000000 --- a/scantopdf +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -# @scanbasic.sh@ - -main() -{ - local pagecount=$1 - local output=$2 - - echo "⭐ START: scantopdf" - - scantopdf "Color" "$pagecount" "$output" \ - || return $? - - echo "✅ SUCCESS: scantopdf" -} - -main $* diff --git a/scantopdfbw b/scantopdfbw deleted file mode 100755 index b1ff268..0000000 --- a/scantopdfbw +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -@scanbasic.sh@ - -main() -{ - local pagecount=$1 - local output=$2 - - echo "⭐ START: scantopdfbw" - - scantopdf "BW" "$pagecount" "$output" \ - || return $? - - echo "✅ SUCCESS: scantopdfbw" -} - -main $* diff --git a/scantopdfgray b/scantopdfgray deleted file mode 100755 index 103a175..0000000 --- a/scantopdfgray +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -@scanbasic.sh@ - -main() -{ - local pagecount=$1 - local output=$2 - - echo "⭐ START: scantopdfgray" - - scantopdf "Gray" "$pagecount" "$output" \ - || return $? - - echo "✅ SUCCESS: scantopdfgray" -} - -main $* diff --git a/src/control b/src/control new file mode 100644 index 0000000..803d8e5 --- /dev/null +++ b/src/control @@ -0,0 +1,6 @@ +Package: scantopdf +Version: $VERSION +Architecture: amd64 +Maintainer: Bastian Dehn +Depends: sane (>= 1.0.14), qpdf (>= 11.3.0), pdftk (>= 2.02), libtiff-tools (>= 4.5.0), ocrmypdf (>= 14.0.1), tesseract-ocr-deu (>= 4.1.0), zbar-tools (>= 0.23.92), qrencode (>= 4.1.), librsvg2-bin (>= 2.54.7) +Description: scan to pdf with ocr scripts for sane-kds-s2000w-net diff --git a/src/generate-trennblatt b/src/generate-trennblatt new file mode 100755 index 0000000..558c56e --- /dev/null +++ b/src/generate-trennblatt @@ -0,0 +1,34 @@ +#!/bin/bash + +errlog() +{ + local lastexit=$? + local msg="$1" + + echo "❌ ERROR $msg" + echo "🛑 EXIT $lastexit" + return $lastexit +} + +main() +{ + echo "⭐ START: generate-trennblatt" + + command -v qrencode > /dev/null \ + || errlog "command qrencode not found" \ + || return $? + command -v rsvg-convert > /dev/null \ + || errlog "command rsvg-convert not found" \ + || return $? + + echo "Trennblatt" \ + | qrencode --level=H \ + --type=SVG \ + --output=- \ + | rsvg-convert --format=pdf \ + --output=trennblatt-qr.pdf + + echo "✅ SUCCESS: generate-trennblatt" +} + +main diff --git a/src/scanbasic.sh b/src/scanbasic.sh new file mode 100644 index 0000000..b7d7692 --- /dev/null +++ b/src/scanbasic.sh @@ -0,0 +1,232 @@ +errlog() +{ + local lastexit=$? + local msg="$1" + + echo "❌ ERROR: $msg" + echo "🛑 EXIT $lastexit" + return $lastexit +} + +optimize() +{ + local input="$1" + local output="$input-out" + + command -v qpdf > /dev/null \ + || errlog "command qpdf not found" \ + || return $? + [ -n "$input" ] \ + || errlog "missing input" \ + || return $? + qpdf --linearize $input $output \ + || errlog "could not linearize $input" \ + || return $? + mv $output $input \ + || errlog "could not rename $output to $input" \ + || return $? +} + +addocr() +{ + local input="$1" + local output="$input-out" + + command -v ocrmypdf > /dev/null \ + || errlog "command ocrmypdf not found" \ + || return $? + [ -n "$input" ] \ + || errlog "missing input" \ + || return $? + + echo "ℹ️ INFO: adding ocr on file $input" + ocrmypdf --quiet --language deu $input $output \ + || errlog "could not add ocr on $input" \ + || return $? + + [ ! -f $output ] && return 0 + + mv $output $input \ + || errlog "could not rename $output to $input" \ + || return $? + optimize $input \ + || return $? +} + +createonepdf() +{ + local output=$1 + + command -v tiff2pdf > /dev/null \ + || errlog "command tiff2pdf not found" \ + || return $? + command -v pdftk > /dev/null \ + || errlog "command pdftk not found" \ + || return $? + [ -n "$output" ] \ + || errlog "missing output" \ + || return $? + + local output_pdf= + local tiff= + for tiff in scanned*.tiff; do + output_pdf=${tiff/.tiff/.pdf} + tiff2pdf $tiff -o $output_pdf \ + || errlog "could not convert tiff to pdf" \ + || return $? + rm $tiff \ + || errlog "could not delete $tiff" \ + || return $? + done + + pdftk scanned*.pdf output $output \ + || errlog "could not convert into single $output" \ + || return $? + + rm scanned*.pdf \ + || errlog "could not remove scanned pdfs" \ + || return $? + + addocr $output \ + || return $? +} + +createpdf() +{ + local start="$1" + local end="$2" + local basename="$3" + local countno= + local pdfs=() + basename=${basename:="out"} + + command -v pdftk > /dev/null \ + || errlog "command pdftk not found" \ + || return $? + [ -n "$start" ] \ + || errlog "missing start" \ + || return $? + [ -n "$end" ] \ + || errlog "missing end" \ + || return $? + + local i= + for i in $(seq $start $end); do + countno=$(printf "%03d" $i) + pdfs=$(echo $pdfs "scanned$countno.pdf") + done + + pdftk $pdfs output "$basename$(printf "%03d" $start).pdf" \ + || errlog "could not create pdf" \ + || return 1 + + for pdf in $pdfs; do + rm "$pdf" \ + || errlog "could not delete $pdf" \ + || return 1 + done + + addocr "$basename$(printf "%03d" $start).pdf" \ + || return $? +} + +detectsplit() +{ + local pdf=$1 + local trenn= + + command -v zbarimg > /dev/null \ + || errlog "command zbarimg not found" \ + || return $? + [ -n "$pdf" ] \ + || errlog "missing pdf" \ + || return $? + + trenn=$(zbarimg --raw --quiet $pdf) + + [ "$trenn" == "Trennblatt" ] \ + && echo "true" \ + && return 0 + + echo "false" +} + +createmultipdfs() +{ + local basename="multi" + local startcount=1 + local endcount=1 + + command -v tiff2pdf > /dev/null \ + || errlog "command tiff2pdf not found" \ + || return $? + [ -n "$basename" ] \ + || errlog "missing basename" \ + || return $? + + local pdf= + local tiff= + for tiff in scanned*.tiff; do + pdf=${tiff/.tiff/.pdf} + tiff2pdf $tiff -o $pdf \ + || errlog "could not convert $tiff to $pdf" \ + || return $? + trenn=$(detectsplit $pdf) \ + || return $? + rm $tiff \ + || errlog "could not delete $tiff" \ + || return $? + + if [ "$trenn" == "true" ]; then + rm $pdf \ + || errlog "could not delete $pdf" \ + || return $? + createpdf "$startcount" \ + "$((endcount - 1))" \ + "$basename" \ + || return $? + startcount=$((endcount + 1)) + fi + + ((endcount++)) + done +} + +scantopdf() { + local mode="$1" + local pagecount="$2" + local output="$3" + + command -v scanimage > /dev/null \ + || errlog "command scanimage not found" \ + || return $? + [ -n "$mode" ] \ + || errlog "missing mode" \ + || return $? + [ -n "$pagecount" ] \ + || errlog "missing pagecount" \ + || return $? + [ -n "$output" ] \ + || errlog "missing output" \ + || return $? + + scanimage --source=DocumentFeeder \ + --mode=$mode \ + --resolution=300 \ + --scanside=Duplex \ + --skip-blank-pages=yes \ + --format=tiff \ + --batch-count=$pagecount \ + --batch-increment=1 \ + --batch=scanned%03d.tiff \ + || errlog "scan error" \ + || return $? + + if [ "$output" == "multi" ]; then + createmultipdfs || return $? + return + fi + + createonepdf "$output" \ + || return $? +} diff --git a/src/scantopdf b/src/scantopdf new file mode 100755 index 0000000..4412985 --- /dev/null +++ b/src/scantopdf @@ -0,0 +1,18 @@ +#!/bin/bash + +# @scanbasic.sh@ + +main() +{ + local pagecount=$1 + local output=$2 + + echo "⭐ START: scantopdf" + + scantopdf "Color" "$pagecount" "$output" \ + || return $? + + echo "✅ SUCCESS: scantopdf" +} + +main $* diff --git a/src/scantopdfbw b/src/scantopdfbw new file mode 100755 index 0000000..b1ff268 --- /dev/null +++ b/src/scantopdfbw @@ -0,0 +1,18 @@ +#!/bin/bash + +@scanbasic.sh@ + +main() +{ + local pagecount=$1 + local output=$2 + + echo "⭐ START: scantopdfbw" + + scantopdf "BW" "$pagecount" "$output" \ + || return $? + + echo "✅ SUCCESS: scantopdfbw" +} + +main $* diff --git a/src/scantopdfgray b/src/scantopdfgray new file mode 100755 index 0000000..103a175 --- /dev/null +++ b/src/scantopdfgray @@ -0,0 +1,18 @@ +#!/bin/bash + +@scanbasic.sh@ + +main() +{ + local pagecount=$1 + local output=$2 + + echo "⭐ START: scantopdfgray" + + scantopdf "Gray" "$pagecount" "$output" \ + || return $? + + echo "✅ SUCCESS: scantopdfgray" +} + +main $* diff --git a/tests/scanbasic.bats b/tests/scanbasic.bats new file mode 100755 index 0000000..c1c8a4b --- /dev/null +++ b/tests/scanbasic.bats @@ -0,0 +1,290 @@ +#!/usr/bin/bats + +tiff2pdf() { + printf "$tiff2pdf_return" + return "$tiff2pdf_exit" +} + +ocrmypdf() { + printf "$ocrmypdf_return" + return "$ocrmypdf_exit" +} + +qpdf() { + printf "$qpdf_return" + return "$qpdf_exit" +} + +mv() { + printf "$mv_return" + return "$mv_exit" +} + +rm() { + printf "$rm_return" + return "$rm_exit" +} + +pdftk() { + printf "$pdftk_return" + return "$pdftk_exit" +} + +zbarimg() { + printf "$zbarimg_return" + return "$zbarimg_exit" +} + +scanimage() { + printf "$scanimage_return" + return "$scanimage_exit" +} + +setup() { + load "/usr/lib/bats/bats-assert/load" + load "/usr/lib/bats/bats-support/load" + + source ./src/scanbasic.sh + + export -f qpdf + qpdf_return= + qpdf_exit=0 + + export -f mv + mv_return= + mv_exit=0 + + export -f ocrmypdf + ocrmypdf_return= + ocrmypdf_exit=0 + + export -f tiff2pdf + tiff2pdf_return= + tiff2pdf_exit=0 + + export -f rm + rm_return= + rm_exit=0 + + export -f pdftk + pdftk_return= + pdftk_exit=0 + + export -f zbarimg + zbarimg_return= + zbarimg_exit=0 + + export -f scanimage + scanimage_return= + scanimage_exit=0 +} + +teardown() { + export -n qpdf + export -n mv + export -n ocrmypdf + export -n tiff2pdf + export -n rm + export -n pdftk + export -n zbarimg + export -n scanimage +} + +@test "should failure optimize input parameter is missing" { + run optimize + + assert_failure + assert_line --index 0 "❌ ERROR: missing input" +} + +@test "should failure optimize when qpdf failes" { + qpdf_exit=1 + + run optimize "testfile" + + assert_failure + assert_line --index 0 "❌ ERROR: could not linearize testfile" +} + +@test "should failure optimize when mv fails" { + mv_exit=1 + + run optimize "testfile" + + assert_failure + assert_line --index 0 \ + "❌ ERROR: could not rename testfile-out to testfile" +} + +@test "should success optimize" { + run optimize "testfile" + + assert_success +} + +@test "should failure addocr missing input parameter" { + run addocr + + assert_failure + assert_line --index 0 "❌ ERROR: missing input" +} + +@test "should failure addocr ocrmypdf fails" { + ocrmypdf_exit=1 + + run addocr "testfile" + + assert_failure + assert_line --index 0 "ℹ️ INFO: adding ocr on file testfile" + assert_line --index 1 "❌ ERROR: could not add ocr on testfile" +} + +@test "should success addocr" { + run addocr "testfile" + + assert_success + assert_line --index 0 "ℹ️ INFO: adding ocr on file testfile" +} + +@test "should failure createonepdf missing output parameter" { + run createonepdf + + assert_failure + assert_line --index 0 "❌ ERROR: missing output" +} + +@test "should failure createonepdf tiff2pdf fails" { + tiff2pdf_exit=1 + + run createonepdf "testfile" + + assert_failure + assert_line --index 0 "❌ ERROR: could not convert tiff to pdf" +} + +@test "should failure createonepdf pdftk fails" { + pdftk_exit=1 + + run createonepdf "testfile" + + assert_failure + assert_line --index 0 "❌ ERROR: could not convert into single testfile" +} + +@test "should success createonepdf" { + run createonepdf "testfile" + + assert_success +} + +@test "should failure createpdf missing start parameter" { + run createpdf + + assert_failure + assert_line --index 0 "❌ ERROR: missing start" +} + +@test "should failure createpdf missing end parameter" { + run createpdf "1" + + assert_failure + assert_line --index 0 "❌ ERROR: missing end" +} + +@test "should failure createpdf pdftk fails" { + pdftk_exit=1 + + run createpdf "1" "2" + + assert_failure + assert_line --index 0 "❌ ERROR: could not create pdf" +} + +@test "should success createpdf" { + run createpdf "1" "2" + + assert_success +} + +@test "should failure detectsplit missing parameter" { + run detectsplit + + assert_failure + assert_line --index 0 "❌ ERROR: missing pdf" +} + +@test "should failure detectsplit zbarimg fails" { + zbarimg_exit=1 + + run detectsplit "testfile.pdf" + + assert_success + assert_line --index 0 "false" +} + +@test "should success and false detectsplit zbarimg nothing find" { + run detectsplit "testfile.pdf" + + assert_success + assert_line --index 0 "false" +} + +@test "should success and true detectsplit zbarimg find trennblatt" { + zbarimg_return="Trennblatt" + + run detectsplit "testfile.pdf" + + assert_success + assert_line --index 0 "true" +} + +@test "should failure createmultipdfs tiff2pdf fails" { + tiff2pdf_exit=1 + + run createmultipdfs + + assert_failure + assert_line --index 0 "❌ ERROR: could not convert scanned*.tiff to scanned*.pdf" +} + +@test "should success createmutlipdfs" { + run createmultipdfs + + assert_success +} + +@test "should failure scantopdf missing mode parameter" { + run scantopdf + + assert_failure + assert_line --index 0 "❌ ERROR: missing mode" +} + +@test "should failure scantopdf missing pagecount parameter" { + run scantopdf "Color" + + assert_failure + assert_line --index 0 "❌ ERROR: missing pagecount" +} + +@test "should failure scantopdf missing output parameter" { + run scantopdf "Color" "5" + + assert_failure + assert_line --index 0 "❌ ERROR: missing output" +} + +@test "should failure scantopdf scanimage fails" { + scanimage_exit=1 + + run scantopdf "Color" "1" "test.pdf" + + assert_failure + assert_line --index 0 "❌ ERROR: scan error" +} + +@test "should success scantopdf" { + run scantopdf "Color" "1" "test.pdf" + + assert_success +}