summaryrefslogtreecommitdiff
path: root/dot/local/bin/choose-printer
blob: 1208045c5e736a4975d645b66dce62f33c356928 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash

die()
{
    echo "Error: $*" >&2
    exit 1
}

show_default()
{
    d=$(lpstat -d)
    case "$d" in
        'system default destination: '*) echo "${d##* }" ;;
    esac

}

set_default()
{
    (
        set -x
        lpoptions -d "$1"
    )
}

printers()
{
    lpstat -p | while read line
    do
        case "$line" in
            "printer "*)
                p="${line#printer }"
                echo "${p%% *}"
                set -- "$p" "$line"
                ;;
        esac
    done
}

show_printers_with_default()
{
    default_printer=$(show_default) # can be empty
    for p
    do
        if [ "$p" = "$default_printer" ]
        then
            echo "[default] $p"
        else
            echo "          $p"
        fi
    done | nl
}

set -e
set -- $(printers)

echo
show_printers_with_default "$@"
echo

read -p 'Choose default printer> ' i || die "Error reading input"
[ "$i" ] || exit 0
[ "$i" -gt 0 ] || die "Invalid choice: $i"
choice=${!i} && [ "$choice" ] || die "Invalid choice: $i"

set_default $choice