From cc09b607330385d6d81b9c47cf64a2442432e93f Mon Sep 17 00:00:00 2001 From: Soispha Date: Tue, 20 Feb 2024 16:35:15 +0100 Subject: refactor(treewide): Add a `.sh` extension to shell scripts --- hm/soispha/pkgs/scripts/apps/aumo | 54 -------- hm/soispha/pkgs/scripts/apps/aumo.sh | 54 ++++++++ hm/soispha/pkgs/scripts/apps/con2pdf | 237 -------------------------------- hm/soispha/pkgs/scripts/apps/con2pdf.sh | 237 ++++++++++++++++++++++++++++++++ hm/soispha/pkgs/scripts/apps/fupdate | 167 ---------------------- hm/soispha/pkgs/scripts/apps/fupdate.sh | 167 ++++++++++++++++++++++ 6 files changed, 458 insertions(+), 458 deletions(-) delete mode 100755 hm/soispha/pkgs/scripts/apps/aumo create mode 100755 hm/soispha/pkgs/scripts/apps/aumo.sh delete mode 100644 hm/soispha/pkgs/scripts/apps/con2pdf create mode 100755 hm/soispha/pkgs/scripts/apps/con2pdf.sh delete mode 100644 hm/soispha/pkgs/scripts/apps/fupdate create mode 100755 hm/soispha/pkgs/scripts/apps/fupdate.sh (limited to 'hm/soispha/pkgs/scripts/apps') diff --git a/hm/soispha/pkgs/scripts/apps/aumo b/hm/soispha/pkgs/scripts/apps/aumo deleted file mode 100755 index b5840fc3..00000000 --- a/hm/soispha/pkgs/scripts/apps/aumo +++ /dev/null @@ -1,54 +0,0 @@ -#! /usr/bin/env dash - -# shellcheck source=/dev/null -SHELL_LIBRARY_VERSION="2.0.13" . %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/aumo.sh b/hm/soispha/pkgs/scripts/apps/aumo.sh new file mode 100755 index 00000000..b5840fc3 --- /dev/null +++ b/hm/soispha/pkgs/scripts/apps/aumo.sh @@ -0,0 +1,54 @@ +#! /usr/bin/env dash + +# shellcheck source=/dev/null +SHELL_LIBRARY_VERSION="2.0.13" . %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 deleted file mode 100644 index be8810bb..00000000 --- a/hm/soispha/pkgs/scripts/apps/con2pdf +++ /dev/null @@ -1,237 +0,0 @@ -#! /usr/bin/env dash - -# shellcheck source=/dev/null -SHELL_LIBRARY_VERSION="2.0.13" . %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 [OPTIONS] --name --device - -OPTIONS: - --out-dir | -o [FILE] - Path to place the generated pdf files (default: ./pdf). - - --name | -n NAME - Name for the pdf files (e.g. _1.pdf). - - --num-pages | -p NUM - Number of pages to merge into one pdf (default: 1). - - --device | -d DEVICE - Device used for scanning. - - --method | -m METHOD - Method to use for scanning (default: ADF). - - --help | -h - Display this help and exit. - - --version | -v - Display version and copyright information and exit. -ARGUMENTS: - FILE := [[fd . --max-depth 3]] - A name of a file to store, default is: ./pdf - - NAME | * := [[fd . --max-depth 3]] - The basename of the generated files - - NUM | *([0-9]) := 0 | 1 | 2 | 3 | 4 - Possible numbers of pages, can be more than 4 - - DEVICE := [[$(cat %DEVICE_FUNCTION)]] - Possible scanner names - - METHOD := ADF | Flatbed - The scanning method to use, not all scanners support both of - these. The default is ADF -EOF -} - -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"; - - counter=0; - pdf_counter=0; - image_cache="$(mktmp)" - while read -r scanned_image; do - dbg "$scanned_image (scanned_image) at $counter (counter)"; - echo "$scanned_image" >> "$image_cache" - : $((counter += 1)) - if [ "$counter" = "$number_of_pages" ]; then - dbg "$counter == $number_of_pages" - counter=0; - convert_images "$image_cache" "${name}_$pdf_counter" "$output_directory" - : $((pdf_counter += 1)) - printf "" > "$image_cache" - fi - done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -V)" -} - -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)" - while read -r scanned_image; do - echo "$scanned_image" >> "$image_cache" - : $((counter += 1)) - if [ "$counter" = "$number_of_pages" ]; then - counter=0; - convert_images "$image_cache" "$name" "$output_directory" - printf "" > "$image_cache" - fi - done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -V)" -} -convert_images() { - image_cache="$1"; - pdf_name="$2"; - output_dir="$3"; - - set -- - while read -r image; do - dbg "setting image: $image"; - set -- "$@" "$image" - done < "$image_cache" - - while [ -e "$output_dir/${pdf_name}.pdf" ]; do - pdf_name="${pdf_name}_$(tr -dc 'A-Za-z0-9' _1.pdf). + + --num-pages | -p NUM + Number of pages to merge into one pdf (default: 1). + + --device | -d DEVICE + Device used for scanning. + + --method | -m METHOD + Method to use for scanning (default: ADF). + + --help | -h + Display this help and exit. + + --version | -v + Display version and copyright information and exit. +ARGUMENTS: + FILE := [[fd . --max-depth 3]] + A name of a file to store, default is: ./pdf + + NAME | * := [[fd . --max-depth 3]] + The basename of the generated files + + NUM | *([0-9]) := 0 | 1 | 2 | 3 | 4 + Possible numbers of pages, can be more than 4 + + DEVICE := [[$(cat %DEVICE_FUNCTION)]] + Possible scanner names + + METHOD := ADF | Flatbed + The scanning method to use, not all scanners support both of + these. The default is ADF +EOF +} + +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"; + + counter=0; + pdf_counter=0; + image_cache="$(mktmp)" + while read -r scanned_image; do + dbg "$scanned_image (scanned_image) at $counter (counter)"; + echo "$scanned_image" >> "$image_cache" + : $((counter += 1)) + if [ "$counter" = "$number_of_pages" ]; then + dbg "$counter == $number_of_pages" + counter=0; + convert_images "$image_cache" "${name}_$pdf_counter" "$output_directory" + : $((pdf_counter += 1)) + printf "" > "$image_cache" + fi + done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -V)" +} + +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)" + while read -r scanned_image; do + echo "$scanned_image" >> "$image_cache" + : $((counter += 1)) + if [ "$counter" = "$number_of_pages" ]; then + counter=0; + convert_images "$image_cache" "$name" "$output_directory" + printf "" > "$image_cache" + fi + done < "$(tmp_pipe fd . "$tiff_temp_path" "|" sort -V)" +} +convert_images() { + image_cache="$1"; + pdf_name="$2"; + output_dir="$3"; + + set -- + while read -r image; do + dbg "setting image: $image"; + set -- "$@" "$image" + done < "$image_cache" + + while [ -e "$output_dir/${pdf_name}.pdf" ]; do + pdf_name="${pdf_name}_$(tr -dc 'A-Za-z0-9' "$config_path"; - ;; - *) - UPDATE_SCRIPT_NOT_ALLOWED=true; - ;; - esac -} - -# Runs the provided script and continues to update the nix flake -# Takes the path to the script and the directory to the flake as arguments -# If the path to the update script is empty, it will be ignored -update(){ - update_script="$1"; - flake_base_dir="$2"; - - [ "$update_script" = "" ] || "$update_script"; - dbg "changed directory to: $flake_base_dir"; - cd "$flake_base_dir" || die "Provided dir \'$flake_base_dir\' can not be accessed"; - - nix flake update - if grep '[^0-9]_[0-9]' flake.lock > /dev/null; then - batgrep '[^0-9]_[0-9]' flake.lock; - die "Your flake.nix contains duplicate inputs!"; - fi -} - -help() { -cat << EOF -This is a Nix flake update manager. - -Usage: - $NAME [--help] - -Options: - --help | -h - Display this help and exit. - --version | -v - Display version and copyright information and exit. - -Commands: - flake - update the flake project - - runs a executable called "update-", if it exists -EOF -} - -main() { - if ! [ "$UPDATE_SCRIPT_NOT_ALLOWED" = true ]; then - update_script="$(check_for_update_script)"; - flake_base_dir="$(search_flake_base_dir)"; # Assume, that the update script is in the base dir - dbg "update_script is: $update_script"; - dbg "flake_base_dir is: $flake_base_dir"; - - if [ "$update_script" = "" ]; then - update "" "$flake_base_dir"; - elif check_for_allowed_update_script "$update_script" && ! [ "$update_script" = "" ]; then - update "$update_script" "$flake_base_dir"; - else - ask_to_allow_update_script "$update_script"; - main; - fi - fi -} - -if [ "$#" -eq 0 ]; then - main; -fi - -for input in "$@"; do - case "$input" in - "--help" | "-h") - help; - exit 0; - ;; - "--version" | "-v") - version; - exit 0; - ;; - "--") - end_of_cli_options=true; - ;; - esac - [ "$end_of_cli_options" = "true" ] && break -done - -while [ "$#" -ne 0 ]; do - case "$1" in - "flake") - main; - shift 1; - ;; - *) - command="$1"; - shift 1; - [ "$1" = "--" ] && shift 1 - if which update-"$command" > /dev/null 2>&1;then - if [ "$end_of_cli_options" = "true" ]; then - update-"$command" "$@"; - else - update-"$command"; - fi - else - die "command \"update-$command\" is not executable, or does not exist"; - fi - ;; - esac - [ "$end_of_cli_options" = "true" ] && break -done diff --git a/hm/soispha/pkgs/scripts/apps/fupdate.sh b/hm/soispha/pkgs/scripts/apps/fupdate.sh new file mode 100755 index 00000000..7722c070 --- /dev/null +++ b/hm/soispha/pkgs/scripts/apps/fupdate.sh @@ -0,0 +1,167 @@ +#! /usr/bin/env dash + +# shellcheck source=/dev/null +SHELL_LIBRARY_VERSION="2.0.13" . %SHELL_LIBRARY_PATH + + +UPDATE_SCRIPT_NAME="update.sh"; +CONFIG_DIRECTORY_PATH="$HOME/.local/share/flake-update"; + +AUTHORS="Soispha" +YEARS="2023" + +# Searches upward for a `UPDATE_SCRIPT_NAME` script +# Returns a path to the script if it exists, otherwise nothing is returned +check_for_update_script() { + dirname="$(search_upward_files "$UPDATE_SCRIPT_NAME")" + if [ "$dirname" ]; then + printf "%s/%s" "$dirname" "$UPDATE_SCRIPT_NAME" + fi +} + +# Checks if a given path to the update script is allowed. +# Takes the path as input +# Return 0, if allowed, 1 if not. +check_for_allowed_update_script(){ + update_script="$1"; + config_path="${CONFIG_DIRECTORY_PATH}${update_script}"; + update_script_hash="$(sha256sum "$update_script")"; + if [ -f "$config_path" ]; then + if [ "$(cat "$config_path")" = "$update_script_hash" ];then + dbg "Recorded hash matches"; + return 0; + else + dbg "Recorded hash \'$(cat "$config_path")\' does not match real hash \'$update_script_hash\', assuming not allowed"; + return 1; + fi + else + dbg "Path \'$config_path\' does not exist, assuming not allowed"; + return 1; + fi +} + + +# Asks the user if they want to allow a given script. +# Takes the path as input +ask_to_allow_update_script(){ + update_script="$1"; + config_path="${CONFIG_DIRECTORY_PATH}${update_script}"; + update_script_hash="$(sha256sum "$update_script")"; + println "\033[2J"; # clear the screen + cat "$update_script"; + readp "Do you want to allow this script?[N/y]: " allow; + dbg "allow is: $allow"; + case "$allow" in + [yY]) + dbg "allowed script"; + dbg "storing contents in: $config_path"; + mkdir --parents "$(dirname "$config_path")"; + print "$update_script_hash" > "$config_path"; + ;; + *) + UPDATE_SCRIPT_NOT_ALLOWED=true; + ;; + esac +} + +# Runs the provided script and continues to update the nix flake +# Takes the path to the script and the directory to the flake as arguments +# If the path to the update script is empty, it will be ignored +update(){ + update_script="$1"; + flake_base_dir="$2"; + + [ "$update_script" = "" ] || "$update_script"; + dbg "changed directory to: $flake_base_dir"; + cd "$flake_base_dir" || die "Provided dir \'$flake_base_dir\' can not be accessed"; + + nix flake update + if grep '[^0-9]_[0-9]' flake.lock > /dev/null; then + batgrep '[^0-9]_[0-9]' flake.lock; + die "Your flake.nix contains duplicate inputs!"; + fi +} + +help() { +cat << EOF +This is a Nix flake update manager. + +Usage: + $NAME [--help] + +Options: + --help | -h + Display this help and exit. + --version | -v + Display version and copyright information and exit. + +Commands: + flake + update the flake project + + runs a executable called "update-", if it exists +EOF +} + +main() { + if ! [ "$UPDATE_SCRIPT_NOT_ALLOWED" = true ]; then + update_script="$(check_for_update_script)"; + flake_base_dir="$(search_flake_base_dir)"; # Assume, that the update script is in the base dir + dbg "update_script is: $update_script"; + dbg "flake_base_dir is: $flake_base_dir"; + + if [ "$update_script" = "" ]; then + update "" "$flake_base_dir"; + elif check_for_allowed_update_script "$update_script" && ! [ "$update_script" = "" ]; then + update "$update_script" "$flake_base_dir"; + else + ask_to_allow_update_script "$update_script"; + main; + fi + fi +} + +if [ "$#" -eq 0 ]; then + main; +fi + +for input in "$@"; do + case "$input" in + "--help" | "-h") + help; + exit 0; + ;; + "--version" | "-v") + version; + exit 0; + ;; + "--") + end_of_cli_options=true; + ;; + esac + [ "$end_of_cli_options" = "true" ] && break +done + +while [ "$#" -ne 0 ]; do + case "$1" in + "flake") + main; + shift 1; + ;; + *) + command="$1"; + shift 1; + [ "$1" = "--" ] && shift 1 + if which update-"$command" > /dev/null 2>&1;then + if [ "$end_of_cli_options" = "true" ]; then + update-"$command" "$@"; + else + update-"$command"; + fi + else + die "command \"update-$command\" is not executable, or does not exist"; + fi + ;; + esac + [ "$end_of_cli_options" = "true" ] && break +done -- cgit 1.4.1