This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathcheck_functions
executable file
·215 lines (207 loc) · 6.81 KB
/
check_functions
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# Check dpkg for package installation status
function check_package_installed() {
# query dpkg for install status and return a value
dpkg-query -W --showformat='${Status}\n' $@ | grep "install ok installed" &> /dev/null; echo $?
}
# Check if single package is installed
# ${1} = package, ${2} = return_function
function check_package() {
# echo_message header "Starting 'check_package' function"
# if package is not installed
if [ $(check_package_installed ${1}) != 0 ]; then
# draw window
if (whiptail \
--title "Install ${1}" \
--yesno "This function requires '${1}' but it is not present on your system. \n\nWould you like to install it to continue? " 10 64) then
# Install
echo_message info "Installing '${3}'..."
superuser_do "apt install -y ${1}"
# Finished
echo_message success "${1} is installation complete."
whiptail --title "Finished" --msgbox "Installation of ${1} Flatpak complete." 8 56
${2}
else
# Cancelled
echo_message info "Installation of ${1} cancelled."
${2}
fi
else
echo_message info "Function dependency '${1}' is installed."
fi
}
# Check for Flatpak repository
# ${1} = remote, ${2} = https://remote.example.com/, ${3} = return_function
function check_flatpak_repo {
# echo_message header "Starting 'check_flatpak_repo' function"
# If repo not is added
if [ $(flatpak remotes | grep ${1} &> /dev/null; echo $?) = 1 ]; then
# draw window
if (whiptail \
--title "Add ${1} Repository" \
--yesno "This function requires '${1^}' repository but it is not present on your system. \n\nWould you like to add it to continue? " 10 64) then
# Add repository
echo_message info "Adding flatpak repository..."
flatpak remote-add --if-not-exists ${1} ${2}
echo_message success "Repository added."
whiptail --title "Finished" --msgbox "The ${1^} repository has been added." 8 56
else
# Cancelled
echo_message info "Addition of ${1^} repository cancelled."
${3}
fi
else
echo_message info "${1^} repository already added."
fi
}
# Check which distribution the user is running
function check_os {
echo_message header "Starting 'check_os' function"
echo_message title "Checking which OS you are using..."
# Variables
OS_NAME="Linux"
# Check if Linux
echo_message info "Current OS is: "$(uname)
if [[ $(uname) != "$OS_NAME" ]]; then
echo_message error "You don't appear to be using $OS_NAME! Aborting. :("
exit 99
else
echo_message success "You are using '$OS_NAME'. :D"
fi
}
# Check which distribution the user is running
function check_distribution {
# check if 'lsb_release' exists
if [[ $(which lsb_release &>/dev/null; echo $?) != 0 ]]; then
echo_message error "\aCan't check which distribution you are using! Aborting."
echo_message error " Aborting..." && sleep 3 && exit 99
else
# if Ubuntu
if lsb_release -ds | grep -qE '(Ubuntu)'; then
echo 'Current distribution is: '$(lsb_release -ds)
echo_message success "You are using Ubuntu. :D"
echo "Proceeding."
# if Mint or elementary
elif lsb_release -ds | grep -qE '(Mint|elementary)'; then
echo 'Current distribution is: '$(lsb_release -ds)
echo_message success "You are using an Ubuntu-based distribution. It's probably fine. :)"
echo "Proceeding."
# if Debian
elif lsb_release -ds | grep -q 'Debian'; then
echo 'Current distribution is: '$(lsb_release -ds)
echo_message warning "You are using Debian. This is not recommended. Some functions may not work. :/"
echo "Proceeding nonetheless."
# if anything else
else
echo_message warning "You are using a distribution that may not be compatible with this script set."
echo_message warning "Proceeding may break your system."
echo_message question "Are you sure you want to continue? (Y)es, (N)o : " && read REPLY
case $REPLY in
# Positive action
[Yy]* )
echo_message warning "You have been warned."
;;
# Negative action
[Nn]* )
echo_message info "Exiting..."
exit 99
;;
# Error
* )
echo_message error 'Sorry, try again.' && check_distribution
;;
esac
fi
fi
}
# Check for and install if missing the required packages for this script set.
function check_dependencies {
echo_message header "Starting 'check_dependencies' function"
echo_message title "Checking if necessary dependencies are installed..."
# Variables
LIST=$(dirname "$0")'/data/dependencies.list'
# Check dependencies
for PACKAGE in $(cat $LIST); do
# If package is not installed
if [ $(check_package_installed $PACKAGE) != 0 ]; then
echo_message info "This script requires '$PACKAGE' and it is not present on your system."
echo_message question 'Would you like to install it to continue? (Y)es, (N)o : ' && read REPLY
case $REPLY in
# Positive action
[Yy]* )
echo_message warning "Requires root privileges"
sudo apt install -y $PACKAGE
echo_message success "Package '$PACKAGE' installed."
;;
# Negative action
[Nn]* )
echo_message info "Exiting..."
exit 99
;;
# Error
* )
echo_message error 'Sorry, try again.' && check_dependencies
;;
esac
else
echo_message info "Dependency '$PACKAGE' is installed."
fi
done
echo_message success "All dependencies are installed. :)"
}
# Check if current user is in the sudo group
function check_privileges {
echo_message header "Starting 'check_privileges' function"
echo_message title "Checking administrative privileges of current user..."
# Check if user is root
if [[ $EUID != 0 ]]; then
if [[ $(groups $USER | grep -q 'sudo'; echo $?) != 0 ]]; then
echo_message error "This user account doesn't have admin privileges."
echo_message info "Log in as a user with admin privileges to be able to much of these scripts.."
echo_message info "Exiting..."
sleep 5 && exit 99
else
# Current user can use 'sudo'
echo_message success "Current user has sudo privileges. :)"
fi
else
# if dependency whiptail is installed
if command -v whiptail 2>&1 >/dev/null; then
# draw window
if (whiptail --title "Root User" --yesno "You are logged in as the root user. This is not recommended.\n\nAre you sure you want to proceed?" 12 56) then
echo_message warning "You are logged in as the root user. This is not recommended. :/"
else
echo_message info "Exiting..."
exit 99
fi
else
# text-based warning
echo_message warning "You are logged in as the root user. This is not recommended. :/"
read -p "Are you sure you want to proceed? [y/N] " REPLY
REPLY=${REPLY:-n}
case $REPLY in
[Yy]* )
echo_message info "Proceeding..."
;;
[Nn]* )
echo_message info "Exiting..."
exit 99
;;
* )
echo_message error 'Sorry, try again.' && check_privileges
;;
esac
fi
fi
}
# Run system checks
function system_checks {
# Check OS
check_os
# Check distribution
check_distribution
# Check sudo
check_privileges
# Check dependencies
check_dependencies
}