-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfunctions.sh
109 lines (95 loc) · 2.75 KB
/
functions.sh
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
check_argument() {
if [ -z "$1" ]; then
echo "Error: Missing argument." >&2
return 1
fi
}
print_default() {
check_argument "$1" || return 1
# For printing gradient messages with auto-fallback.
if command -v lolcat &>/dev/null; then
echo -e "$1" | lolcat
else
echo -e "${BGray}$1"
fi
}
print_default2() {
# For printing gray messages.
echo -e "${BGray}$1"
}
print_success() {
check_argument "$1" || return 1
# For printing gradient success messages with auto-fallback.
if command -v lolcat &>/dev/null; then
echo -e "✓ Success. $1" | lolcat
else
echo -e "${BGray}✓ Success. $1"
fi
}
print_warning() {
check_argument "$1" || return 1
# For printing gradient warning messages with auto-fallback.
if command -v lolcat &>/dev/null; then
echo -e "⚠︎ Warning: $1" | lolcat >&2
else
echo -e "${BGray}⚠︎ Warning: $1" >&2
fi
}
print_error() {
check_argument "$1" || return 1
# For printing gradient error messages with auto-fallback.
if command -v lolcat &>/dev/null; then
echo -e "✗ Error. $1" | lolcat >&2
else
echo -e "${BRed}✗ Fail. $1" >&2
fi
}
rootcheck() {
if [[ $EUID -ne 0 ]]; then
print_warning "This script must be run as root. Restarting with sudo/su..."
if [[ -n "${SUDO_USER:-}" ]]; then
print_error "The script is already running with sudo privileges."
tput sgr0
exit 1
fi
if command -v sudo &> /dev/null; then
print_default2 "Using 'sudo' for automatic elevation."
tput sgr0
sudo -E bash "$0" "$@"
exit $?
elif command -v su &> /dev/null; then
print_default2 "Using 'su' for automatic elevation."
tput sgr0
script_path="$(readlink -f "$0")"
su -c "$script_path" -- "${@}"
exit $?
else
print_error "Neither sudo nor su were found. Exiting."
tput sgr0
exit 1
fi
fi
}
accidental_start_prevention() {
print_default "Accidental start prevention: press 'enter' within 5 seconds to continue, CTRL+C to cancel."
for _ in {1..5}; do
stty -echo
if read -rt 5 -N 1 key; then
stty echo
case $key in
$'\n') return 0 ;;
$'\x03') print_default2 "Ctrl+C detected. Exiting..." && exit 130 ;;
*) print_error "Invalid input. Try again." ;;
esac
else
stty echo
print_default2 "No input detected in 5 seconds. Exiting..." && exit 1
fi
done
print_warning "Maximum attempts reached. Exiting..." && exit 1
}
cleanup() {
print_default "Received CTRL+C. Exiting..."
tput sgr0
exit 130
}