#!/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 unalias() { # quick exit code if [[ "${1:0:1}" != "%" ]] ; then echo $1 return fi local TARGET TARGETS TARGETBYNUM N CHOICE CACHED_ALIAS ALREADY PREFERED # lookup in cache CACHED_ALIAS=$(get_local_config `echo LUNAR_ALIAS_${1:1}`) if [ -n "$CACHED_ALIAS" ]; then echo $CACHED_ALIAS return fi debug_msg "unalias($@)" # 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 '{if ($1==mod){print $2}}' $ALIASES) # shortcut out when explicitly instructed so if [[ -n "$NEVER_ASK" ]] ; then echo $1 return fi # propose one and let the user pick it from a list: debug_msg "unalias: 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)" error_message "or the already installed module if there is one.${DEFAULT_COLOR}" ((N=0)) ((ALREADY=0)) ((PREFERED=1)) for TARGET in $TARGETS ; do ((N++)) TARGETBYNUM[$N]=$TARGET if module_installed $TARGET ; then ((ALREADY++)) ((PREFERED=$N)) 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 debug_msg "unalias: number of modules already installed = $ALREADY" debug_msg "unalias: already installed, or default option = $PREFERED" if test $ALREADY -gt 1 ; then read -p "Enter choice []: " CHOICE elif read -t $PROMPT_DELAY -p "Enter choice [default=$PREFERED]: " CHOICE ; then # check whether user hit return to get default if test "x$CHOICE" = "x" ; then CHOICE=$PREFERED fi else # timed out, so use default CHOICE=$PREFERED fi debug_msg "CHOICE = $CHOICE" # test directly first if echo $TARGETS | grep -qw "$CHOICE" ; then verbose_msg "Stored alias mapping $1 -> $TARGET" set_local_config `echo LUNAR_ALIAS_${1:1}` $TARGET 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 }