summaryrefslogtreecommitdiff
path: root/src/usb.sh
blob: 55d8b488519cbcb7489db55b68fc804c89a4c448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash

die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; }

whole_device()
{
    case "$1" in
        *-part?) false ;;
        *-part??) false ;;
        *-part???) false ;;
        */usb\*) false ;;
        *) true ;;
    esac
}

confirm_usb()
{
    local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? "
    printf "$msg" "$1" >&2
    read line
    case "$line" in
        [yY][eE][sS]) return ;;
        *) die "Aborted by user." ;;
    esac
}

choose_usb()
{
    local devs maj
    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
        confirm_usb "$1" || die impossible
        USB=$(readlink -f "$1")
    else
        die "multiple USB devices connected and choice between them is unimplemented.  ($*)"
    fi
}

choose_cdrom()
{
    die 'choose_cdrom: unimplemented; specify cdrom device with --out'
}

choose_usb
echo $USB