os/pkgs/scan2paperless.nix

59 lines
1.5 KiB
Nix

self: with self; ''
echo "$@"
DUPLEX=,Duplex
removeempty="true"
[[ $1 == "-s" ]] && {
DUPLEX=""
removeempty=false
shift
}
[[ $1 == "-a" ]] && {
DUPLEX=,Duplex
removeempty="true"
shift
}
[[ $1 == "-d" ]] && {
DUPLEX=,Duplex
shift
}
cleanup() {
echo finalizing pdf file.
${img2pdf}/bin/img2pdf --pdfa --output /tmp/scan2paperless_$$.pdf /tmp/scan2paperless_$$_*.png && \
rm -f /tmp/scan2paperless_$$_*.png
scp /tmp/scan2paperless_$$.pdf pub-solar@paperless.faenix.eu:~/.local/share/scandir \
&& rm -f /tmp/scan2paperless_$$* \
|| echo upload failed, retaining file /tmp/scan2paperless_$$.pdf >&2
}
trap 'cleanup; exit 1' EXIT
${sane-backends}/bin/scanimage --device 'brother5:bus1;dev5' --format=png --resolution 300 --batch=/tmp/scan2paperless_$$_%d.png --AutoDeskew=yes --batch-start=10 --source="Automatic Document Feeder(left aligned''${DUPLEX})" -x 210 -y 297
if $removeempty
then
threshold=99
images=( )
values=( )
for f in /tmp/scan2paperless_$$_*.png
do
images[''${#images[@]}]=$f
values[''${#values[@]}]=$(${imagemagick}/bin/convert "$f" -fuzz 02% -fill black +opaque white -fill white -opaque white -format "%[fx:100*mean]" info:)
done
for ((i=0;i<''${#images[@]};i++))
do
if [[ $(echo "''${values[i]} > $threshold" | ${bc}/bin/bc -l) == "1" ]]
then
# bc will output 1 if the comparison is true, 0 otherwise
echo image "''${images[i]}" was found to be mostly white, removing.
rm "''${images[i]}"
fi
done
fi
''