diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..917110b --- /dev/null +++ b/flake.nix @@ -0,0 +1,20 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + outputs = { + self, + nixpkgs, + } @ inputs: let + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages."${system}"; + in { + packages."${system}".default = pkgs.writeShellApplication { + name = "hello_world"; + + runtimeInputs = with pkgs; [sane-backends brscan5 ghostscript]; + + text = builtins.readFile ./scanbuddy.bash; + }; + }; +} diff --git a/scanbuddy.bash b/scanbuddy.bash index 06565ab..54b57e0 100755 --- a/scanbuddy.bash +++ b/scanbuddy.bash @@ -1,9 +1,5 @@ #!/usr/bin/env bash -# device="brother5:bus2;dev2" -device=$(scanimage -f "%d%n" | grep brother5 | head -n 1) -echo "Selecting scanner '$device'" - # option values for the brother DS-740D scanner simplex_source='Automatic Document Feeder(left aligned)' duplex_source='Automatic Document Feeder(left aligned,Duplex)' @@ -25,6 +21,10 @@ last_out_file="out$current_pageno.pdf" next_out_file="out$((current_pageno+1)).pdf" scan() { + # device="brother5:bus2;dev2" + device=$(scanimage -f "%d%n" | grep brother5 | head -n 1) + echo "Selecting scanner '$device'" + usage() { echo "scanbuddy.bash scan -d -r -b " } @@ -68,7 +68,7 @@ scan() { echo "Scanning to '$next_out_file'..." scanimage \ - --device-name=$device \ + --device-name="$device" \ --format=pdf \ --output-file="$next_out_file" \ --mode="$scanner_mode" \ @@ -77,14 +77,36 @@ scan() { -x 210 -y 297 # A4 } +if [ "$#" -lt 1 ]; then + echo "You need an argument!" + exit 1 +fi + case "$1" in scan) scan "${@:2}" ;; unscan) - rm "$current_pageno" + rm "$last_out_file" ;; dispatch) + readarray -d '' out_files < <(find . -regextype posix-awk -regex "\./out[0-9]+\.pdf" -print0 | sort -Vz) + combined_file=$(mktemp --suff="_scanbuddy.pdf") + gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE="$combined_file" -dBATCH "${out_files[@]}" + echo "Merged pdf written to $combined_file" + + # send to paperless + PAPERLESS_STATUS=$(curl \ + -H "Authorization: Token $PAPERLESS_TOKEN" \ + -F document=@"$combined_file" \ + "$([ -v PAPERLESS_TAG ] && echo "-F tags=$PAPERLESS_TAG")" \ + "${PAPERLESS_URL}/api/documents/post_document/") || FAILED=1 + + if [ -v FAILED ]; then + echo "Paperless failed with message:\n$PAPERLESS_STATUS" + else + echo "Paperless consumption job: '$PAPERLESS_STATUS'" + fi ;; *) echo "Usage: scanbuddy"