summarylogtreecommitdiffstats
path: root/calibre-alternatives.sh
blob: a2327ff046bd0efeb284b62c7e2c9c9b85c4bd79 (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
#!/bin/sh

libdir=/usr/lib/calibre
bindir=/usr/lib/calibre/bin

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

usage() {
    cat <<- __EOF__
	usage: calibre-alternatives [help|get]
	usage: calibre-alternatives set [2|3]

	Set the default python interpreter for calibre, either python2 or python3.
__EOF__
}

get_current() {
    currentdir=$(readlink "${libdir}/bin")
    echo "${currentdir##*-py}"
}

find_alternative() {
    for i in 2 3; do
        if [ -d "${libdir}/bin-py${i}" ]; then
            echo "${i}"
            return 0
        fi
    done
    return 1
}

set_current() {
    alt=${1}
    if [ -z "${alt}" ]; then
        [ -z "$(get_current)" ] || exit 0 # nothing to do
        alt=$(find_alternative) || die "error: no alternatives exist"
    fi

    if ! [ $(id -u) = 0 ]; then
        die "error: cannot set without being root"
    fi

    if [ -d "${libdir}/bin-py${alt}" ]; then
        ln -snvf bin-py${alt} "${libdir}/bin"
    else
        die "alternative '${alt}' does not exist"
    fi
}

case ${1} in
    help)
        usage;;
    ''|get)
        get_current;;
    set)
        shift; set_current "${1}";;
    *)
        die "invalid option: '${1}'"
esac