about summary refs log tree commit diff stats
path: root/home-manager
diff options
context:
space:
mode:
Diffstat (limited to 'home-manager')
-rw-r--r--home-manager/packages/scripts.nix2
-rwxr-xr-xhome-manager/packages/scripts/apps/con2pdf217
2 files changed, 132 insertions, 87 deletions
diff --git a/home-manager/packages/scripts.nix b/home-manager/packages/scripts.nix
index 67d00e7b..4cda9d03 100644
--- a/home-manager/packages/scripts.nix
+++ b/home-manager/packages/scripts.nix
@@ -21,7 +21,7 @@
   con2pdf-scr = write_script {
     name = "con2pdf";
     path = "apps";
-    dependencies = builtins.attrValues {inherit (pkgs) sane-backends imagemagick;};
+    dependencies = builtins.attrValues {inherit (pkgs) sane-backends imagemagick gawk coreutils fd;};
   };
 
   dldragon-scr = write_script {
diff --git a/home-manager/packages/scripts/apps/con2pdf b/home-manager/packages/scripts/apps/con2pdf
index 30800043..95b634b7 100755
--- a/home-manager/packages/scripts/apps/con2pdf
+++ b/home-manager/packages/scripts/apps/con2pdf
@@ -1,101 +1,146 @@
 #! /usr/bin/env dash
-# shellcheck disable=SC2086
 # shellcheck source=/dev/null
 . %SHELL_LIBRARY_PATH
 
 
-# Vars
-counter=0;
-a_set=false;
-pw=$(pwd)
-
-usage() {
-    cat << EOF
-    This is a help doc!
-    -p for path
-    -h for help
-    -n for name
-    -g gen pdf dir
-    -r for number of pages per pdf
+# needed for help() and version
+# shellcheck disable=2034
+AUTORS="Soispha";
+# shellcheck disable=2034
+YEARS="2023";
+# shellcheck disable=2034
+VERSION="1.0.0";
+NAME="con2pdf";
+
+help() {
+cat << EOF
+Scan images and turn them into a pdf.
+
+Usage:
+    $NAME [--help|--version|--out-dir|--num-pages|--method] --name --device
+
+Options:
+    --out-dir | -o
+                            Path to place the generated pdf files (default: ./pdf).
+    --name | -n
+                            Name for the pdf files (e.g. "\$name"_1.pdf).
+    --num-pages | -p
+                            Number of pages to merge into one pdf (default: 1).
+    --device | -d
+                            Device used for scanning.
+    --method | -m
+                            Method to use for scanning (default: ADF).
+    --help | -h
+                            Display this help and exit.
+    --version | -v
+                            Display version and copyright information and exit.
 EOF
-exit 0;
 }
 
-# Ops
-while getopts ":hp:n:gr:" flag;do
-    case "$flag" in
-        p)
-            Command_path=$OPTARG
-            ;;
-        n)
-            name=$OPTARG
-            ;;
-        g)
-            gen=true
-            ;;
-        r)
-            pages=$OPTARG
-            ;;
-        h)
-            usage;;
-        *)
-            usage;;
+scan() {
+    number_of_pages="$1";
+    device="$2";
+    output_directory="$3";
+    name="$4";
+    method="$5";
 
-        esac
-    done
-[ -z "$pages" ] && die "No pages set"
-
-
-# Scan
-if [ -z $Command_path ];then
-    tm=$(mktemp -d)
-    cd $tm || die "Bug"
-
-    for i in $(seq $pages);do
-        scanimage --format=tiff --progress \
-            --source ADF --device="airscan:w1:Brother DCP-9022CDW" \
-            --batch=%d.tif --batch-increment=$pages --batch-start=$i
-        [ $? -ne 0 ] && scanimage --format=tiff --progress \
-            --source ADF --device="airscan:w1:Brother DCP-9022CDW" \
-            --batch=%d.tif --batch-increment=$pages --batch-start=$i
-        if [ $pages -ge 2 ];then
-            msg "Finished first turn, please change side!";
-            readp "Ready to continue?" noop
+    [ -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"
+
+    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!'"
+
+
+        if [ "$number_of_pages" -ne 1 ];then
+            msg "Finished turn, please change side!";
+            readp "Press enter to continue" noop
         fi
     done
-else
-    tm=$Command_path;
-fi
-cd $pw || die "Bug"
-
-# mk pdf
-if [ "$gen" = true ];then
-    mkdir pdfs
-    cd pdfs/  || die "Bug"
-fi
-
-while IFS= read -r i;do
-
-    if [ $pages -ge 2 ];then
-        a_set=false;
-        if [ -z "$a" ];then
-            a="$i"
-            a_set=true;
-            : $((counter += 1))
-        fi
 
-        if [ -n "$a" ] && ! [ "$a_set" = true ];then
-            convert "$a" "$i" -compress jpeg -quality 70 "$name"_"$counter".pdf
-            a=
+    cd "$output_directory" || die "Bug"
+
+    # mk pdf
+    mkdir pdfs;
+    cd ./pdfs/  || die "Bug"
+
+    counter=0;
+    while read -r scanned_image_a;do
+        if [ "$number_of_pages" -ne 1 ];then
+            scanned_image_b_set=false;
+            if [ -z "$scanned_image_b" ];then
+                scanned_image_b="$i"
+                scanned_image_b_set=true;
+                : $((counter += 1))
+            fi
+
+            if [ -n "$scanned_image_a" ] && [ "$scanned_image_b_set" = false ];then
+                convert "$scanned_image_b" "$scanned_image_a" -compress jpeg -quality 100 "$name"_"$counter".pdf
+            fi
+        else
+            : $((counter += 1))
+            convert "$scanned_image_a" -compress jpeg -quality 100 "$name"_"$counter".pdf
         fi
-    else
-        : $((counter += 1))
-        convert "$i" -compress jpeg -quality 70 "$name"_"$counter".pdf
-    fi
-
-
-done < "$(tmp "ls $tm | sort -n | awk -v a=$tm -v c='/' '{printf a} {printf c} {print \$0}' ")"
 
+    done < "$(tmp "fd . '$tiff_temp_path' | sort -n | awk -v dir='$tiff_temp_path' '{printf dir/\$0}' ")"
+}
 
-rm -r $tm
-if [ -d /tmp/LIB_FILE_TEMP_DIR/ ];then rm -r /tmp/LIB_FILE_TEMP_DIR/; fi
+for input in "$@"; do
+    case "$input" in
+        "--help" | "-h")
+            help;
+            exit 0;
+            ;;
+        "--version" | "-v")
+            version;
+            exit 0;
+            ;;
+    esac
+done
+
+number_of_pages="1";
+unset device;
+output_directory="$(pwd)/pdf";
+unset name;
+method="ADF";
+
+while [ "$#" -ne 0 ]; do
+    case "$1" in
+        "--help" | "-h")
+            ;;
+        "--version" | "-v")
+            ;;
+        "--out-dir" | "-o")
+            shift 1;
+            output_directory="$1";
+            ;;
+        "--name" | "-n")
+            shift 1;
+            name="$1";
+            ;;
+        "--num-pages" | "-p")
+            shift 1;
+            number_of_pages="$1";
+            ;;
+        "--device" | "-d")
+            shift 1;
+            device="$1";
+            ;;
+        "--method" | "-m")
+            shift 1;
+            method="$1";
+            ;;
+        *)
+            die "Command line arg $1 does not exist. See --help for a list.";
+            ;;
+    esac
+    shift 1;
+done
+scan "$number_of_pages" "$device" "$output_directory" "$name" "$method";