#!/bin/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 <martin@mvath.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.36.7).
set -u

if eix_funcs=`cat "/usr/share/eix/eix-functions" 2>/dev/null`
then	eval "$eix_funcs"
else	echo "${0##*/}: cannot find eix-functions.sh" >&2
	exit 1
fi
ReadFunctions

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

Explanation: There might be (security) reasons to avoid adding layman overlays
to the regular portage repositories. If you avoid this, they are also
not added to your eix database and thus also cannot be found with eix.
However, you might want to add/remove them temporarily to your eix database
without changing your configuration. 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 -- a $layman_make"
	shift
	[ $# -eq 0 ] && { set -- a "`gettext \
		'none (i.e. no readable layman make.conf specified)'`"
		shift
	}
	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 - ${1+"$@"} -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 - ${1+"$@"} -l 2>/dev/null <<ENDLAYONE
from layman.config import Config
print(Config()['make_conf'])
ENDLAYONE
}

CalcLaymanMake() {
	l=`PrintLaymanMake ${1+"$@"}` || 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 ${1+"$@"}
eval "set -- a $opt"
shift
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;;
	'?')	Exit 1;;
	*)	Usage 0;;
	esac
done
[ $OPTIND -le 1 ] || shift `Expr $OPTIND - 1`
$statusline || {
	NOSTATUSLINE=true
	export NOSTATUSLINE
}

GetOverlays() {
	set -f
	eval "set -- a $layman_make"
	shift
	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 "`pgettext 'Statusline eix-layman' '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-help}
[ $# -gt 0 ] && shift

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

Exit 0
