summaryrefslogtreecommitdiff
path: root/install-packages.bash
blob: 0402dfaa403ab28897d5b3d4c0f53fc1f14690af (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -e

basics='etckeeper git par ssh tmux vim w3m debhelper'
docs='info bash-doc make-doc'
xorg='xorg xterm x11vnc xserver-xephyr xtightvncviewer xdotool'
media='libdbus-glib-1-2 mpv pulseaudio imagemagick'

main()
{
    [ "$(id -u)" = 0 ] || exec sudo bash "$0" "$@"

    apt install -q --show-progress --no-upgrade $basics $docs $xorg $media
    apt_backport /usr/local/src https://salsa.debian.org/i3-team/i3-wm.git i3-wm
}

apt()
{
        (set -x; command apt "$@")
}

all_files_exist()
{
    for f in "$@"
    do
        [ -e "$f" ] || return
    done
    true
}

# Clone into temporary directory then move to final dest -- to preserve
# idempotence in case of git failure.
git_clone()
{
    local src dst tmpdst
    local src="$1"
    local dst="$(realpath "$2")" || return
    local tmpdst="$(mktemp -d "$dst"~tmp~XXXXXX)" || return

    if git clone --depth=1 -- "$src" "$tmpdst"
    then
        mv -T -- "$tmpdst" "$dst"
    else
        r=$?
        rm -rf -- "$tmpdst"
        return $r
    fi
}

apt_backport()
{
    (
        set -e
        SOURCES_DIR=$1
        TARGET_URL=$2
        TARGET_PKG=$3
        case "$TARGET_PKG" in
                '' | */*) exit 1 ;;
        esac
        mkdir -p "$SOURCES_DIR"
        cd "$SOURCES_DIR"

        if [ -e "$TARGET_PKG" ]
        then
                cd "$TARGET_PKG"
                git pull --ff-only
        else
                git_clone "$TARGET_URL" "$TARGET_PKG"
                cd "$TARGET_PKG"
        fi

        arch=$(dpkg-architecture -q DEB_BUILD_ARCH)
        version=$(dpkg-parsechangelog --show-field Version)
        pkgs=$(dh_listpackages)

        debs=
        for pkg in $pkgs
        do
                debs="$debs ../${pkg}_${version}_${arch}.deb"
        done

        if ! all_files_exist $debs
        then
                build_deps=$(get_build_deps)
                if [ "$build_deps" ]
                then
                        apt install --mark-auto $build_deps
                fi
                fakeroot ./debian/rules binary
        fi

        apt install $debs
    )
}

get_build_deps()
{
        # credit: Archemar @ https://unix.stackexchange.com/a/648927
        dpkg-checkbuilddeps 2>&1 |
                sed -n -e 's/dpkg-checkbuilddeps: error: Unmet build dependencies: //gp' \
                       -e 's/([^)]*) //g'
}

main "$@"