#!/bin/bash
#
# Usage: guses <regexp>
#
# Greps /usr/portage/profiles/use[.local].desc for regexp
# and displays formatted output. Copyright JeR 2005.
#
# TODO: 1) build a database of USE flags from both Portage files
#       2) and search that instead...

function printusage() {
	echo -e "\033[1;36mUsage: \033[0;37mguses [<regexp>|-h|--help]"
	echo -e "\033[1;36mNote: \033[0;37mReally only regular expressions that match USE flags are useful."
}

function grepuses () {

declare -x GREP_COLOR='1;32'
USEDESC=`grep -e $* /usr/portage/profiles/use.desc | grep -ve '^# '`
[[ $USEDESC == "" ]] || {
	FOUNDIT=1
	echo -e "Found '\033[1;37m$*\033[0;37m' in \033[1;36muse.desc\033[0;37m:" ;
	USEDESCOUT=`echo "$USEDESC" | grep --colour=always -e '^[[:alnum:]]' -`;
	echo "$USEDESCOUT";
}
USELOCALDESC=`grep -e $* /usr/portage/profiles/use.local.desc | grep -ve '^# '`
[[ $USELOCALDESC == "" ]] || {
	FOUNDIT=1 ; 
	echo -e "Found '\033[1;37m$*\033[0;37m' in \033[1;36muse.local.desc\033[0;37m:" ;
	USELOCALDESCOUT=`echo "$USELOCALDESC" | GREP_COLOR='1;32' grep --colour=always -e '^[[:graph:]]' -`;
	        echo "$USELOCALDESCOUT" ;
		}
[[ $FOUNDIT == 1 ]] || { echo; echo -e "Sorry, couldn't find '\033[1;37m$*\033[0;37m' in \033[1;36muse.\033[0;36m[local.]\033[1;36mdesc\033[0;37m. :("; }
declare +x GREP_COLOR 
}

if ! [[ $* == "" || $* == "-h" || $* == "--help" || $* == "-" ]] ;
	then grepuses "$1";
	else printusage;
fi

