#!/bin/bash
# -*-eselect-*-  vim: ft=eselect
# Copyright (c) 2005-2025 Gentoo Authors
#
# 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, see <http://www.gnu.org/licenses/>.

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

# Where are modules installed by default?
ESELECT_DEFAULT_MODULES_PATH="${ESELECT_DATA_PATH}/modules"

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

# 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_PROGRAM_NAME="eselect"
ESELECT_VERSION="1.4.30"

# Invocation information
ESELECT_BINARY_NAME="$0"
ESELECT_KILL_TARGET="$$"

# Support variables for Gentoo Prefix. BROOT is hardcoded as the
# prefix for the build host. EPREFIX defaults to the same, but can be
# overridden at runtime when building for a different prefix.
BROOT=""
: "${EPREFIX="${BROOT}"}"
EROOT="${ROOT%${EPREFIX:+/}}${EPREFIX}"

# Remove all alias definitions. Unset functions and variables that are
# known to cause trouble.
"unalias" -a
unset -f rm
unset CDPATH GLOBIGNORE

shopt -s extglob
shopt -s expand_aliases

umask +rx

# Sanitise PATH: We don't want to execute Portage's internal helpers
# if we're called from an ebuild.
IFS=:
# Append ${IFS} to prevent any trailing empty field from being dropped
set -f; path=(${PATH}${IFS}); set +f
for i in "${!path[@]}"; do
	[[ ${path[i]} == */portage?(/*)/ebuild-helpers?(/*) ]] && unset "path[i]"
done
PATH="${path[*]}"
unset i path

IFS=$' \t\n'

# Save stderr file descriptor
if (( BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 1 || BASH_VERSINFO[0] > 4 ))
then
	exec {ESELECT_STDERR}>&2	# >=bash-4.1
fi

# 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 'exit 250' 15

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

# es_do_help
# Display eselect help
es_do_help() {
	es_do_usage
	echo
	# display all recognized global options
	write_list_start "Global options:"
	write_kv_list_entry "--brief" "Make output shorter"
	write_kv_list_entry "--colour=<yes|no|auto>" \
		"Enable or disable colour output (default 'auto')"
	write_kv_list_entry "--eprefix=<path>" \
		"Override the EPREFIX variable for a cross-prefix build"
	write_kv_list_entry "--root=<path>" \
		"The target root path for eselect's operations"
	echo
	# display all available eselect modules
	do_action modules list
}

# es_do_version
# Display eselect version
es_do_version() {
	echo "eselect ${ESELECT_VERSION}"
	echo
	echo "Copyright (c) 2005-2025 Gentoo Authors"
	echo "Distributed under the terms of the GNU GPL version 2 or later."
}

### 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

# parse global options
colour=""
while [[ ${1##--} != "$1" ]]; do
	case ${1##--} in
		brief)
			set_output_mode brief
			;;
		colour=*|color=*|colour|color)
			# accept all arguments that are valid for ls or emerge
			case ${1#*=} in
				yes|y|always|force|"$1") colour=yes ;;
				no|n|never|none) colour=no ;;
				auto|tty|if-tty) colour="" ;;
				*) die -q "Invalid argument for ${1%%=*} option" ;;
			esac
			;;
		help|version)
			[[ -z ${action} ]] || die -q "Too many parameters"
			action=${1##--}
			;;
		eprefix=*|eprefix)
			[[ ${1#*=} == "$1" ]] && die -q "Option $1 requires an argument"
			# set EPREFIX and recalculate EROOT
			EPREFIX=${1#*=}
			EROOT="${ROOT%${EPREFIX:+/}}${EPREFIX}"
			;;
		root=*|root)
			[[ ${1#*=} == "$1" ]] && die -q "Option $1 requires an argument"
			# set ROOT and recalculate EROOT
			ROOT=${1#*=}
			EROOT="${ROOT%${EPREFIX:+/}}${EPREFIX}"
			;;
		"")
			# -- indicates end of options
			shift
			break
			;;
		*)
			die -q "Unknown option $1"
			;;
	esac
	shift
done

if [[ -z ${action} && $# -gt 0 ]]; then
	action=$1
	shift
fi

# enable colour output and get width of terminal iff stdout is a tty
if [[ -t 1 ]]; then
	# command line option overrides NO_COLOR variable
	[[ -z ${colour} && -n ${NO_COLOR} ]] && colour=no
	colours ${colour:-yes}
	init_columns
else
	colours ${colour:-no}
fi
unset colour

if [[ -n ${action} ]]; then
	if is_function "es_do_${action}"; then
		[[ $# -eq 0 ]] || die -q "Too many parameters"
		es_do_${action}
	else
		do_action "${action}" "$@"
	fi
else
	es_do_help
fi
