-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·258 lines (210 loc) · 5.8 KB
/
install.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env bash
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
# Styles
NC='\033[0m' # No Color
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# Global variables
CURRENT_DIRECTORY="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
INSTALL_DIRECTORY="${CURRENT_DIRECTORY}/install"
CONFIG_DIRECTORY="${CURRENT_DIRECTORY}/config"
LOGS_DIRECTORY="${CURRENT_DIRECTORY}/logs"
SCRIPTS_DIRECTORY="${CURRENT_DIRECTORY}/scripts"
# Initialize variables
MULTIPLE_LOG_FILES=0
INSTALL_AUR=1
INSTALL_FLATPAKS=1
ENABLE_MULTILIB=1
INSTALL_FSTAB=1
# If PARALLEL_DOWNLOADS is greater than 0, it will be set otherwise ignored
PARALLEL_DOWNLOADS=0
FSTAB=()
errors=()
function print_message() {
echo -e "${BOLD}${2} ==> ${NC} ${BOLD}${1}${NORMAL}"
log "${2}==> ${1}"
}
function print_inner_message() {
echo -e "${BOLD}${BLUE} ->${NC} ${BOLD}${1}${NORMAL}"
log " -> ${1}"
}
function print_error() {
echo -e "${BOLD}${RED} ->${NC} ${BOLD}${1}${NORMAL}"
log " -> ${1}"
exit 1
}
function log() {
[[ -d ${LOGS_DIRECTORY} ]] || mkdir ${LOGS_DIRECTORY}
count=0
logfile="install.${count}.log"
# Use multiple log files
if [ ${MULTIPLE_LOG_FILES} -eq 1 ]; then
# log file already exists
if [ -f "${LOGS_DIRECTORY}/${logfile}" ]; then
# get it's new possible name
while [ -f "${LOGS_DIRECTORY}/${logfile}" ]; do
count=$(($count + 1))
logfile="install.${count}.log"
done
# rename the existing file
mv "${LOGS_DIRECTORY}/install.0.log" "${LOGS_DIRECTORY}/install.${count}.log"
# reset the name
count=0
logfile="install.${count}.log"
fi
fi
echo "${1}" >> "${LOGS_DIRECTORY}/${logfile}"
}
function pacman_install() {
sudo pacman -S --needed --noconfirm ${1} || print_message "Could not install package: ${1}"
}
function aur_install() {
paru -S --noconfirm --noprovides --skipreview ${1} || print_message "Could not install aur package: ${1}"
}
function flatpak_install() {
flatpak install -y ${1} || print_error "Could not install flatpak: ${1}"
}
function enable_parallel_downloads () {
sudo sed 's/#ParallelDownloads = 5/ParallelDownloads = '${PARALLEL_DOWNLOADS}'/' /etc/pacman.conf
}
function init_install_directory() {
[[ -d ${INSTALL_DIRECTORY} ]] && print_message "Install directory already exist"
mkdir -p ${INSTALL_DIRECTORY} || print_error "Unable to create the install directory"
}
function remove_install_directory() {
[[ -d ${INSTALL_DIRECTORY} ]] && rm -rf ${INSTALL_DIRECTORY}
}
function enable_multilib () {
sudo sed -i '/^#\[multilib\]/{N;s/#//g}' /etc/pacman.conf
}
function update_mirrors () {
sudo pacman -Syy --noconfirm
}
function upgrade_system () {
sudo pacman -Syu --noconfirm
}
function clear_pacman_cache () {
sudo pacman -Sc --noconfirm
}
function check_privileges() {
if [ "$(id -u)" = 0 ]; then
echo "####################################"
echo "This script MUST NOT be run as root!"
echo "####################################"
exit 1
fi
sudo echo ""
[[ ${?} -eq 1 ]] && print_error "Your root password is wrong"
}
function check_config() {
# Get all config files
configs=()
for file in "${CONFIG_DIRECTORY}"/*; do
configs+=($(basename $file))
done
# Check if there are config files
if [ ${#configs[@]} -eq 0 ]; then
print_error "No configs detected"
exit 1
fi
# Check if selected config was not given
if [ "$SELECTED_CONFIG" == "" ]; then
SELECTED_CONFIG="default"
fi
# Check if the given configuration file name exists
valid=0
for config in "${configs[@]}"; do
if [ "$SELECTED_CONFIG" == "$config" ]; then
valid=1
fi
done
if [ $valid -eq 0 ]; then
print_error "Configuration file \"$SELECTED_CONFIG\" not found"
exit 1
fi
}
usage()
{
echo "Usage: $0 [args]"
echo "Args:"
echo "--config <config_name>"
echo "--no-flatpak"
echo "--no-aur"
echo "--no-multilib"
echo "--parallel-downloads <amount>"
}
args=()
while [ "${1:-}" != "" ]; do
args+=("${1:-}")
shift
done
for i in "${!args[@]}"; do
arg=${args[$i]}
case $arg in
--config)
SELECTED_CONFIG=${args[$(($i + 1))]}
;;
--no-flatpak)
INSTALL_FLATPAK=0
;;
--no-aur)
INSTALL_AUR=0
;;
--no-multilib)
ENABLE_MULTILIB=0
;;
--parallel-downloads)
PARALLEL_DOWNLOADS=${args[$(($i + 1))]}
if ! [[ $PARALLEL_DOWNLOADS =~ '^[0-9]+$' ]] ; then
PARALLEL_DOWNLOADS=0
print_error "Invalid argument for PARALLEL_DOWNLOADS"
exit 1
fi
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
esac
done
check_privileges
check_config
source "${CONFIG_DIRECTORY}/${SELECTED_CONFIG}"
# Check configurable directories
[ "$DOTFILES_DIRECTORY" = "" ] && DOTFILES_DIRECTORY="${INSTALL_DIRECTORY}/dotfiles"
[ "$DOTFILES_ROOT_DIRECTORY" = "" ] && DOTFILES_ROOT_DIRECTORY="/opt/dotfiles_root"
# Check configurable vars
[ "$PACKAGES" = "" ] && print_error "PACKAGES is not defined"
if [ ${PARALLEL_DOWNLOADS} -gt 0 ]; then
enable_parallel_downloads
fi
if [ ${ENABLE_MULTILIB} -eq 1 ]; then
enable_multilib
fi
if [ ${INSTALL_FSTAB} -eq 1 ]; then
source "${SCRIPTS_DIRECTORY}/_config_fstab"
fi
update_mirrors
init_install_directory
source "${SCRIPTS_DIRECTORY}/_install_packages"
for script in "${SCRIPTS[@]}"; do
source "${SCRIPTS_DIRECTORY}/${script}"
done
clear_pacman_cache
remove_install_directory
print_message "Installation completed."
# Check for errors
if [ ${#errors[@]} -gt 0 ]; then
print_message "There are ${#errors[@]} errors. See logs/install.0.log for more informations."
fi
# # Check for errors
# if [ ${#errors[@]} -gt 0 ]; then
# print_message "Packages not installed:"
# for package in "${errors[@]}"; do
# print_inner_message ${package}
# done
# fi