summarylogtreecommitdiffstats
path: root/uak
blob: 6bfddac7d1e354c58d13dd9cd1a8bdb67560b3cd (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash -e
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

export LC_COLLATE=C

source /usr/share/makepkg/util/message.sh

function check_root() {
    if [[ "$EUID" != 0 ]]; then
        error "Must be runned with superuser privilegies"
        exit 2
    fi
}

function dropindirs_sort() {
    local suffix=$1; shift
    local -a files
    local f d i

    readarray -t files <<<"$(
        for d in "$@"; do
            for i in "$d/"*"$suffix"; do
                if [[ -e "$i" ]]; then
                    echo "${i##*/}"
                fi
            done
        done | sort -Vu
    )"

    for f in "${files[@]}"; do
        for d in "$@"; do
            if [[ -e "$d/$f" ]]; then
                echo "$d/$f"
                continue 2
            fi
        done
    done
}

function die() {
    code="$1"
    shift
    echo "$*" >&2
    exit "$code"
}

function usage() {
    echo "Usage:"
    echo "        $0 install KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
    echo "        $0 remove KERNEL-VERSION"
    echo "        $0 regenerate"
}

for i in "$@"; do
    if [ "$i" == "--help" ] || [ "$i" == "-h" ]; then
        usage
        exit 0
    fi
done

if [[ -f /etc/os-release ]]; then
    OS_RELEASE_DEST=/etc/os-release
    source "$OS_RELEASE_DEST"
elif [[ -f /usr/lib/os-release ]]; then
    OS_RELEASE_DEST=/usr/lib/os-release
    source "$OS_RELEASE_DEST"
else
    warning "Missing os-release, things can go wrong AND THEY WILL GO WRONG"
fi

if [[ -f /etc/uak/config ]]; then
    source /etc/uak/config
fi

if [[ ! "$UAK_ESP_PATH" ]]; then
    UAK_ESP_PATH=$(bootctl --print-esp-path)
fi

if [[ ! -d "$UAK_ESP_PATH" ]]; then
    die 1 "Invalid ESP path: $UAK_ESP_PATH"
fi

if [[ ! "$UAK_SPLASH" ]]; then
    UAK_SPLASH=/usr/share/systemd/bootctl/splash-arch.bmp
fi

export UAK_ESP_PATH UAK_SPLASH

export UAK_ESP_TARGET="$UAK_ESP_PATH/EFI/Linux"

COMMAND="$1"

case "$COMMAND" in
    install)
        check_root

        KERNEL_VERSION="$2"
        KERNEL_IMAGE="$3"
        INITRDS=( "${@:4}" )

        if [[ ! $KERNEL_VERSION ]] || [[ ! "$KERNEL_IMAGE" ]]; then
            die 1 "Not enough arguments"
        fi

        if [[ ! -f "$KERNEL_IMAGE" ]]; then
            die 2 "Kernel image '${KERNEL_IMAGE}' not a file"
        fi

        shift 3

        EFISTUB_IMAGE=$(mktemp)
        EFISTUB_INSTALL_PATH="$UAK_ESP_TARGET/$ID-$KERNEL_VERSION.efi"
    ;;
    remove)
        check_root

        KERNEL_VERSION="$2"

        if [[ ! $KERNEL_VERSION ]]; then
            die 1 "Not enough arguments"
        fi

        shift 2

        EFISTUB_INSTALL_PATH="$UAK_ESP_TARGET/$ID-$KERNEL_VERSION.efi"
    ;;
    regenerate)
        check_root

        exec /usr/share/libalpm/scripts/uak-script install-all
    ;;
    '')
        usage
    ;;
    *)
        die 1 "Unknown command '$COMMAND'"
    ;;
esac

readarray -t PLUGINS <<<"$(
    dropindirs_sort ".install" \
        "/etc/uak/install.d" \
        "/usr/lib/uak/install.d"
)"

for plugin in "${PLUGINS[@]}"; do
    if [[ -x "$plugin" ]]; then
        source "$plugin"
    fi
done


if [[ "$COMMAND" == "install" ]]; then
    if [[ -f "$EFISTUB_IMAGE" ]]; then
        install -DT "$EFISTUB_IMAGE" "$EFISTUB_INSTALL_PATH" || warning "Unable to install image at $EFISTUB_INSTALL_PATH"
    else
        warning "No image was generated, skip install"
    fi
elif [[ "$COMMAND" == "remove" ]]; then
    if [[ -f "$EFISTUB_INSTALL_PATH" ]]; then
        unlink "$EFISTUB_INSTALL_PATH" || warning "Unable to remove image at '$EFISTUB_INSTALL_PATH'"
    fi
fi