From 027ab59aabee2e034fc41ac5dd8729426418f343 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Tue, 9 May 2023 14:02:32 +0200 Subject: [PATCH] bin: improve scripts --- .local/bin/scripts/dupcheck | 4 ++-- .local/bin/scripts/duploc | 27 ++++++++++++++++++--------- .local/bin/scripts/nas | 3 +++ .local/bin/scripts/save-power | 3 ++- .local/bin/scripts/sign | 14 +++++++++----- .local/bin/scripts/topng | 2 ++ .local/bin/scripts/update-e2b | 24 +++++++++++++++++++----- 7 files changed, 55 insertions(+), 22 deletions(-) create mode 100755 .local/bin/scripts/nas create mode 100755 .local/bin/scripts/topng diff --git a/.local/bin/scripts/dupcheck b/.local/bin/scripts/dupcheck index 64bd8fd..42fa4d2 100755 --- a/.local/bin/scripts/dupcheck +++ b/.local/bin/scripts/dupcheck @@ -2,8 +2,8 @@ # Check the first file against all other ones given and print duplicates. # Checks first size then diff. # TODO diff initial bytes -if ! test $# -lt 2 -then echo "Need at least 2 files to compare!" 2>/dev/null +if test $# -lt 2 +then echo "Need at least 2 files to compare!" >&2 exit 2 fi target="$1" diff --git a/.local/bin/scripts/duploc b/.local/bin/scripts/duploc index 0014d3b..efbf12a 100755 --- a/.local/bin/scripts/duploc +++ b/.local/bin/scripts/duploc @@ -3,13 +3,22 @@ # which exist elsewhere as listed by locate. # Args: [threshold (MB)] # OptDepends: synct (for checking against original filename) -case $1 in ([0-9]|[0-9][0-9]) threshold=$1; shift;; esac -find "$@" -size +${threshold:-50}M -type f -exec sh -c "IFS=$'\n'"' - filepath="{}" - target="$(synct-unarchive "$filepath" || echo "$filepath")" - highlight "$filepath" - for existing in $(locate -b "$target") - do test -n "$(dupcheck "$target" "$existing") && - echo "Duplicate found at $existing" - rm -vi "$filepath" && break +while test $# -gt 0; do + case "$1" in + ([0-9]|[0-9][0-9]) threshold=$1;; + (-n) noscan=$1;; + (-v) verbose=$1; set -x;; + (*) break;; + esac + shift +done +test "$noscan" || "Updating Locate Database..." && sudo updatedb +find "$@" -size +${threshold:-50}M -type f -exec sh -c "IFS=$'\n'; verbose='$verbose'"' + target="$(synct-unarchive "{}" || echo "{}")" + highlight "{}" + for existing in $(locate -b "$(basename "$target")") + do test "$verbose" && echo "Checking $existing against {}" + test -n "$(dupcheck "{}" "$existing")" && + echo "Duplicate found at $existing" && + rm -vi "{}" && break done' \; diff --git a/.local/bin/scripts/nas b/.local/bin/scripts/nas new file mode 100755 index 0000000..166b9d3 --- /dev/null +++ b/.local/bin/scripts/nas @@ -0,0 +1,3 @@ +#!/bin/sh -e +cd /mnt/nas +find -mindepth 1 -maxdepth 1 -exec mount {} \; diff --git a/.local/bin/scripts/save-power b/.local/bin/scripts/save-power index 2d32ec0..72ef04b 100755 --- a/.local/bin/scripts/save-power +++ b/.local/bin/scripts/save-power @@ -5,4 +5,5 @@ pkill --echo --ignore-case discord pkill --echo signal pkill --echo aw-server pkill --echo aw-qt -systemctl stop --user syncthing plasma-baloorunner kde-baloo openvpn-client@deltaPeak.service +systemctl stop --user syncthing plasma-baloorunner kde-baloo +systemctl stop openvpn-client@deltaPeak.service diff --git a/.local/bin/scripts/sign b/.local/bin/scripts/sign index 814f360..cb7694e 100755 --- a/.local/bin/scripts/sign +++ b/.local/bin/scripts/sign @@ -1,6 +1,7 @@ #!/bin/sh -ex test "$1" = "-q" && quiet=$1 && shift -test ! -r "$1" && echo "Usage: sign [hoffset (-160) [voffset (-310) [scale [signature-image]]]]" && exit 1 +case "$1" in ([0-9]) page=$1; shift;; esac +test ! -r "$1" && echo "Usage: sign [page] [hoffset (-160) [voffset (-310) [scale [signature-image]]]]" && exit 1 signature=$(pass info/signature$(test -n "$5" && echo "-$5") || echo "$5") @@ -22,15 +23,18 @@ fi export TEXMF="" pdfjam $quiet "$signature" --outfile "$sig" --papersize "{595pt, 842pt}" --noautoscale true \ --offset "${h}pt ${v}pt" --scale "${4:-1}" -pdfjam $quiet "$1" last "$sig" --outfile "$tmp_signed" --delta "0 -842pt" --nup "1x2" --fitpaper true +pdfjam $quiet "$1" ${page:-last} "$sig" --outfile "$tmp_signed" --delta "0 -842pt" --nup "1x2" --fitpaper true # TODO consider pdftk stamp timg "$tmp_signed" # https://apple.stackexchange.com/questions/198854/shell-command-to-count-pages-in-a-pdf-other-than-pdftk -if test "$(pdftk "$1" dump_data | grep Pages | cut -d' ' -f2)" -gt 1 - then +if test "$page" +then pdfjam $quiet "$1" 1-$(expr $page - 1) "$tmp_signed" 1 "$1" $(expr $page + 1)-last --outfile "$result" +elif test "$(pdftk "$1" dump_data | grep Pages | cut -d' ' -f2)" -gt 1 +then pdfjam $quiet "$1" last-1 --outfile "$tmp_reversed" pdfjam $quiet "$tmp_reversed" last-2 "$tmp_signed" --outfile "$result" - else cp "$tmp_signed" "$result" +else cp "$tmp_signed" "$result" fi + diff --git a/.local/bin/scripts/topng b/.local/bin/scripts/topng new file mode 100755 index 0000000..50a26da --- /dev/null +++ b/.local/bin/scripts/topng @@ -0,0 +1,2 @@ +#!/bin/sh +convert -background white -density 500 "$@" "$1.png" diff --git a/.local/bin/scripts/update-e2b b/.local/bin/scripts/update-e2b index 0e7002a..238743f 100755 --- a/.local/bin/scripts/update-e2b +++ b/.local/bin/scripts/update-e2b @@ -2,18 +2,32 @@ # Update an Easy2Boot USB Stick after adding ISOs # Call with Linux ISOs to be copied as args # Need to be around E2B mountpoint if it is not the default -if ! dir="$(find . "${MNT:-${XDG_RUNTIME_DIR}/mnt}" -maxdepth 3 -name _ISO -type d | grep .)" +if ! dir="$(find . "${MNT:-${XDG_RUNTIME_DIR}/mnt}" -maxdepth 3 -name _ISO -type d | grep --max-count 1 .)" then echo "Please mount your multiboot stick first!" exit 1 fi -cmd=mv +cmd=cp case $1 in + (-upgrade) upgrade=$2; shift 2;; (-cp) cmd=cp; shift;; + (-mv) cmd=mv; shift;; esac test $# -eq 0 || $cmd -vi "$@" "$dir/LINUX" find "$dir" -type f -iname "*32-*.iso" -exec mv -vi {} {}def32 \; find "$dir" -type f -name "*.iso" -exec mv -vi {} {}def64 \; -drive=$(mount | grep "$(dirname "$dir")" | cut -d' ' -f1) -sudo umount $drive -sudo udefrag $drive +mountdir="$(dirname "$dir")" +drive=$(mount | grep "$mountdir" | cut -d' ' -f1) +sudo umount "$drive" +if test "$upgrade"; then + echo "Do you want to reformat $drive as NTFS? (Ctrl-C to abort)" + read + mkfs -t ntfs "$drive" + ntfslabel "$drive" "E2B" + sudo mount "$drive" "$mountdir" + unzip "$upgrade" -d "$mountdir" + touch -v "$dir/nodelay.txt" + cp -v /mnt/nas/media/iso/utilities/*.iso "$dir/UTILITIES" +else + sudo udefrag -m "$drive" +fi