-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect-profile.sh
executable file
·87 lines (81 loc) · 2.6 KB
/
select-profile.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
# Function select_profile()
# This function generates a list of all the profile files present
# in the profile directory (${base}/etc/voyage-profiles), with
# the name of the profile defined within the file. It then asks
# the user to choose which profile he wants to use
# Params: $1 - the base directory name
# Returns: $w contains the profile name (NULL if error)
# environment variable VOYAGE_PROFILE updated if no error
#
select-profile() {
local filelist filename pro proix prolist saveifs var
# Have a "sanity check" to assure the profile directory exists
if [ ! -d $1/etc/voyage-profiles ]; then
err_msg "No voyage-profiles directory in $1/etc"
w=""
return
fi
# To get a clean directory list, we want to "cd" into the profile directory.
cd $1/etc/voyage-profiles
# generate a list of all the files
prolist=""
filelist=""
for filename in *; do
if [ -f $filename ]; then
# get the internal profile name
pro=`grep VOYAGE_PROFILE $filename | sed -e "s/VOYAGE_PROFILE=//"`
# if there are quotes or apostrophes, delete them
pro=${pro//[\"\']/}
if [ "$pro" ]; then
if [ "$prolist" ]; then
prolist="$prolist%$pro"
else
prolist=$pro
fi
if [ "$filelist" ]; then
filelist=$filelist%$filename
else
filelist=$filename
fi
fi
fi
done
# restore our directory
cd $RUNDIR
# Get current setting of $VOYAGE_PROFILE to use as user's default
# in the following call to ask_setting (list_str returns ix in $v)
list_str "$prolist" "$VOYAGE_PROFILE"
# if "run" is not defined, ask user for profile...
if [ "$2" != "run" ] ; then
# Now that we have the data, present it to the user
# (ask_setting returns the user's choice in $v)
ask_setting "Please select Voyage profile:" "$prolist" $v
fi
# save the user's choice - we'll need it when we fetch the file contents
proix=$v
list_str "$prolist" "$VOYAGE_PROFILE"
# Translate the numeric choice into the corresponding profile name
# (list_ix returns the name in $w)
list_ix "$prolist" $v
# Since the user has chosen to take a "preset" profile, we must get
# rid of any stored config settings. All profile settings begin with
# "VOYAGE_", so we can go through $CONFLIST and remove those
saveifs=$IFS
IFS="%"
for var in $CONFIGLIST; do
if [ ${var/#VOYAGE_*/VOYAGE_} == VOYAGE_ ]; then
delete_config_var $var CONFIGLIST
unset $var
fi
done
IFS=$saveifs
# Similarly, we clear the current profile
VOYAGE_CONF_LIST=""
# save the value
VOYAGE_PROFILE="$w"
save_config_var VOYAGE_PROFILE VOYAGE_CONF_LIST
save_config_var VOYAGE_PROFILE CONFIGLIST
# and then 'source' the file
list_ix "$filelist" $proix
read_config "$1/etc/voyage-profiles/$w" VOYAGE_CONF_LIST
}