# Copyright 2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id$ inherit config EDITOR_VAR="EDITOR" EDITOR_ENVFILE="/etc/env.d/99editor" # list of most common cases only EDITOR_LIST="/bin/nano /usr/bin/emacs /usr/bin/vi /usr/bin/xemacs" DESCRIPTION="Manage ${EDITOR_VAR} variable" MAINTAINER="ulm@gentoo.org" SVN_DATE='$Date$' VERSION=$(svn_date_to_version "${SVN_DATE}") find_targets() { local cur targets i targets=${EDITOR_LIST} cur=$(read_env_value) # also include the current value if it's not already in our list [[ -n ${cur} ]] && ! has "${cur}" "${targets}" \ && targets="${targets} ${cur}" for i in ${targets}; do [[ -f "${ROOT}${i}" ]] && echo "${i}" done } read_env_value() { load_config "${ROOT}${EDITOR_ENVFILE}" "${EDITOR_VAR}" } write_env_value() { [[ -w "${ROOT}${EDITOR_ENVFILE%/*}" ]] \ || die -q "You need root privileges!" store_config "${ROOT}${EDITOR_ENVFILE}" "${EDITOR_VAR}" "${1}" } ### show action ### describe_show() { echo "Show value of the ${EDITOR_VAR} variable in profile" } do_show() { [[ ${#@} -gt 0 ]] && die -q "Too many parameters" local cur=$(read_env_value) write_list_start "${EDITOR_VAR} variable in profile:" write_kv_list_entry "${cur:-(none)}" } ### list action ### describe_list() { echo "List available targets for the ${EDITOR_VAR} variable" } do_list() { [[ ${#@} -gt 0 ]] && die -q "Too many parameters" local cur targets i cur=$(read_env_value) targets=( $(find_targets) ) write_list_start "Available targets for the ${EDITOR_VAR} variable:" if [[ -n ${targets[@]} ]]; then for (( i = 0; i < ${#targets[@]}; i = i + 1 )); do # Display a star to indicate the currently chosen version [[ ${targets[${i}]} = ${cur} ]] \ && targets[${i}]="${targets[${i}]} $(highlight '*')" done write_numbered_list "${targets[@]}" fi write_numbered_list_entry " " "(free form)" } ### set action ### describe_set() { echo "Set the ${EDITOR_VAR} variable in profile" } describe_set_options() { echo "target : Target name or number (from 'list' action)" } describe_set_parameters() { echo "" } do_set() { [[ -z "${1}" ]] && die -q "You didn't tell me what to set the variable to" [[ ${#@} -gt 1 ]] && die -q "Too many parameters" local target="${1}" targets dir # target may be specified by its name or its index if is_number "${target}"; then targets=( $(find_targets) ) [[ ${target} -ge 1 && ${target} -le ${#targets[@]} ]] \ || die -q "Number out of range: ${1}" target=${targets[$(( ${target} - 1 ))]} fi # is the target an absolute path? if not, try to find it if [[ -n ${target##/*} ]]; then for dir in /bin /usr/bin; do [[ -f "${ROOT}${dir}/${target}" ]] || continue target=${dir}/${target} break done fi # we should now have a path to an existing binary [[ -z ${target##/*} && -f "${ROOT}${target}" ]] \ || die -q "Target \"${target}\" doesn't appear to be valid!" echo "Setting ${EDITOR_VAR} to ${target} ..." write_env_value "${target}" # update profile do_action env update noldconfig } ### update action ### describe_update() { echo "Update the ${EDITOR_VAR} variable if it is unset or invalid" } do_update() { [[ ${#@} -gt 0 ]] && die -q "Too many parameters" local cur targets cur=$(read_env_value) [[ -n ${cur} && -f "${ROOT}${cur}" ]] && return targets=( $(find_targets) ) [[ ${#targets[@]} -gt 0 ]] \ || die -q "No valid target for ${EDITOR_VAR} found" echo "Setting ${EDITOR_VAR} to ${targets[0]} ..." write_env_value "${targets[0]}" do_action env update noldconfig }