-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapts
More file actions
executable file
·319 lines (290 loc) · 9.78 KB
/
apts
File metadata and controls
executable file
·319 lines (290 loc) · 9.78 KB
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
#!/usr/bin/env bash
#++++++++++++++++++++++++++++++
# VARIABLES.
# check for extisting tmp dir and
# read package name from command arguments
arg_amount="$#"
exact=false
debug=false
update=false
upgrade=false
detail="normal"
apt_command="install"
apt_options=""
shopt -s extglob
#declare -a cmd_arg_array
#echo "arguments are: ${@:2}"
help(){
echo -e "apts: apt-get installer helper script"
echo -e "Searches for available packages in apt's repositories and installs them, if selected."
echo -e "\tUSAGE: apts [OPTIONS] SEARCHSTRING\n"
echo -e "\tThis script requires root privileges to run."
echo -e "\tOPTIONS:"
echo -e "\t\t-y\tForce yes when installing/uninstalling"
echo -e "\t\t-e\tOnly find packages with SEARCHSTRING in pkg-name"
echo -e "\t\t-d\tEnable debug mode (print apt command rather than executing it)"
echo -e "\t\t-s\tUse short list format when displaying results"
echo -e "\t\t-l\tUse detailed list format when displaying results"
echo -e "\t\t-u\tUpdate package list before searching/installing"
echo -e "\t\t-U\tUpgrade packages before searching/installing (assumes 'yes')"
echo -e "\t\t-h\tDisplay this help message\n"
exit 0
}
case $arg_amount in
1)
pkg="$1"
cmd_args=""
;;
0)
help
exit 0
;;
*)
#pkg="${@: -1}"
pkg=${@:$#} # last parameter
cmd_args_rest=${*%${!#}} # all parameters except the last
cmd_args_raw=$(echo "$cmd_args_rest" | tr -cd 'eydslhuU')
cmd_args=$(echo "$cmd_args_raw" | grep -o . | sort -u | tr -d '\n')
# echo $cmd_args_rest
# echo $cmd_args_raw
# echo $cmd_args
if [[ $(echo "$cmd_args" | grep -qiE '[^eydslhuU]' && echo "$?") = 0 ]]; then
echo -e "$cmd_args not a recognised option. Type 'apts -h' to display help."
fi
;;
esac
. /home/kallefornia/coding/bash/colours
#++++++++++++++++++++++++++++++
# FUNCTIONS
prepare(){
if [[ $update = true ]]; then
sudo apt-get --quiet update
fi
if [[ $upgrade = true ]]; then
sudo apt-get --quiet upgrade -y
fi
tmp_list="apts.list"
tmp_dir="/home/kallefornia/.temp/apts"
if [ ! -d "$tmp_dir" ]; then
mkdir "$tmp_dir"
fi
tmp_list_path="${tmp_dir}/${tmp_list}"
echo "$(sudo apt search $pkg)" > $tmp_list_path 2>/dev/null
}
# Output take package name and assigned number and output info text
pkg_info(){
pkgnumber="$1"
pkgid="$2"
echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo -e "$pkgnumber:\t$pkgid"
echo -e "\t$(apt-get info ${pkgid})"
echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
}
# $counter $txtlineno $pkgname $pkgversion
print_pkg(){
pkgnumber="$1"
pkgidlineno=$2
pkgtxtlineno=$((pkgidlineno+1))
pkgid="$3"
pkgver=$4 # $( echo $4 | tr ',' '\n' | uniq | xargs)
# echo "$pkgver"
if [[ $(echo "$4" | grep -qiF install && echo "$?") = 0 ]]; then
installed=true
install_status=$(echo "$pkgver" | awk -F'[][]' '{print $2}')
status_style=$Green
name_style=$BIGreen${UGreen}
else
installed=false
install_status="Not installed"
status_style=$Red
name_style=${BICyan}${UCyan}
fi
distro=$(echo "$pkgver" | sed -e 's/\[[^][]*\]//g' | awk '{print $1}')
version_no=$(echo "$pkgver" | sed -e 's/\[[^][]*\]//g' | awk '{$1="";print $0}')
pkgtxt="$(sed -n ${pkgtxtlineno}p $tmp_list_path | sed 's/^[ \t]*//;s/[ \t]*$//')"
case $detail in
short)
echo -e "${BIYellow} $pkgnumber:\t${name_style}${pkgid}${EndColor}"
echo -e "\t${BIYellow}>>> ${White}$pkgtxt ${EndColor}\n"
;;
normal)
echo -e "${BIYellow}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
echo -e "${BIWhite} $pkgnumber:\t${name_style}${pkgid}${EndColor}\n"
echo -e "${On_IBlue}${BBlack}\tDistro:${On_Cyan}${White} ${distro}\t${On_Cyan}${BBlack}Version:${On_Red}${White} ${version_no} ${EndColor}"
echo -e "${BWhite}\tStatus: ${status_style}$install_status ${EndColor}"
echo -e "${BWhite}\tPackage Info:${EndColor}\n\t${BIYellow}>>> ${White}$pkgtxt ${EndColor}"
#echo -e "\n"
;;
long)
echo -e "${BIYellow}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
echo -e "${BIWhite} $pkgnumber:\t${name_style}${pkgid}${EndColor}\n"
echo -e "${BWhite}\tStatus: ${status_style}$install_status ${EndColor}"
echo -e "${Yellow}$(sudo apt-get--quiet show $pkgid)${EndColor}"
;;
esac
}
# main function
main(){
# read all available packages and their line numbers and store them in an associative array (pkgindex)
declare -A pkgindex
declare -a orderarray
while IFS=$':' read lineno title version; do
#echo -e "$lineno\t$title\t$version"
pkgindex[$lineno]="${title}:${version}"
orderarray+=($lineno)
done < <( grep -n -i -v -e "full" -e "sorting" -e '^$' -e '^[[:space:]]' "./.temp/apts/apts.list" | awk -F'/' '{printf "%s:%s\n", $1, $2}')
# for i in "${orderarray[@]}"; do #"${!pkgindex[@]}"; do
# #echo -e "lineno from orderarray: $i"
# idx_tuple=${pkgindex[$i]}
# #echo "idx_tuple = $idx_tuple"
# echo -e "$i: $idx_tuple"
# done
# echo "${pkgindex[@]}"
#exit 0
# search for desired [string] in list of available packages and look for the previous non-indendet line (i.e.
# the "headline" of the text matching [string], which should include the corresponding package name).
### This is done by taking the line number (txtlineno) where [string] is matched, continuously decrementing it by 1 and
### checking the array of package names (pgkindex) for a matching number.
# Then assign a sequential number (counterup) to the matched package name and store them in an array (matchedpkg)
declare -A matchedpkg
counter=1
echo -e "${BIYellow}Found the following packages containing ${BICyan}$pkg:${EndColor}\n"
while IFS=$':' read txtlineno pkgname pkgversion_raw; do
# echo "$txtlineno"
case $exact in
true)
if [[ $(echo "$pkgname" | grep -qiF "$pkg" && echo "$?") != 0 ]]; then
continue
fi
;;
false)
:
;;
esac
matchedpkg[$counter]="$pkgname"
pkgversion=$(echo "$pkgversion_raw" | tr ', ' '\n' | uniq | xargs)
# echo -e "Counter: $counter\nLine no: $txtlineno\nName: $pkgname\nVersion: $pkgversion"
print_pkg $counter $txtlineno $pkgname "$pkgversion"
((counter++))
done < <(for line_no in "${orderarray[@]}"; do
pkg_tuple="${pkgindex[$line_no]}"
echo "${line_no}:${pkg_tuple}";
done)
# ask for user input about what to do next. Read choice into variable (choice).
# input can be either the number(s) of the packages to be installed, all packages, or to quit
result_count=$((counter-1))
echo -e " ${BIYellow}${UYellow}Which packages do you want to install?${EndColor}"
echo -e " ${Yellow}Enter numbers as comma-seperated list:${EndColor} ${White}${On_IBlue}${BWhite}[1-$result_count]${EndColor} (e.g. 1, 2, 3), ${On_Purple}${BWhite}all[a]${EndColor} or ${On_Red}${BWhite}quit[q]${EndColor} to quit..."
echo -e " ${BIYellow}Enter ${On_Purple}'++'${EndColor} ${BIYellow}for advanced options${EndColor}"
read -p " >>> " selection
choice=$(echo $selection | tr -d '[:blank:]' | sed 's/,/ /g')
declare -a installarray
if [[ $choice =~ '++' ]]; then
echo -e "\t${UWhite}${BIRed}Advanced Options:${EndColor}"
echo -e "\t\t${BWhite}[d] Switch to 'REMOVE'${EndColor}"
echo -e "\t\t${BRed}[p] Enable 'PURGE'${EndColor}"
echo -e "\t\t${BBlue}[r] Enable 'REINSTALL'${EndColor}"
echo -e "\t\t${BYellow}[b] Enable 'FIX BROKEN'${EndColor}"
echo -e "\t\t${BCyan}[m] Enable 'FIX MISSING'${EndColor}"
echo -e "\t\t${BGreen}[i] Allow 'INSECURE' and 'UNAUTHENTICATED'${EndColor}"
echo -e "\t${BWhite}Enter ${On_Red}[d|p|r|b|m|i]${EndColor} ${BWhite}(one or multiple, no commas!) or ${On_Purple}[q]${EndColor}${BWhite} to return.${EndColor}"
read -p ">>> " adv_selection
case $adv_selection in
quit)
echo "returning to previous menu..."
;;
*d*)
apt_command="remove"
echo "Switched to 'remove'"
;;&
*p*)
apt_options+="--purge "
echo "enabled '--purge'"
;;&
*r*)
apt_options+="--reinstall "
echo "enabled '--reinstall'"
;;&
*b*)
apt_options+="--fix-broken "
echo "enabled '--fix-broken'"
;;&
*m*)
apt_options+="--fix-missing "
echo "enabled '--fix-missing'"
;;&
*i*)
apt_options+="--allow-insecure-repositories --allow-unauthenticated "
echo "enabled '--allow-insecure-repositories' and '--allow-unauthenticated'"
;;
esac
echo -e " ${BIYellow}${UYellow}Which packages do you want to install?${EndColor}"
echo -e " ${Yellow}Enter numbers as comma-seperated list:${EndColor} ${White}${On_IBlue}${BWhite}[1-$result_count]${EndColor} (e.g. 1, 2, 3), ${On_Purple}${BWhite}all[a]${EndColor} or ${On_Red}${BWhite}quit[q]${EndColor} to quit..."
echo -e " ${BIYellow}Enter ${On_Purple}'++'${EndColor} ${BIYellow}for advanced options${EndColor}"
read -p " >>> " selection
choice=$(echo $selection | tr -d '[:blank:]' | sed 's/,/ /g')
fi
case $choice in
q|quit)
echo "Exiting without installing..."
exit 0
;;
a|all)
echo "Installing these packages:"
for pkg in "${matchedpkg[@]}"; do
installarray+="$pkg "
echo -e "\t$pkg"
done
;;
*[0-9]*|*[0-9][0-9]*|*[0-9][0-9][0-9]*)
echo "Installing these packages:"
while IFS= read -a choiceno; do
pkginst="${matchedpkg[$choiceno]}"
echo -e "\t$pkginst"
installarray+="$pkginst "
done < <(for sel in $choice; do echo "$sel"; done)
;;
*)
echo "Invalid input ($choice), exiting..."
exit 0
;;
esac
shopt -u extglob
if [[ $debug = true ]]; then
echo "sudo apt-get $apt_command $apt_options $installarray"
else
sudo apt-get $apt_command $apt_options $installarray
fi
echo "Installed all packages, done."
}
shopt -s extglob
case $cmd_args in
*h*)
help
exit 0
;;
*e*)
exact=true
;;&
*d*)
debug=true
;;&
*y*)
apt_command+="-y "
;;&
*u*)
update=true
;;&
*U*)
upgrade=true
;;&
*s*)
detail="short"
;;
*l*)
detail="long"
;;
esac
prepare
main
exit 0