about summary refs log tree commit diff stats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--hm/soispha/pkgs/scripts/apps/con2pdf138
1 files changed, 91 insertions, 47 deletions
diff --git a/hm/soispha/pkgs/scripts/apps/con2pdf b/hm/soispha/pkgs/scripts/apps/con2pdf
index 2ea6ba6e..4a45250e 100644
--- a/hm/soispha/pkgs/scripts/apps/con2pdf
+++ b/hm/soispha/pkgs/scripts/apps/con2pdf
@@ -59,66 +59,74 @@ ARGUMENTS:
 EOF
 }
 
-scan() {
-    number_of_pages="$1";
-    device="$2";
-    output_directory="$(readlink -f "$3")";
-    name="$4";
-    method="$5";
-
-    [ -z "$number_of_pages" ] && die "Parameter 'number_of_pages' is not set!"
-    [ -z "$device" ] && die "Parameter 'device' is not set!"
-    [ -z "$output_directory" ] && die "Parameter 'output_directory' is not set!"
-    [ -z "$name" ] && die "Parameter 'name' is not set!"
-    [ -z "$method" ] && die "Parameter 'method' is not set!"
-
-    tiff_temp_path="$(mktmp -d)";
-    cd "$tiff_temp_path" || die "Bug"
-
-    msg "Started scanning...";
-    if [ "$method" = "Flatbed" ]; then
-        for i in $(seq "$number_of_pages"); do
-            do_until_success \
-                "scanimage --format=tiff --progress --source='$method' --device='$device' --output-file=$i.tiff" \
-                "warn 'Retrying scan, as we assume a network error!'"
-            if [ "$number_of_pages" -ne 1 ];then
-                msg "Finished turn, please change side!";
-                readp "Press enter to continue" noop
-            fi
-        done
-    else
-        for i in $(seq "$number_of_pages");do
-            do_until_success \
-                "scanimage --format=tiff --progress --source='$method' --device='$device' --batch=%d.tif --batch-increment='$number_of_pages' --batch-start='$i'" \
-                "warn 'Retrying scan, as we assume a network error!'"
-
+scan_adf() {
+    device="$1";
+    sides_per_page="$2";
+    method="ADF";
+    for i in $(seq "$sides_per_page");do
+        do_until_success \
+            "scanimage --format=tiff --progress --source='$method' --device='$device' --batch=%d.tif --batch-increment='$sides_per_page' --batch-start='$i'" \
+            "warn 'Retrying scan, as we assume a network error!'"
+
+
+        if [ "$sides_per_page" -ne 1 ];then
+            msg "Finished turn, please change side!";
+            readp "Press enter to continue" noop
+        fi
+    done
+}
+process_images_adf() {
+    tiff_temp_path="$1";
+    output_directory="$2";
+    name="$3";
 
-            if [ "$number_of_pages" -ne 1 ];then
-                msg "Finished turn, please change side!";
-                readp "Press enter to continue" noop
-            fi
-        done
-    fi
+    counter=0;
+    pdf_counter=0;
+    image_cache="$(mktmp)"
+    while read -r scanned_image; do
+        echo "$scanned_image" >> "$image_cache"
+        : $((counter += 1))
+        if [ "$counter" = "$number_of_pages" ]; then
+            counter=0;
+            convert_images_flatbed "$image_cache" "${name}_$pdf_counter" "$output_directory"
+            : $((pdf_counter += 1))
+            echo "" > "$image_cache"
+        fi
+    done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -n)"
+}
 
-    msg "Creating output directory...";
-    mkdir "$output_directory";
-    cd "$output_directory" || die "Bug"
+scan_flatbed() {
+    device="$1";
+    number_of_pages"$2";
+    method="Flatbed";
+    for i in $(seq "$number_of_pages"); do
+        do_until_success \
+            "scanimage --format=tiff --progress --source='$method' --device='$device' --output-file=$i.tiff" \
+            "warn 'Retrying scan, as we assume a network error!'"
+        if [ "$number_of_pages" -ne 1 ];then
+            msg "Finished turn, please change side!";
+            readp "Press enter to continue" noop
+        fi
+    done
+}
+process_images_flatbed() {
+    tiff_temp_path="$1";
+    output_directory="$2";
+    name="$3";
 
     counter=0;
     image_cache="$(mktmp)"
-    msg "Converting images to pdfs...";
     while read -r scanned_image; do
         echo "$scanned_image" >> "$image_cache"
         : $((counter += 1))
         if [ "$counter" = "$number_of_pages" ]; then
             counter=0;
-            process_images "$image_cache" "$name" "$output_directory"
+            convert_images_flatbed "$image_cache" "$name" "$output_directory"
             echo "" > "$image_cache"
         fi
     done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -n)"
 }
-
-process_images() {
+convert_images_flatbed() {
     image_cache="$1";
     name="$2";
     output_dir="$3";
@@ -129,12 +137,48 @@ process_images() {
     done < "$image_cache"
 
     while [ -e "$output_dir/${name}.pdf" ]; do
-        name="${name}_$(tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' </dev/urandom | head -c 25)"
+        name="${name}_$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 25)"
     done
 
     convert "$@" -compress jpeg -quality 100 "$output_dir/${name}.pdf"
 }
 
+scan() {
+    number_of_pages="$1";
+    device="$2";
+    output_directory="$(readlink -f "$3")";
+    name="$4";
+    method="$5";
+
+    [ -z "$number_of_pages" ] && die "Parameter 'number_of_pages' is not set!"
+    [ -z "$device" ] && die "Parameter 'device' is not set!"
+    [ -z "$output_directory" ] && die "Parameter 'output_directory' is not set!"
+    [ -z "$name" ] && die "Parameter 'name' is not set!"
+    [ -z "$method" ] && die "Parameter 'method' is not set!"
+
+    tiff_temp_path="$(mktmp -d)";
+    cd "$tiff_temp_path" || die "Bug"
+
+    msg "Started scanning...";
+    if [ "$method" = "Flatbed" ]; then
+        scan_flatbed "$device" "$number_of_pages"
+    else
+        scan_adf "$device" "$number_of_pages"
+    fi
+
+    msg "Creating output directory...";
+    mkdir "$output_directory";
+    cd "$output_directory" || die "Bug"
+
+    msg "Converting images to pdfs...";
+    if [ "$method" = "Flatbed" ]; then
+        process_images_flatbed "$tiff_temp_path" "$output_directory" "$name"
+    else
+        process_images_adf "$tiff_temp_path" "$output_directory" "$name"
+    fi
+}
+
+
 for input in "$@"; do
     case "$input" in
         "--help" | "-h")