From 128fa6834d422bcf195436468927e57f332fd97d Mon Sep 17 00:00:00 2001 From: Gordon GECOS Date: Sun, 1 Nov 2020 17:24:33 -0500 Subject: rename usb.sh -> usb --- Makefile | 2 +- src/parted-usb.sh | 4 ++- src/usb | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/usb.sh | 90 ------------------------------------------------------- 4 files changed, 94 insertions(+), 92 deletions(-) create mode 100755 src/usb delete mode 100755 src/usb.sh diff --git a/Makefile b/Makefile index 453b315..e63d940 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ src_bin_programs = xorriso-usb.sh btrfs-functions.sh btrfs-receive-root.sh btrfs-send-root.sh var.sh grub-efi.sh keygen.sh initrd.sh qemu.sh \ dnsmasq-dhcp-script.sh samizdat-password-agent samizdat-gpg-agent publish-ip.sh \ selfstrap samizdat-daily-snapshot-root samizdat-diff-root kiki-export-stdout \ -kiki-import-stdin store-child-permanently git-ll-remote \ +kiki-import-stdin store-child-permanently git-ll-remote usb \ hostname.cryptonomic.net ${dyndns_progs} bin_programs=$(addprefix src/, $(src_bin_programs)) samizdat-paths.sh ${cc_files} ${btrfs_utils} diff --git a/src/parted-usb.sh b/src/parted-usb.sh index 05b776c..f8855d3 100755 --- a/src/parted-usb.sh +++ b/src/parted-usb.sh @@ -353,12 +353,14 @@ fi default_target=boot-disk.img +USB=$(which usb || echo ./src/usb) + if [ "$1" ] then if [ "$1" = USB ] then [ "$DO_BUILD" ] && arg= || arg=-n - target=$(./src/usb.sh $arg) || exit 1 + target=$($USB $arg) || exit 1 elif [ -f "$1" ] then case "$1" in diff --git a/src/usb b/src/usb new file mode 100755 index 0000000..a8fd5fb --- /dev/null +++ b/src/usb @@ -0,0 +1,90 @@ +#!/bin/bash + +die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } + +ECHO() { printf "%s\n" "$*" >&2; } + +PRINTF() { printf "$@" >&2; } + +whole_device() +{ + case "$1" in + *-part?) false ;; + *-part??) false ;; + *-part???) false ;; + */usb\*) false ;; + *) true ;; + esac +} + +confirm_usb_strict() +{ + CONFIRM_MSG="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? " + CONFIRM_WITH_ONE_LETTER= + confirm_helper "$@" +} + +confirm_usb_lax() +{ + CONFIRM_MSG="The device used will be:\n\n\t%s\n\nType 'y' to confirm.\nContinue? " + CONFIRM_WITH_ONE_LETTER=y + confirm_helper "$@" +} + +confirm_helper() +{ + PRINTF "$CONFIRM_MSG" "$1" + read line + case "$line" in + [yY][eE][sS]) return ;; + [yY]) [ "$CONFIRM_WITH_ONE_LETTER" ] && return || : ;; + esac + die "Aborted by user." +} + +choose_usb() +{ + local devs maj USB + set -- /dev/disk/by-id/usb* + for dev; do + shift + whole_device "$dev" || continue + set -- "$@" "$dev" + done + if [ $# = 0 ]; then + die "no usb device found" + elif [ $# = 1 ]; then + [ "$NO_ACT" ] && confirm=confirm_usb_lax || confirm=confirm_usb_strict + $confirm "$1" || exit + USB=$1 + else + ECHO + PRINTF "%s\n" "The following candidate target devices have been found:" + i=0 + for dev; do + i=$((i+1)) + PRINTF "\n\n\t%2i. %s\n" $i "$dev" + done + PRINTF "%s\n\n" '' "Choose a device by its number, from 1 to $#, then press enter." "To abort, press CTRL-C." + PRINTF "\n%s" 'Your choice: ' + read line + case "$line" in + [1-9]|[1-9][0-9]) + [ $line -le $# -a $line -ge 1 ] || die "Invalid argument (out of range): $line" + USB=${!line} + ;; + *) die "Invalid argument: $line" ;; + esac + confirm_usb_lax + fi + [ "$USB" ] && readlink -f "$USB" +} + +NO_ACT= +case "$1" in + -n) NO_ACT=y;; + -*) exit 1;; +esac + +choose_usb + diff --git a/src/usb.sh b/src/usb.sh deleted file mode 100755 index a8fd5fb..0000000 --- a/src/usb.sh +++ /dev/null @@ -1,90 +0,0 @@ -#!/bin/bash - -die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } - -ECHO() { printf "%s\n" "$*" >&2; } - -PRINTF() { printf "$@" >&2; } - -whole_device() -{ - case "$1" in - *-part?) false ;; - *-part??) false ;; - *-part???) false ;; - */usb\*) false ;; - *) true ;; - esac -} - -confirm_usb_strict() -{ - CONFIRM_MSG="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? " - CONFIRM_WITH_ONE_LETTER= - confirm_helper "$@" -} - -confirm_usb_lax() -{ - CONFIRM_MSG="The device used will be:\n\n\t%s\n\nType 'y' to confirm.\nContinue? " - CONFIRM_WITH_ONE_LETTER=y - confirm_helper "$@" -} - -confirm_helper() -{ - PRINTF "$CONFIRM_MSG" "$1" - read line - case "$line" in - [yY][eE][sS]) return ;; - [yY]) [ "$CONFIRM_WITH_ONE_LETTER" ] && return || : ;; - esac - die "Aborted by user." -} - -choose_usb() -{ - local devs maj USB - set -- /dev/disk/by-id/usb* - for dev; do - shift - whole_device "$dev" || continue - set -- "$@" "$dev" - done - if [ $# = 0 ]; then - die "no usb device found" - elif [ $# = 1 ]; then - [ "$NO_ACT" ] && confirm=confirm_usb_lax || confirm=confirm_usb_strict - $confirm "$1" || exit - USB=$1 - else - ECHO - PRINTF "%s\n" "The following candidate target devices have been found:" - i=0 - for dev; do - i=$((i+1)) - PRINTF "\n\n\t%2i. %s\n" $i "$dev" - done - PRINTF "%s\n\n" '' "Choose a device by its number, from 1 to $#, then press enter." "To abort, press CTRL-C." - PRINTF "\n%s" 'Your choice: ' - read line - case "$line" in - [1-9]|[1-9][0-9]) - [ $line -le $# -a $line -ge 1 ] || die "Invalid argument (out of range): $line" - USB=${!line} - ;; - *) die "Invalid argument: $line" ;; - esac - confirm_usb_lax - fi - [ "$USB" ] && readlink -f "$USB" -} - -NO_ACT= -case "$1" in - -n) NO_ACT=y;; - -*) exit 1;; -esac - -choose_usb - -- cgit v1.2.3