#!/bin/bash

# Copyright (c) 2005 Gentoo Foundation.
# $Id$
# This file is part of the 'eselect' tools framework.
#
# eselect is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# eselect is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# eselect; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA

# Where is our data?
ESELECT_DATA_PATH="/usr/share/eselect/"

# Look in these places for modules
ESELECT_MODULES_PATH=( \
    "${HOME}/.eselect/modules" \
    "${ESELECT_DATA_PATH}/modules" )

# Look in this place for libraries
ESELECT_CORE_PATH="${ESELECT_DATA_PATH}/libs"

# Look here for the default contents of a module
ESELECT_DEFAULT_ACTIONS="${ESELECT_CORE_PATH}/default.eselect"

# Our program name and version
ESELECT_VERSION="1.0.11"
ESELECT_PROGRAM_NAME="eselect"

# Invokation information
ESELECT_BINARY_NAME="${0}"
ESELECT_KILL_TARGET="$$"

# Global options
ESELECT_KNOWN_OPTIONS="no-colour no-color"
ESELECT_OPTIONS=""

shopt -s extglob
shopt -s expand_aliases

# Load core functions
source "${ESELECT_CORE_PATH}/core.bash" || exit 255
# Load necessary functions for the main script
inherit manip output path-manipulation tests

# Sneaky trick to make die in subshells work. If you don't get
# it, don't ask...
trap 'echo "exiting." ; exit 250' 15

# ec_find_module foo
# Find and echo the filename of the foo module. If there's no foo module,
# die.
ec_find_module() {
    local modname="$1" modpath="" modfile=""
    [[ -z ${modname} ]] && die "Usage: ${FUNCNAME} <module>"
    for modpath in "${ESELECT_MODULES_PATH[@]}" ; do
        [[ -f ${modpath}/${modname}.eselect ]] && break
    done

    modfile="${modpath}/${modname}.eselect"
    [[ -r ${modfile} ]] || die -q "Can't load module ${modname}"
    echo ${modfile}
}

# ec_do_usage
# Display eselect usage
ec_do_usage() {
    echo "Usage: eselect <global options> <module name> <module options>"
}

# ec_do_help
# Display eselect help
ec_do_help() {
    ec_do_usage
    echo
    ec_do_list-options
    echo
    ec_do_list-modules
}

# ec_do_version
# Display eselect version
ec_do_version() {
    echo "eselect ${ESELECT_VERSION}"
    echo
    echo "Copyright (c) 2005 Gentoo Foundation. Distributed under the"
    echo "terms of the GNU General Public License v2."
}

# ec_do_list-options
# Display all recognized global options
ec_do_list-options() {
    write_list_start "Global options:"
    write_kv_list_entry "--no-color,--no-colour"    "Disable coloured output"
}

# ec_do_list-modules
# Display all available eselect modules
ec_do_list-modules() {
    local path file module name desc

    write_list_start "Built-in modules:"
    write_kv_list_entry "help"          "Display a help message"
    write_kv_list_entry "list-modules"  "Find and display available modules"
    write_kv_list_entry "usage"         "Display a usage message"
    write_kv_list_entry "version"       "Display version information"

    extra_modules=()
    for path in "${ESELECT_MODULES_PATH[@]}" ; do
        [[ -d ${path} ]] || continue
        for file in ${path}/*.eselect ; do
            [[ -f ${file} ]] || continue
            extra_modules=( ${extra_modules[@]} "${file}" )
        done
    done

    if [[ ${#extra_modules[@]} -gt 0 ]] ; then
        echo
        write_list_start "Extra modules:"
        for module in ${extra_modules[@]} ; do
            name=${module##*/}
            name=${name%%.eselect}
            desc=$(
                source "$ESELECT_DEFAULT_ACTIONS" 2>/dev/null \
                    || die "Couldn't source ${ESELECT_DEFAULT_ACTIONS}"
                source "${module}" 2>/dev/null \
                    || die "Couldn't source ${module}"
                echo "${DESCRIPTION}"
            )
            write_kv_list_entry "${name}" "${desc}"
        done
    fi
}

### main code ###

# figure out what the action is. we need to know whether we're
# invoked as a something-config/something-update.
action=""

for suffix in config update{,r} tool manager reader ; do
    if [[ ${0%%-${suffix}} != ${0} ]] ; then
        action=$(basename "${0}" )
        action=${action%%-${suffix}}
        break
    fi
done
unset suffix

if [[ -z ${action} ]] ; then
    binname=$(basename "${0}" )
    for prefix in config update{,r} manage 'read' ; do
        if [[ ${binname##${prefix}-} != ${binname} ]] ; then
            action=$(basename "${0}" )
            action=${action##${prefix}-}
            break
        fi
    done
    unset binname prefix
fi

if [[ -z ${action} ]] && [[ -n ${1##--} ]] ; then
    while [[ ${1##--} != ${1} ]] ; do
        has ${1##--} "${ESELECT_KNOWN_OPTIONS[@]}" || \
            die -q "Unknown option ${1}!"
        case ${1##--} in
            no-colour|no-color)
                ESELECT_OPTIONS=(${ESELECT_OPTIONS[@]} "NOCOLOUR")
                nocolours
                shift
                ;;
        esac
    done
    action=${1}
    shift
fi

if [[ -n "${action}" ]] ; then
    if is_function "ec_do_${action}" ; then
        ec_do_${action} "${@}"
    else
        do_action "${action}" "${@}"
    fi
else
    ec_do_help
fi

# vim: set sw=4 et sts=4 tw=80 :
