summarylogtreecommitdiffstats
path: root/grub-theme-vimix-very-dark-blue.install
blob: 5965825203a73f9e0998af4f3b6fb2cee5e6da7e (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
#!/bin/sh

# Generate or update the GRUB configuration file with superuser privileges, and
# provide setup instructions, if this theme is newly installed. Additionally,
# warn the user if GRUB continues to refer to this theme after it has been
# uninstalled.
#
# Usage:
#     post_install:
#         Generate the GRUB configuration file with superuser privileges if this
#         theme is in use. If this theme is newly installed, provide setup
#         instructions to the user.
#
#         Usage: post_install
#
#     post_upgrade:
#         Generate the GRUB configuration file with superuser privileges if this
#         theme is in use.
#
#         Usage: post_upgrade
#
#     post_remove:
#         Warn the user if GRUB still uses this theme after uninstalling it.
#
#         Usage: post_remove
#
# Dependencies:
#     This script depends on the following commands: `grep`, `grub-mkconfig`,
#     `printf`, `sudo`.
#
# Note:
#     This script assumes that the theme is located at
#     `/usr/share/grub/themes/grub-theme-vimix-very-dark-blue`.

readonly GRUB_THEME_CFG_PATH="/boot/grub/grub.cfg"
readonly GRUB_THEME_CONFIG_LINE='GRUB_THEME="/usr/share/grub/themes/grub-theme-vimix-very-dark-blue/theme.txt"'
readonly GRUB_THEME_DEFAULT_PATH="/etc/default/grub"
readonly GRUB_THEME_INSTALL_PATH="/usr/share/grub/themes/grub-theme-vimix-very-dark-blue"

readonly GRUB_THEME_SEARCH_PATTERN="^$GRUB_THEME_CONFIG_LINE"

# Generate the GRUB configuration file with superuser privileges if this theme
# is in use. If this theme is newly installed, provide setup instructions to the
# user.
#
# Usage: post_install
post_install() {
    _grub_mkconfig_smart

    if ! _is_use_theme; then
        _status \
            "This GRUB theme is currently installed, but not being used." \
            ""
            "To apply this theme, update the 'GRUB_THEME' line in the '$GRUB_THEME_DEFAULT_PATH' file to the following:" \
            "$GRUB_THEME_CONFIG_LINE" \
            "" \
            "Then, generate the GRBU configuration file with superuser privileges by running the following command:" \
            "sudo grub-mkconfig -o '$GRUB_THEME_CFG_PATH'" \
            "" \
            "The system will start using the new theme after the next reboot."
    fi
}

# Generate the GRUB configuration file with superuser privileges if this theme
# is in use.
#
# Usage: post_upgrade
post_upgrade() {
    _grub_mkconfig_smart
}

# Warn the user if GRUB continues to refer to this theme after it has been
# uninstalled.
#
# Usage: post_remove
post_remove() {
    if _is_use_theme_grub_cfg; then
        _status_error "The current GRUB configuration file '$GRUB_THEME_CFG_PATH' still references this theme in the following line(s):"
        _grep_theme_in_grub_cfg
        _status_error "Please remove any reference to this theme from the current GRUB configuration file as soon as possible. It is recommended to do this before rebooting!"
    fi

    if _is_use_theme_default_grub; then
        _status_error "The default GRUB configuration file '$GRUB_THEME_DEFAULT_PATH' still references this theme in the following line(s):"
        _grep_theme_in_default_grub
        _status_error "Please either replace this theme with another one, or disable custom GRUB themes by commenting out this line."
    fi
}

# Print lines from the default GRUB configuration file that match this theme.
#
# Usage: _grep_theme_in_default_grub
_grep_theme_in_default_grub() {
    grep -P -n "$GRUB_THEME_SEARCH_PATTERN" "$GRUB_THEME_DEFAULT_PATH"
}

# Print lines from the current GRUB configuration file that match this theme.
#
# Usage: _grep_theme_in_grub_cfg
_grep_theme_in_grub_cfg() {
    grep -F -n "$GRUB_THEME_INSTALL_PATH" "$GRUB_THEME_CFG_PATH"
}

# Generate the GRUB configuration file with superuser privileges.
#
# Usage: _grub_mkconfig
_grub_mkconfig() {
    sudo grub-mkconfig -o "$GRUB_THEME_CFG_PATH"
}

# Generate the GRUB configuration file with superuser privileges if this theme
# is in use.
#
# Usage: _grub_mkconfig_smart
_grub_mkconfig_smart() {
    if _is_use_theme; then
        _status \
            "Generating the GRUB configuration file with superuser privileges by running the following command:" \
            "sudo grub-mkconfig -o $GRUB_THEME_CFG_PATH"
        _grub_mkconfig
    fi
}

# Return true if the default GRUB theme uses this theme, otherwise return false.
#
# Usage: _is_use_theme_default_grub
_is_use_theme_default_grub() {
    _grep_theme_in_default_grub >/dev/null
}

# Return true if the current GRUB theme uses this theme, otherwise return false.
#
# Usage: _is_use_theme_grub_cfg
_is_use_theme_grub_cfg() {
    _grep_theme_in_grub_cfg >/dev/null
}

# Return true if both the default GRUB configuration file and the current GRUB
# configuration file use this theme. Otherwise return false.
#
# Usage: _is_use_theme
_is_use_theme() {
    _is_use_theme_default_grub && _is_use_theme_grub_cfg
}

# Print each MESSAGE on a new line with the `grub-theme-vimix-very-dark-blue: `
# prefix.
#
# Usage: status [MESSAGE ...]
_status() {
    printf 'grub-theme-vimix-very-dark-blue: %s\n' "$@"
}

# Print each MESSAGE on a new line with the `grub-theme-vimix-very-dark-blue: `
# prefix to stderr.
#
# Usage: status_error [MESSAGE ...]
_status_error() {
    _status "$@" >&2
}