#! /usr/bin/env sh
# This script is part of the eix project and distributed under the
# terms of the GNU General Public License v2.
#
# Author and Copyright (c):
#   Martin V\"ath <vaeth@mathematik.uni-wuerzburg.de>
#
# This file must be "source"d by POSIX scripts.
#
# It can add or remove local layman overlays to the current eix
# database. (eix 0.22.5).

. '/usr/bin/eix-functions.sh'
ReadFunctions

Usage() {
	exitcode="${1:-1}"
	n="${0##*/}"
	p='eix 0.22.5'
	eval_gettext 'Usage: ${n} [options] command
Add or remove local layman overlays to the eix database (${p}).

Explanation: If your /etc/make.conf does not source layman/make.conf,
then the layman overlays are not considered as part of you PORTDIR_OVERLAY;
in this case, they are are usually not added to your eix database and thus
cannot be found with eix.
However, you might want to add/remove them temporarily to your eix database
without changing your /etc/make.conf. This is the purpose of this script.
This script is also meant as an example how to use eix-functions.sh.

The following commands are provided:

add:    Add the local layman overlays to the eix database
remove: Remove the local layman overlays from the eix database.

Available options:

-a OVERLAY    Add overlay to the list of layman overlays.
-A PATH       Add PATH to the sourced make.confs used to determine
              the layman overlays. Current value:'
	eval "set -- ${layman_make}"
	[ ${#} -eq 0 ] && set -- "`gettext \
		'none (i.e. no readable layman make.conf specified)'`"
	for i
	do	printf '%s%s' '
              ' "${i}"
	done
	gettext '
-C            Clear the above list
-c LAYMAN_CFG Use layman.cfg file to determine entry for above list
-v            Verbose (default)
-q            Quiet'
	echo
	exit ${exitcode:-1}
}

ClearLaymanMake() {
	Push -c layman_make
}

PushLaymanMake() {
	r=1
	for m
	do	[ -n "${m}" ] || continue
		test -r "${m}" || continue
		Push layman_make "${m}"
		r=0
	done
	return ${r}
}

PrintLaymanMake() {
	printf '%s\n%s\n' 'from layman.config import Config' \
	"print(Config()['make_conf'])" | python -- "${@}" 2>/dev/null
}

CalcLaymanMake() {
	l=`PrintLaymanMake "${@}"` || l=''
	[ -n "${l}" ] && PushLaymanMake "${l}"
}

GetPortdir
ClearLaymanMake
CalcLaymanMake || \
	PushLaymanMake '/usr/local/portage/layman/make.conf' || \
	PushLaymanMake "${local_portdir:-/}local/layman/make.conf"
layman_overlays=''

eval "Push -c opt `eix-update --print EIX_LAYMAN_OPTS`"
Push opt "${@}"
eval "set -- ${opt}"
OPTIND=1
while getopts 'vqa:A:c:C?hH' opt
do	case "${opt}" in
		v) verbose=:;;
		q) verbose=false;;
		a) Push layman_overlays "${OPTARG}";;
		c) CalcLaymanMake -c "${OPTARG}";;
		A) PushLaymanMake "${OPTARG}";;
		C) ClearLaymanMake;;
		*) Usage 0;;
	esac
done
[ ${OPTIND} -gt 1 ] && shift "`Expr ${OPTIND} - 1`"

GetOverlays() {
	eval "set -- ${layman_make}"
	for i
	do	[ -n "${i}" ] && test -r "${i}" && Push layman_overlays \
			`PORTDIR_OVERLAY=''; . "${i}"; \
				printf '%s' "${PORTDIR_OVERLAY}"`
	done
}

Init() {
	ClearUpdateArgs
	GetOverlays
	AddLocalMethods
}

Add() {
	Init
	eval "AddOverlays ${layman_overlays}"
	CallUpdate
}

Remove() {
	Init
	eval "AddExcludes ${layman_overlays}"
	CallUpdate
}

main_command="${1}"
[ ${#} -gt 0 ] && shift

case "${main_command}" in
add*)
	Add "${@}";;
rem*|del*|rm*|sub*)
	Remove "${@}";;
*)	Usage;;
esac

exit 0
