forked from lunar-linux/lunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.lunar
81 lines (75 loc) · 3.26 KB
/
aliases.lunar
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
#!/bin/bash
############################################################
# #
# aliases.lunar - Lunar alias code #
# #
############################################################
# #
# Copyright 2004 by Auke Kok under GPLv2under GPLv2 #
# #
############################################################
# translate %ALIAS if needed to a module name that is installed
# and add it to the dependency chain if needed
expand_alias() {
# If param doesn't start with "%", or if NEVER_ASK
# is set, just echo the param and return immediately
if [[ "${1:0:1}" != "%" ]]; then
echo $1
return
fi
local TARGET TARGETS TARGETBYNUM N CHOICE CACHED_ALIAS
# lookup in cache
CACHED_ALIAS=$(get_local_config LUNAR_ALIAS_${1:1})
if [[ -n "$CACHED_ALIAS" ]]; then
echo $CACHED_ALIAS
return
fi
if [[ -n "$NEVER_ASK" ]]; then
# there's no stored value for the alias and we mustn't ask for one...
echo $1
return
fi
debug_msg "expand_alias($@)"
# try to figure out where the aliases file is:
if [[ -z "$ALIASES" ]] || [[ ! -f "$ALIASES" ]]; then
if [ -f "$MOONBASE/aliases" ] ; then
ALIASES="$MOONBASE/aliases"
else
ALIASES="/var/lib/lunar/aliases"
fi
fi
TARGETS=$(awk -F: -v mod=$1 '$1 == mod {print $2}' "$ALIASES")
# propose one and let the user pick it from a list:
debug_msg "expand_alias: starting selection loop"
error_message "${MODULE_COLOR}$MODULE${DEFAULT_COLOR}${MESSAGE_COLOR} depends on ${DEFAULT_COLOR}${QUERY_COLOR}\"${1:1}\"${DEFAULT_COLOR}${MESSAGE_COLOR} which is an alias${DEFAULT_COLOR}"
while true ; do
error_message "${MESSAGE_COLOR}Please select a substitute ! Enter the number or the name of the module"
error_message "Press [ENTER] to select choice #1 (the recommended choice).${DEFAULT_COLOR}"
((N=0))
for TARGET in $TARGETS ; do
((N++))
TARGETBYNUM[$N]=$TARGET
if module_installed $TARGET ; then
error_message " ${QUERY_COLOR}$N${MESSAGE_COLOR} - ${DEFAULT_COLOR}${MODULE_COLOR}$TARGET${DEFAULT_COLOR} ${LRM_COLOR}(Installed)${DEFAULT_COLOR}"
else
error_message " ${QUERY_COLOR}$N${MESSAGE_COLOR} - ${DEFAULT_COLOR}${MODULE_COLOR}$TARGET${DEFAULT_COLOR} ${MESSAGE_COLOR}(Not installed)${DEFAULT_COLOR}"
fi
done
echo -n -e "${MESSAGE_COLOR}Choice> ${DEFAULT_COLOR}" >&2
read -t $PROMPT_DELAY CHOICE
# test directly first
if [[ " $TARGETS " =~ " $CHOICE " ]] ; then
verbose_msg "Stored alias mapping $1 -> $CHOICE"
set_local_config `echo LUNAR_ALIAS_${1:1}` $CHOICE
echo $TARGET
return
# then the number, resolving the default
elif [[ -n "${TARGETBYNUM[${CHOICE:=1}]}" ]] ; then
verbose_msg "Stored alias mapping $1 -> ${TARGETBYNUM[$CHOICE]}"
set_local_config `echo LUNAR_ALIAS_${1:1}` ${TARGETBYNUM[$CHOICE]}
echo ${TARGETBYNUM[$CHOICE]}
return
fi
error_message "${MESSAGE_COLOR}Sorry, I can't do anything with \"${DEFAULT_COLOR}${QUERY_COLOR}$CHOICE${DEFAULT_COLOR}${MESSAGE_COLOR}\", please try again${DEFAULT_COLOR}"
done
}