-
Notifications
You must be signed in to change notification settings - Fork 4
/
ask_user_choice.sh
executable file
·175 lines (133 loc) · 5.39 KB
/
ask_user_choice.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
#!/bin/bash
source src/hardcoded_variables.txt
read_categories() {
data_source=$1
if [[ $data_source == "selected" ]]; then
echo $(awk '/installationType:/ {print $2}' $SELECTED_SOFTWARE_LIST_LOCATION)
fi
if [[ $data_source == "supported" ]]; then
echo $(awk '/installationType:/ {print $2}' $SUPPORTED_SOFTWARE_LIST_LOCATION)
fi
}
read_software_packages() {
data_source=$1
# get all supported installation categories
installation_types=($(read_categories $data_source)) # outer brackets to convert string to list
software_packages=""
for i in "${!installation_types[@]}"; do
software_packages+=$(read_software_packages_per_category $data_source "${installation_types[i]}")" "
done
echo $software_packages
}
read_software_packages_per_category() {
data_source=$1
installation_type=$2
if [ $data_source == "selected" ]; then
echo $(awk '/'$installation_type':/ {print $2}' $SELECTED_SOFTWARE_LIST_LOCATION)
fi
if [ $data_source == "supported" ]; then
echo $(awk '/'$installation_type':/ {print $2}' $SUPPORTED_SOFTWARE_LIST_LOCATION)
fi
}
prompt_user_choice() {
# determine whether the user is installing or uninstalling
activity=$1
shift # eat first argument
# read incoming list of installation types that are tested
installation_types=("$@")
# clear output log
source src/helper.sh
$(remove_log_file $SELECTED_SOFTWARE_FILENAME)
# create output log file
$(create_log_file $SELECTED_SOFTWARE_FILENAME $LOG_LOCATION)
# initialise lists of softwares that are evaluated
supported_software_packages=""
selected_software_packages=""
# loop through each installation type
for i in "${!installation_types[@]}"; do
# Append the installation type to the user selection log
echo "installationType: ${installation_types[i]}" >> $LOG_LOCATION$SELECTED_SOFTWARE_FILENAME
# read the possible software packages per installation type
new_software=$(read_software_packages_per_category "supported" "${installation_types[i]}")
# Convert software packages from string to list
new_software_list=($new_software)
# Loop through list of software packages
for j in "${!new_software_list[@]}"; do
# Ask the user if they want to install this software package
user_input=$(ask_if_user_wants_some_software_package $activity ${new_software_list[j]})
# add the software package to the list or not, depending on user choice
if [ $(echo -n $user_input | tail -c 4) == true ]; then
# add the software package to the variable list
selected_software_packages+=${new_software_list[j]}" "
# add the software package to the user selection log
echo "${installation_types[i]}: "${new_software_list[j]} >> $LOG_LOCATION$SELECTED_SOFTWARE_FILENAME
fi
done
done
echo "The selected software packages are:"
echo $selected_software_packages
# TODO: ask verification of correctness of list to user.
}
# Convert the arguments passed to the cli in the form of a list of selected packages to an output file.
cli_args_to_package_list() {
# read incoming list of installation types that are tested
cli_args=("$@")
#echo "cli_args=${cli_args[@]}"
# TODO: swap username with element in this list make switch case.
# read the installation types that are tested
installation_types=$(read_categories "supported")
installation_types=($installation_types) # convert to array
#echo "in installation_types=${installation_types[@]}"
# clear output log
source src/helper.sh
$(remove_log_file $SELECTED_SOFTWARE_FILENAME)
# create output log file
$(create_log_file $SELECTED_SOFTWARE_FILENAME $LOG_LOCATION)
# initialise lists of softwares that are evaluated
supported_software_packages=""
selected_software_packages=""
# loop through each installation type
for i in "${!installation_types[@]}"; do
# Append the installation type to the user selection log
echo "installationType: ${installation_types[i]}" >> $LOG_LOCATION$SELECTED_SOFTWARE_FILENAME
# read the possible software packages per installation type
new_software=$(read_software_packages_per_category "supported" "${installation_types[i]}")
# Convert software packages from string to list
new_software_list=($new_software)
# Loop through list of software packages
for j in "${!new_software_list[@]}"; do
for k in "${!cli_args[@]}"; do
# add the software package to the list or not, depending on user cli arguments
if [ "${new_software_list[j]}" == "${cli_args[k]}" ]; then
# add the software package to the variable list
selected_software_packages+=${new_software_list[j]}" "
# add the software package to the user selection log
echo "${installation_types[i]}: "${new_software_list[j]} >> $LOG_LOCATION$SELECTED_SOFTWARE_FILENAME
fi
done
done
done
#echo "The selected software packages are:"
echo $selected_software_packages
# TODO: ask verification of correctness of list to user.
}
ask_if_user_wants_some_software_package() {
activity=$1
software_package=$2
if [ ${#software_package} -ge 1 ]; then
while true; do
if [ "$activity" == "install" ]; then
read -p "Do you wish to install: $software_package?" yn
elif [ "$activity" == "uninstall" ]; then
read -p "Do you wish to uninstall: $software_package?" yn
fi
case $yn in
[Yy]* ) echo "true"; break;;
[Nn]* ) echo "false"; break;;
* ) echo "Please answer yes or no.";;
esac
done
else
echo "false"
fi
}