This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
mhwd-kernel
executable file
·108 lines (89 loc) · 4.01 KB
/
mhwd-kernel
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
#!/bin/bash
root_check() {
[[ $EUID != 0 ]] && err "Please run as root."
}
args_check() {
[[ $1 -lt $2 || $1 -gt $3 ]] && err "Please use the right amount of arguments (use -h for help)."
}
err() {
printf "\e[31mError:\e[0m $1\n" 1>&2; exit 1
}
kernel_usage() {
echo "Usage: mhwd-kernel [option]
-h --help Show this help message
-i --install Install a new kernel [kernel(s)] [optional: rmc = remove current kernel]
-l --list List all available kernels
-li --listinstalled List installed kernels
-r --remove Remove a kernel [kernel(s)]"
}
kernel_install() {
pkginstall=()
rmc=0
for kernel in "$@"; do
[[ $kernel = "rmc" ]] && rmc=1 && continue
[[ $kernel != linux[0-9][0-9]?([0-9]) && $kernel != linux[0-9][0-9]?([0-9])-rt && $kernel != "rmc" ]] && err "Invalid argument.\nPlease choose one of the $(kernel_repo)"
[[ $current = $kernel ]] && err "You can't reinstall your current kernel. Please use 'pacman -Syu' instead to update."
[[ -z $(pacman -Ssq "^$kernel$") ]] && err "Please make sure if the given kernel(s) exist(s).\n$(kernel_repo)"
for pkg in $(pacman -Qqs "$current"); do
pkg=${pkg//$current/$kernel}
[[ -n $(pacman -Ssq "^$pkg$" | grep -E "^$pkg$|^$pkg-\S+$") ]] && pkginstall+=("$pkg")
done
done
pacman -Syy
outofdate="$(pacman -Qqu | tr '\n' ' ')"
if [[ -n $outofdate ]]; then
echo "The following packages are out of date, please update your system first: $outofdate" >&2
read -p "Do you want to continue anyway? [y/N] " yesno
[[ $yesno = [Yy]?([Ee][Ss]) ]] || exit 1
fi
pacman -S "${pkginstall[@]}"
[[ $rmc = 1 && $? = 0 ]] && pacman -R $(pacman -Qqs $current)
[[ $rmc = 1 && $? != 0 ]] && { echo ""; err "'rmc' aborted because the kernel failed to install or canceled on removal."; }
}
kernel_repo() {
printf "\e[32mavailable kernels:\e[0m\n"
pacman -Ssq "^linux[0-9][0-9]?([0-9])$" | while read -r; do echo " * $REPLY"; done
pacman -Ssq "^linux[0-9][0-9]?([0-9])-rt$" | while read -r; do echo " * $REPLY"; done
}
kernel_list() {
printf "\e[32mCurrently running:\e[0m $(uname -r) (${current})\n"
echo "The following kernels are installed in your system:"
pacman -Qqs "^linux[0-9][0-9]?([0-9])$" | while read -r; do echo " * $REPLY"; done
pacman -Qqs "^linux[0-9][0-9]?([0-9])-rt$" | while read -r; do echo " * $REPLY"; done
}
kernel_remove() {
pkgremove=()
for kernel in "$@"; do
[[ -z "$kernel" ]] && err "Invalid argument (use -h for help)."
[[ $kernel != linux[0-9][0-9]?([0-9]) && $kernel != linux[0-9][0-9]?([0-9])-rt ]] && err "Please enter a valid kernel name.\n$(kernel_list)"
[[ $current = $kernel ]] && err "You can't remove your current kernel."
[[ -z $(pacman -Qqs "^$1$") ]] && err "Kernel not installed.\n$(kernel_list)"
for pkg in $(pacman -Qqs "$kernel" | grep -E "^$kernel$|^$kernel-\S+$"); do
pkgremove+=("$pkg")
done
done
pacman -R "${pkgremove[@]}"
}
IFS=. read -r major minor _ <<< "$(uname -r)"
current="linux$major$minor"
case "$1" in
-h | --help) args_check $# 1 1
kernel_usage
exit 0;;
-i | --install) shift
root_check
kernel_install $@
exit 0 ;;
-l | --list) args_check $# 1 1
kernel_repo
exit 0 ;;
-li| --listinstalled) args_check $# 1 1
kernel_list
exit 0 ;;
-r | --remove) shift
root_check
kernel_remove $@
exit 0 ;;
-*) err "Invalid argument (use -h for help)." ;;
*) err "No arguments given (use -h for help)." ;;
esac