-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedfuncs
executable file
·577 lines (569 loc) · 17.2 KB
/
sharedfuncs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/bin/bash
#-------------------------------------------------------------------------------
#Created by helmuthdu mailto: helmuthdu[at]gmail[dot]com
#Contribution: flexiondotorg
#-------------------------------------------------------------------------------
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
# Run this script after your first boot with archlinux (as root)
#VARIABLES {{{
checklist=( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
# COLORS {{{
Bold=$(tput bold)
Underline=$(tput sgr 0 1)
Reset=$(tput sgr0)
# Regular Colors
Red=$(tput setaf 1)
Green=$(tput setaf 2)
Yellow=$(tput setaf 3)
Blue=$(tput setaf 4)
Purple=$(tput setaf 5)
Cyan=$(tput setaf 6)
White=$(tput setaf 7)
# Bold
BRed=${Bold}$(tput setaf 1)
BGreen=${Bold}$(tput setaf 2)
BYellow=${Bold}$(tput setaf 3)
BBlue=${Bold}$(tput setaf 4)
BPurple=${Bold}$(tput setaf 5)
BCyan=${Bold}$(tput setaf 6)
BWhite=${Bold}$(tput setaf 7)
#}}}
# PROMPT {{{
prompt1="Enter your option: "
prompt2="Enter n° of options (ex: 1 2 3 or 1-3): "
prompt3="You have to manual enter the following commands, then press ${BYellow}ctrl+d${Reset} or type ${BYellow}exit${Reset}:"
#}}}
# EDITOR {{{
AUTOMATIC_MODE=0
if [[ -f /usr/bin/vim ]]; then
EDITOR="vim"
elif [[ -z $EDITOR ]]; then
EDITOR="nano"
fi
#}}}
# DESKTOP ENVIRONMENT
CINNAMON=0
GNOME=0
KDE=0
# ARCHITECTURE
ARCHI=`uname -m`
UEFI=0
# AUR PACKAGE
AUR=`echo -e "(${BPurple}aur${Reset})"`
#CURRENT DIRECTORY
AUI_DIR=`pwd`
MOUNTPOINT="/mnt"
# VERBOSE MODE
[[ $1 == -v || $1 == --verbose ]] && VERBOSE_MODE=1 || VERBOSE_MODE=0
# LOG FILE
LOG="${AUI_DIR}/`basename ${0}`_error.log"
[[ -f $LOG ]] && rm -f $LOG
PKG_MANAGER="pacman"
PKG=""
PKG_FAIL="${AUI_DIR}/`basename ${0}`_pkg_fail_list.log"
[[ -f $PKG_FAIL ]] && rm -f $PKG_FAIL
# CONNECTION CHECK
XPINGS=0
#MISC
SPIN="/-\|"
AUTOMATIC_MODE=0
#}}}
#COMMON FUNCTIONS {{{
error_msg() { #{{{
local MSG="${1}"
echo -e "${MSG}"
exit 1
} #}}}
cecho() { #{{{
echo -e "$1"
echo -e "$1" >>"$LOG"
tput sgr0;
} #}}}
ncecho() { #{{{
echo -ne "$1"
echo -ne "$1" >>"$LOG"
tput sgr0
} #}}}
spinny() { #{{{
echo -ne "\b${SPIN:i++%${#SPIN}:1}"
} #}}}
progress() { #{{{
ncecho " ";
while true; do
kill -0 $pid 2> /dev/null;
if [[ $? == 0 ]]; then
spinny
sleep 0.25
else
ncecho "\b\b";
wait $pid
retcode=$?
echo -ne "$pid's retcode: $retcode" >> $LOG
if [[ $retcode == 0 ]] || [[ $retcode == 255 ]]; then
cecho success
else
cecho failed
echo -e "$PKG" >> $PKG_FAIL
tail -n 15 $LOG
read -p "ERROR! Continue install [y/N]?" OPTION
[[ $OPTION != y ]] && exit 1
fi
break
fi
done
} #}}}
check_root() { #{{{
if [[ "$(id -u)" != "0" ]]; then
error_msg "ERROR! You must execute the script as the 'root' user."
fi
} #}}}
check_user() { #{{{
if [[ "$(id -u)" == "0" ]]; then
error_msg "ERROR! You must execute the script as a normal user."
fi
} #}}}
check_archlinux() { #{{{
if [[ ! -e /etc/arch-release ]]; then
error_msg "ERROR! You must execute the script on Arch Linux."
fi
} #}}}
check_hostname() { #{{{
if [[ `echo ${HOSTNAME} | sed 's/ //g'` == "" ]]; then
error_msg "ERROR! Hostname is not configured."
fi
} #}}}
check_pacman_blocked() { #{{{
if [[ -f /var/lib/pacman/db.lck ]]; then
error_msg "ERROR! Pacman is blocked. \nIf not running remove /var/lib/pacman/db.lck."
fi
} #}}}
check_domainname() { #{{{
DOMAINNAME=`echo ${HOSTNAME} | cut -d'.' -f2- | sed 's/ //g'`
# Hmm, still no domain name. Keep looking...
if [[ "${DOMAINNAME}" == "" ]]; then
DOMAINNAME=`grep domain /etc/resolv.conf | sed 's/domain //g' | sed 's/ //g'`
fi
# OK, give up.
if [[ "${DOMAINNAME}" == "" ]]; then
error_msg "ERROR! Domain name is not configured."
fi
} #}}}
check_connection(){ #{{{
XPINGS=$(( $XPINGS + 1 ))
ping_gw() {
IP_ADDR=`ip r | grep default | cut -d ' ' -f 3`
[[ -z $IP_ADDR ]] && IP_ADDR="8.8.8.8"
ping -q -w 1 -c 1 ${IP_ADDR} > /dev/null && return 1 || return 0
}
WIRED_DEV=`ip link | grep enp | awk '{print $2}'| sed 's/://'`
WIRELESS_DEV=`ip link | grep wlp | awk '{print $2}'| sed 's/://'`
if ping_gw; then
print_warning "ERROR! Connection not Found."
print_info "Network Setup"
conn_type_list=("Wired Automatic" "Wired Manual" "Wireless")
PS3="$prompt1"
select CONNECTION_TYPE in "${conn_type_list[@]}"; do
case "$REPLY" in
1)
systemctl start dhcpcd@${WIRED_DEV}.service
break
;;
2)
systemctl stop dhcpcd@${WIRED_DEV}.service
read -p "IP Address: " IP_ADDR
read -p "Submask: " SUBMASK
read -p "Gateway: " GATEWAY
ip link set ${WIRED_DEV} up
ip addr add ${IP_ADDR}/${SUBMASK} dev ${WIRED_DEV}
ip route add default via ${GATEWAY}
$EDITOR /etc/resolv.conf
break
;;
3)
ip link set ${WIRELESS_DEV} up
wifi-menu ${WIRELESS_DEV}
break
;;
*)
invalid_option
;;
esac
done
if [[ $XPINGS -gt 2 ]]; then
print_warning "Can't establish connection. exiting..."
exit 1
fi
check_connection
fi
} #}}}
check_vga() { #{{{
# Determine video chipset - only Intel, ATI and nvidia are supported by this script"
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Detecting video chipset "
local VGA=`lspci | grep VGA | tr "[:upper:]" "[:lower:]"`
if [[ -n $(dmidecode --type 1 | grep VirtualBox) ]]; then
cecho Virtualbox
VIDEO_DRIVER="virtualbox"
elif [[ -n $(echo ${VGA} | grep "nvidia") ]]; then
cecho Nvidia
read_input_text "Install NVIDIA proprietary driver" $PROPRIETARY_DRIVER
if [[ $OPTION == y ]]; then
VIDEO_DRIVER="nvidia"
else
VIDEO_DRIVER="nouveau"
fi
elif [[ -n $(echo ${VGA} | grep "intel corporation") ]]; then
cecho Intel
VIDEO_DRIVER="intel"
elif [[ -n $(echo ${VGA} | grep "advanced micro devices") ]]; then
cecho AMD/ATI
read_input_text "Install ATI proprietary driver" $PROPRIETARY_DRIVER
if [[ $OPTION == y ]]; then
VIDEO_DRIVER="catalyst"
else
VIDEO_DRIVER="ati"
fi
else
cecho VESA
VIDEO_DRIVER="vesa"
fi
OPTION="y"
[[ $VIDEO_DRIVER == intel || $VIDEO_DRIVER == vesa ]] && read -p "Confirm video driver: $VIDEO_DRIVER [Y/n]" OPTION
if [[ $OPTION == n ]]; then
read -p "Type your video driver [ex: sis, fbdev, modesetting]: " VIDEO_DRIVER
fi
} #}}}
read_input() { #{{{
if [[ $AUTOMATIC_MODE -eq 1 ]]; then
OPTION=$1
else
read -p "$prompt1" OPTION
fi
} #}}}
read_input_text() { #{{{
if [[ $AUTOMATIC_MODE -eq 1 ]]; then
OPTION=$2
else
read -p "$1 [y/N]: " OPTION
echo ""
fi
OPTION=`echo "$OPTION" | tr '[:upper:]' '[:lower:]'`
} #}}}
read_input_options() { #{{{
local line
local packages
if [[ $AUTOMATIC_MODE -eq 1 ]]; then
array=("$1")
else
read -p "$prompt2" OPTION
array=("$OPTION")
fi
for line in ${array[@]/,/ }; do
if [[ ${line/-/} != $line ]]; then
for ((i=${line%-*}; i<=${line#*-}; i++)); do
packages+=($i);
done
else
packages+=($line)
fi
done
OPTIONS=("${packages[@]}")
} #}}}
print_line() { #{{{
printf "%$(tput cols)s\n"|tr ' ' '-'
} #}}}
print_title() { #{{{
clear
print_line
echo -e "# ${Bold}$1${Reset}"
print_line
echo ""
} #}}}
print_info() { #{{{
#Console width number
T_COLS=`tput cols`
echo -e "${Bold}$1${Reset}\n" | fold -sw $(( $T_COLS - 18 )) | sed 's/^/\t/'
} #}}}
print_warning() { #{{{
#Console width number
T_COLS=`tput cols`
echo -e "${BRed}$1${Reset}\n" | fold -sw $(( $T_COLS - 1 ))
} #}}}
start_module() { #{{{
modprobe $1
} #}}}
replaceinfile() { #{{{
SEARCH=${1}
REPLACE=${2}
FILEPATH=${3}
FILEBASE=`basename ${3}`
sed -e "s/${SEARCH}/${REPLACE}/" ${FILEPATH} > /tmp/${FILEBASE} 2>"$LOG"
if [[ ${?} -eq 0 ]]; then
mv /tmp/${FILEBASE} ${FILEPATH}
else
cecho "failed: ${SEARCH} - ${FILEPATH}"
fi
} #}}}
add_module() { #{{{
#check if the number of arguments is less then 2
for MODULE in $1; do
[[ $# -lt 2 ]] && MODULE_NAME="$MODULE" || MODULE_NAME="$2";
echo "$MODULE" >> /etc/modules-load.d/$MODULE_NAME.conf
start_module "$MODULE"
done
} #}}}
update_early_modules() { #{{{
local NEW_MODULE=${1}
local OLD_ARRAY=`egrep ^MODULES= /etc/mkinitcpio.conf`
if [[ -n ${NEW_MODULE} ]]; then
# Determine if the new module is already listed.
_EXISTS=`echo ${OLD_ARRAY} | grep ${NEW_MODULE}`
if [ $? -eq 1 ]; then
source /etc/mkinitcpio.conf
if [[ -z ${MODULES} ]]; then
NEW_MODULES="${NEW_MODULE}"
else
NEW_MODULES="${MODULES} ${NEW_MODULE}"
fi
replaceinfile "MODULES=\"${MODULES}\"" "MODULES=\"${NEW_MODULES}\"" /etc/mkinitcpio.conf
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Rebuilding init "
mkinitcpio -p linux >>"$LOG" 2>&1 &
pid=$!;progress $pid
fi
fi
} #}}}
is_package_installed() { #{{{
#check if a package is already installed
for PKG in $1; do
pacman -Q $PKG &> /dev/null && return 0;
done
return 1
} #}}}
checkbox() { #{{{
#display [X] or [ ]
[[ "$1" -eq 1 ]] && echo -e "${BBlue}[${Reset}${Bold}X${BBlue}]${Reset}" || echo -e "${BBlue}[ ${BBlue}]${Reset}";
} #}}}
checkbox_package() { #{{{
#check if [X] or [ ]
is_package_installed "$1" && checkbox 1 || checkbox 0
} #}}}
aui_download_packages() { #{{{
for PKG in $1; do
#exec command as user instead of root
su - ${USER_NAME} -c "
[[ ! -d aui_packages ]] && mkdir aui_packages
cd aui_packages
curl -o $PKG.tar.gz https://aur.archlinux.org/packages/${PKG:0:2}/$PKG/$PKG.tar.gz
tar zxvf $PKG.tar.gz
rm $PKG.tar.gz
cd $PKG
makepkg -csi --noconfirm
"
done
} #}}}
aur_package_install() { #{{{
#install package from aur
for PKG in $1; do
if ! is_package_installed "${PKG}" ; then
if [[ $AUTOMATIC_MODE -eq 1 ]]; then
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Installing ${AUR} ${Bold}${PKG}${Reset} "
su - ${USER_NAME} -c "${AUR_PKG_MANAGER} --noconfirm -S ${PKG}" >>"$LOG" 2>&1 &
pid=$!;progress $pid
else
su - ${USER_NAME} -c "${AUR_PKG_MANAGER} -S ${PKG}"
fi
else
if [[ $VERBOSE_MODE -eq 0 ]]; then
cecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Installing ${AUR} ${Bold}${PKG}${Reset} success"
else
echo -e "Warning: ${PKG} is up to date --skipping"
fi
fi
done
} #}}}
package_install() { #{{{
#install packages using pacman
if [[ $AUTOMATIC_MODE -eq 1 || $VERBOSE_MODE -eq 0 ]]; then
for PKG in ${1}; do
PKG_REPO=`pacman -Sp --print-format %r ${PKG} | uniq | sed '1!d'`
case $PKG_REPO in
"core")
PKG_REPO="${BRed}${PKG_REPO}${Reset}"
;;
"extra")
PKG_REPO="${BYellow}${PKG_REPO}${Reset}"
;;
"community")
PKG_REPO="${BGreen}${PKG_REPO}${Reset}"
;;
"multilib")
PKG_REPO="${BCyan}${PKG_REPO}${Reset}"
;;
esac
if ! is_package_installed "${PKG}" ; then
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Installing (${PKG_REPO}) ${Bold}${PKG}${Reset} "
$PKG_MANAGER -S --noconfirm --needed ${PKG} >>"$LOG" 2>&1 &
pid=$!;progress $pid
else
cecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Installing (${PKG_REPO}) ${Bold}${PKG}${Reset} exists "
fi
done
else
$PKG_MANAGER -S --needed $1
fi
} #}}}
package_remove() { #{{{
#remove package
for PKG in ${1}; do
if is_package_installed "${PKG}" ; then
if [[ $AUTOMATIC_MODE -eq 1 || $VERBOSE_MODE -eq 0 ]]; then
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Removing ${Bold}${PKG}${Reset} "
pacman -Rcsn --noconfirm ${PKG} >>"$LOG" 2>&1 &
pid=$!;progress $pid
else
pacman -Rcsn ${PKG}
fi
fi
done
} #}}}
system_upgrade() { #{{{
local MODIFY=`stat /var/lib/pacman/sync | egrep ^Modify | cut -d':' -f2 | cut -d' ' -f2`
local TODAY=`date +%Y-%m-%d`
if [[ ${MODIFY} != ${TODAY} ]]; then
pacman -Syu ${1}
fi
} #}}}
system_update() { #{{{
if [[ $VERBOSE_MODE -eq 0 ]]; then
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Updating packages "
pacman -Syy >>"$LOG" 2>&1 &
pid=$!;progress $pid
else
pacman -Syy $1
fi
} #}}}
npm_install() { #{{{
#install packages using pacman
npm install -g $1
} #}}}
contains_element() { #{{{
#check if an element exist in a string
for e in "${@:2}"; do [[ $e == $1 ]] && break; done;
} #}}}
config_xinitrc() { #{{{
#create a xinitrc file in home user directory
cp -fv /etc/skel/.xinitrc /home/${USER_NAME}/
echo -e "exec $1" >> /home/${USER_NAME}/.xinitrc
chown -R ${USER_NAME}:users /home/${USER_NAME}/.xinitrc
} #}}}
invalid_option() { #{{{
print_line
echo "Invalid option. Try another one."
pause_function
} #}}}
pause_function() { #{{{
print_line
if [[ $AUTOMATIC_MODE -eq 0 ]]; then
read -e -sn 1 -p "Press any key to continue..."
fi
} #}}}
menu_item() { #{{{
#check if the number of arguments is less then 2
[[ $# -lt 2 ]] && PACKAGE_NAME="$1" || PACKAGE_NAME="$2";
#list of chars to remove from the package name
CHARS_TO_REMOVE=("Ttf-" "-bzr" "-hg" "-svn" "-git" "-stable" "-icon-theme" "Gnome-shell-theme-" "Gnome-shell-extension-");
#remove chars from package name
for CHARS in ${CHARS_TO_REMOVE[@]}; do PACKAGE_NAME=`echo ${PACKAGE_NAME^} | sed 's/'$CHARS'//'`; done
#display checkbox and package name
echo -e "$(checkbox_package "$1") ${Bold}$PACKAGE_NAME${Reset}"
} #}}}
mainmenu_item() { #{{{
echo -e "$(checkbox "$1") ${Bold}$2${Reset}"
} #}}}
elihw() { #{{{
[[ $OPT == b || $OPT == d ]] && break;
} #}}}
add_user_to_group() { #{{{
local _USER=${1}
local _GROUP=${2}
if [[ -z ${_GROUP} ]]; then
error_msg "ERROR! 'add_user_to_group' was not given enough parameters."
fi
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} Adding ${Bold}${_USER}${Reset} to ${Bold}${_GROUP}${Reset} "
gpasswd -a ${_USER} ${_GROUP} >>"$LOG" 2>&1 &
pid=$!;progress $pid
} #}}}
system_ctl() { #{{{
local ACTION=${1}
local OBJECT=${2}
ncecho " ${BBlue}[${Reset}${Bold}X${BBlue}]${Reset} systemctl ${ACTION} ${OBJECT} "
systemctl ${ACTION} ${OBJECT} >> "$LOG" 2>&1
pid=$!;progress $pid
}
#}}}
arch_chroot() { #{{{
arch-chroot $MOUNTPOINT /bin/bash -c "${1}"
}
#}}}
setkeymap() { #{{{
local keymaps=(`localectl list-keymaps`)
PS3="(shift+pgup/pgdown) $prompt1"
echo "Select keymap:"
select KEYMAP in "${keymaps[@]}"; do
if contains_element "$KEYMAP" "${keymaps[@]}"; then
break
else
invalid_option
fi
done
}
#}}}
setlocale() { #{{{
local locale_list=(`cat /etc/locale.gen | grep _ | sed 's/\..*$//' | sed '/@/d' | awk '{print $1}' | uniq | sed 's/#//g'`);
PS3="$prompt1"
echo "Select locale:"
select LOCALE in "${locale_list[@]}"; do
if contains_element "$LOCALE" "${locale_list[@]}"; then
LOCALE_8859="${LOCALE} ISO-8859"
LOCALE_UTF8="${LOCALE}.UTF-8"
break
else
invalid_option
fi
done
}
#}}}
settimezone() { #{{{
local zone=(`timedatectl list-timezones | sed 's/\/.*$//' | uniq`);
PS3="$prompt1"
echo "Select zone:"
select ZONE in "${zone[@]}"; do
if contains_element "$ZONE" "${zone[@]}"; then
local subzone=(`timedatectl list-timezones | grep ${ZONE} | sed 's/^.*\///'`)
PS3="$prompt1"
echo "Select subzone:"
select SUBZONE in "${subzone[@]}"; do
if contains_element "$SUBZONE" "${subzone[@]}"; then
break
else
invalid_option
fi
done
break
else
invalid_option
fi
done
} #}}}
#}}}