From 3f600ab07dbad3b6dd7655587ddea158b19aea71 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sat, 26 Aug 2023 23:42:21 +0200 Subject: Refactor(treewide): Abbreviate path names --- hm/soispha/pkgs/scripts/apps/aumo | 54 +++++++++++++ hm/soispha/pkgs/scripts/apps/con2pdf | 152 +++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100755 hm/soispha/pkgs/scripts/apps/aumo create mode 100755 hm/soispha/pkgs/scripts/apps/con2pdf (limited to 'hm/soispha/pkgs/scripts/apps') diff --git a/hm/soispha/pkgs/scripts/apps/aumo b/hm/soispha/pkgs/scripts/apps/aumo new file mode 100755 index 00000000..e49ac82d --- /dev/null +++ b/hm/soispha/pkgs/scripts/apps/aumo @@ -0,0 +1,54 @@ +#! /usr/bin/env dash + +# shellcheck source=/dev/null +SHELL_LIBRARY_VERSION="1.1.4" . %SHELL_LIBRARY_PATH + +# TODO: rewrite this + +if [ "$1" = "-u" ];then udisksctl unmount -b "$(cat "$HOME/.cache/aumo/mnt_disk")"; exit 0; fi +all_available_disks=$(mktemp) +awk '{print $4}' /proc/partitions | sed '1 s|.*||' > "$all_available_disks" + +mounted_disk=$(mktemp) +awk '{print $1}' /proc/mounts > "$mounted_disk" + +unmounted_disks=$(mktemp) + +for i in $(cat $all_available_disks);do + if ! grep -qw "/dev/$i" $mounted_disk && ! ls /sys/block/$i/*/partition > /dev/null 2> /dev/null;then echo $i >> $unmounted_disks;fi +done + + +u=1 +disk=-1 +while [ $disk -lt 0 ] || [ $disk -gt $(wc -l $unmounted_disks | awk '{print $1}') ];do + + [ "$1" = "-v" ] && for i in $(cat $unmounted_disks);do + sudo fdisk -l "/dev/$i" + done + + for i in $(cat $unmounted_disks);do + printf "%4s) %s\n" "$u" "$i" + u=$(( u + 1 )) + done + + printf "%4s) Exit\n" "0" + printf "Input number: " + read -r disk + + [ $disk -lt 0 ] || [ $disk -gt $(wc -l $unmounted_disks | awk '{print $1}') ] && printf "Wrong number. Please retry\n" + [ $disk -eq 0 ] && exit 0 +done + +mnt_disk="/dev/$(awk -v n=$disk 'NR==n' $unmounted_disks)" + +if ! [ "$1" = "-v" ] && [ -n "$1" ];then sudo mount "$mnt_disk" "$1" && exit 0;fi +[ -n "$2" ] && sudo mount "$mnt_disk" "$1" && exit 0 +if mount | grep -q ~/mnt ;then dien "Something is mounted at ~/mnt";else udisksctl mount -b "$mnt_disk";fi + +[ -e $HOME/.cache/aumo/mnt_disk ] || mkdir -p $HOME/.cache/aumo +echo $mnt_disk >> $HOME/.cache/aumo/mnt_disk +rm $all_available_disks +rm $mounted_disk +rm $unmounted_disks +[ -d /tmp/LIB_FILE_TEMP_DIR/ ] && rm -r /tmp/LIB_FILE_TEMP_DIR/ diff --git a/hm/soispha/pkgs/scripts/apps/con2pdf b/hm/soispha/pkgs/scripts/apps/con2pdf new file mode 100755 index 00000000..9cadc376 --- /dev/null +++ b/hm/soispha/pkgs/scripts/apps/con2pdf @@ -0,0 +1,152 @@ +#! /usr/bin/env dash + +# shellcheck source=/dev/null +SHELL_LIBRARY_VERSION="1.1.4" . %SHELL_LIBRARY_PATH + + +# needed for help() and version +# shellcheck disable=2034 +AUTHORS="Soispha"; +# shellcheck disable=2034 +YEARS="2023"; +# shellcheck disable=2034 +VERSION="1.0.0"; + +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 +} + +scan() { + number_of_pages="$1"; + device="$2"; + output_directory="$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 + do_until_success \ + "scanimage --format=tiff --progress --source='$method' --device='$device' --output-file=1.tiff" \ + "warn 'Retrying scan, as we assume a network error!'" + 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!'" + + + if [ "$number_of_pages" -ne 1 ];then + msg "Finished turn, please change side!"; + readp "Press enter to continue" noop + fi + done + fi + + msg "Creating output directory..."; + mkdir "$output_directory"; + cd "$output_directory" || die "Bug" + + counter=0; + msg "Converting images to pdfs..."; + 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 + + done < "$(tmp "fd . '$tiff_temp_path' | sort -n")" +} + +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"; -- cgit 1.4.1