#!/bin/bash

meb="${HOME}/.useflagger_1_ebuilds"
auf="${HOME}/.useflagger_2_alluseflags"
uuf="${HOME}/.useflagger_3_undocumenteduseflags"
log="${HOME}/.useflagger_4_log"
temp_files="${meb} ${auf} ${uuf} ${log}"

advise() {
	echo "This tool requires app-portage/euses to be"
	echo "installed to work."
	echo "To install euses, get the latest ebuild at"
	echo "http://www.xs4all.nl/~rooversj/gentoo/ and"
	echo "do: \`emerge euses'"
	exit
}

initialize() {
	which euses &> /dev/null || advise
	source /etc/make.conf
	portdir="${PORTDIR}"
	portdir_overlay="${PORTDIR_OVERLAY}"
}

color_setup() {
	c_blue=$'\e[34;01m'
	c_cyan=$'\e[36;01m'
	c_green=$'\e[32;01m'
	c_normal=$'\e[0m'
	c_red=$'\e[31;01m'
	c_yellow=$'\e[33;01m'
	grep_color='--colour=always'
}

pre_clean() {
	echo " ${c_green}*${c_normal} Resetting temporary files ..."
	for i in ${temp_files}
	do
		[ -f ${i} ] && rm ${i}; touch ${i}
	done
	date >> ${log}
}

get_ebuilds() {
	echo " ${c_green}*${c_normal} Generating a list of all ebuilds ..."
	echo " ${c_green}*${c_normal}	in ${c_cyan}${portdir}${c_normal}"
	echo ${portdir}/*-*/*/*.ebuild >> ${meb}
	echo " ${c_green}*${c_normal}	in ${c_cyan}${portdir_overlay}${c_normal}"
	echo ${portdir_overlay}/*-*/*/*.ebuild >> ${meb}
	meb_lines="$(wc -w ${meb} | cut -f 1 -d ' ')"
	echo " ${c_green}*${c_normal} ${meb_lines} ebuilds to consider..."
}

get_flags() {
	echo " ${c_green}*${c_normal} Extracting use flags from ebuilds ..."
	for i in $(< ${meb})
	do
		( source ${i} 2>/dev/null
		for j in $IUSE
		do
			echo "${i} ${j}" >> ${auf}
		done )
	done
	auf_lines="$(wc -l ${auf} | cut -f 1 -d ' ')"
	echo "${auf_lines} are used in ${meb_lines} ebuilds."
}

euses_1_4() {
	for i in ${unique_use_flags}
	do
		euses ${i} >> ${log} || echo ${i} >> ${uuf}
	done
}

euses_1_5() {
	euses ${unique_use_flags} | sort | uniq | grep 'No matches for: ' | grep -oe '[[:alnum:]]*$' >> ${uuf}
}

test_flags() {
	unique_use_flags="$(cut -f 2 -d ' ' ${auf} | sort | uniq)"
	unique_use_flags_no="$(echo "${unique_use_flags}" | wc -w - | cut -f 1 -d ' ')"
	echo " ${c_green}*${c_normal} Running ${unique_use_flags_no} use flags through euses..."
	if [[ "1.4" < $(euses | tail -n 1 | cut -f 2 -d ' ') ]]
	then
		euses_1_5
	else
		euses_1_4
	fi
}

show_uuf() {
	echo " ${c_green}*${c_normal} The use flags not (yet) described in use.[local.]desc are:"
	for i in $(cat ${uuf})
	do
		grep "${i}\$" ${auf} | grep ${grep_color} ${i}
	done
}

ask_and_clean() {
	echo " ${c_green}*${c_normal} These are the temporary files I use:"
	for i in ${meb} ${auf} ${uuf} ${log}
	do
		if [[ -f ${i} ]]; then
			echo "${i}: $(wc -l ${i} | cut -f 1 -d ' ') lines"
		else
			echo "${i} (not found)"
		fi
	done
	echo -n " ${c_yellow}*${c_normal} Do you want to reuse these files? [Yn] "
	read ask_clean
	echo
	if [[ ${ask_clean} = "n" ]]
	then # clean everything:
		pre_clean
		get_ebuilds
		get_flags
	else # reuse everything but UUF:
		[ -f ${uuf} ] && rm ${uuf}; touch ${uuf}
	fi
}

isatty() {
        echo -n " * Do you want to go interactive? [yN] "
        read ask_interactive
        echo
        if [[ ${ask_interactive} = "y" ]]
	then
		color_setup
		ask_and_clean
	else
		echo " * Going non-interactive..."
		pre_clean
		get_ebuilds
		get_flags
	fi
}

initialize
isatty
test_flags
show_uuf


syntax highlighted by Code2HTML, v. 0.9.1