#! /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.28.2).

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

Usage() {
	exitcode=${1:-1}
	n=${0##*/}
	p='eix 0.28.2'
	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 your PORTDIR_OVERLAY;
in this case, they 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:'
	set -f
	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)
-H            Suppress status line update
-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() {
	if python - "${@}" -l 2>/dev/null <<ENDLAYTWO
from layman.argsparser import ArgsParser
from layman.config import BareConfig
print(BareConfig(config=ArgsParser()['config'], read_configfile=True)['make_conf'])
ENDLAYTWO
	then	return
	fi
	python - "${@}" -l 2>/dev/null <<ENDLAYONE
from layman.config import Config
print(Config()['make_conf'])
ENDLAYONE
}

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=

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

GetOverlays() {
	set -f
	eval "set -- ${layman_make}"
	for i
	do	[ -n "${i:++}" ] && test -r "${i}" || continue
		j=`PORTDIR_OVERLAY=
		. "${i}"
		printf '%s' "${PORTDIR_OVERLAY}x"`
		PushEscaped layman_overlays "${j%x}"
	done
}

Init() {
	Statusline "`gettext 'Analyzing'`"
	ClearUpdateArgs
	GetOverlays
	AddLocalMethods
}

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

Remove() {
	Init
	set -f
	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
