#!/bin/bash
# bash functions and aliases that may or may not be useful...       (c) JeR 2005

#Config:
toolpath="/keeps/gentoo/tools"
utilpath="${toolpath}/utils.sh"
privpath="${toolpath}/private.sh"

ebuildswithouthppa() {
	for i in $(cat /keeps/useflagger_1_ebuilds); do
		( source ${i} &>/dev/null
		echo "${i} ${KEYWORDS}" | grep -v 'hppa' ;)
	done | tee -a /keeps/ebuilds_without_hppa
}

quickpkgall() { # quickpkgall -- Runs quickpkg on /var/db/pkg/*/*
	( cd /var/db/pkg
	quickpkg */*; )
}

emergeinfo() { #
	local outputfile="/keeps/gentoo/bugs/emerge.info-$(hostname)"
	{ date; emerge info 2>&1 ;} | \
		tee ${outputfile}
	echo
	ftpxs4all gentoo/bugs ${outputfile}
}

piduser() { # piduser -- uses up pids until it hits a low one
	i=0
	j=1
	while test ${j} -gt ${i} ; do
		i=${j}
		j=$( bash -c 'echo $$' )
	done
	echo "${i} -> ${j}"
}

sandboxes() { # sandboxes SECONDS -- Output all sandbox activity every SECONDS seconds
	local mysleep=${1}
	local psout=""
	local psout1=""
	local myout=""
	test -z ${mysleep} && mysleep=1
	while sleep ${mysleep}; do
		psout1="${psout}"
		psout="$(ps aux | grep  [s]andbox | grep -o '\[.*\]')"
		if test "${psout1}JeR" = "${psout}JeR" -a -n "${psout}"; then
			myout='.'
		elif test -z "${psout}"; then
			myout=''			
		else
			myout="\n${psout}"
		fi
		echo -en "${myout}"
	done
}

epackage() { # epackage PATTERN -- return all CAT/PKG that contain PATTERN
	( source /etc/make.conf
	cd ${PORTDIR}
	for i in */* ; do
		echo ${i}
	done | grep -v 'distfiles\|eclass\|header\.txt\|licenses\|metadata\|packages\|profiles\|scripts\|skel\.[[:alnum:]]' | grep ${*}
	cd ${PORTDIR_OVERLAY}
	for i in */* ; do
		echo ${i}
	done | grep ${*} ) | sort | uniq; }

ecategoryname() { # ecategoryname CAT/PKG -- return only the category name CAT
	for i in ${*} $(cat /dev/stdin); do
		echo ${i%/*}
	done
}

epackagename() { # epackagename CAT/PKG -- return only the package name PKG
	for i in ${*} $(cat /dev/stdin); do
		echo ${i#*/}
	done
}
stripversion() { # stripversion CAT/PKG-VER -- return only the canonical name CAT/PKG
	for i in ${*} $(cat /dev/stdin) ; do
		( this=${i%%-r*}
		echo ${this%-[0-9]*}; )
	done
}

allpackages() { # allpackages -- Returns all installed packages as CAT/PKG-VER
	( cd /var/db/pkg
	echo */* | tr ' ' '\n' | sort; )
}

allpackagesemerge() { # allpackagesemerge -- Returns all installed packages as =CAT/PKG-VER
	( cd /var/db/pkg
	echo */* | tr ' ' '\n' | sed 's/^/\=/' | sort; )
}

allpackagesonthishostftp() {
	local mypackagefile="/keeps/gentoo/bugs/allpackages-$(hostname)"
	allpackages | \
		tee ${mypackagefile} | \
			wc -l
	ftpxs4all gentoo/bugs ${mypackagefile}
}

extractebuilds() { # extractebuilds -- Returns things that look like CAT/PKG-VER from stdin
	grep -o '[[:alnum:]]*-[[:alnum:]]*\/[[:alnum:].-]*'
}

matchebuilds() { # matchebuilds CAT/PKG -- returns ebuilds paths matching CAT/PKG
	local i
	test -n "${1}" && local ebuildpaths=$( (
		source /etc/make.globals
		source /etc/make.conf
		for i in ${PORTDIR} ${PORTDIR_OVERLAY}; do
			echo ${i}/${1}/*.ebuild;
		done; )	| grep -v '*' )
		test -n "${ebuildpaths}" && \
			local usefulpaths=$(for i in ${ebuildpaths}; do echo $(dirname ${i}); done | sort | uniq)
		test -n "${usefulpaths}" && \
			for i in ${usefulpaths}; do
				# ls -v doesn't work for ebuilds... :-\
				ls -v ${i}/*.ebuild
			done
}

matchebuildskeywords() { # matchebuildskeywords CAT/PKG -- Returns keywords per ebuild
	if test -z ${1}; then
		echo " * Need a CAT/PKG to work with here"
	else
		local f
		local myforest=$( (
			source /etc/make.globals
			source /etc/make.conf;
			echo "${PORTDIR} ${PORTDIR_OVERLAY}") )
		for f in ${myforest}; do
			( cd ${f}
			treebuilds="$( echo ${1}/*.ebuild 2>/dev/null | \
				grep -v '\*' )" 
			for t in ${treebuilds}; do
				# Find bash equivalents here:
				mycategory=$(echo ${t} | cut -f1 -d'/')
				# And here:
				myebuildplusversion=$(echo ${t} | cut -f3 -d'/' | sed -e 's|.ebuild||g')
				echo -n "${mycategory}/${myebuildplusversion} "
				source $t &>/dev/null
				echo "${KEYWORDS}"
			done; )
		done
	fi
}

keywordandgenlop() {
	echo " * Keyword status for ${1} :"
	for i in matchebuildskeywords 'genlop -i'; do
		${i} ${1}
		echo
	done
}

matchebuildslatestedit() { # matchebuildslatestedit CAT/PKG -- Opens the latest ebuild for CAT/PKG in EDITOR
	${EDITOR} $(matchebuildslatest ${1});
}

allebuilds() { # allebuilds -- Returns all ebuilds in PORTDIR and PORTDIR_OVERLAY 
	( source /etc/make.globals; source /etc/make.conf;
	test -z "${PORTDIR}" && PORTDIR="/usr/portage"
	test -z "${PORTDIR_OVERLAY}" && PORTDIR_OVERLAY="/usr/local/portage"
	for i in ${PORTDIR} ${PORTDIR_OVERLAY}; do
		test -e "${i}" \
			&& echo ${i}/*-*/*/*.ebuild \
				| sed -e 's/ /\n/g';
	done; )
}

packagekeywords() { # packagekeywords cat-egory/package [~]arch -- Add to package.keywords
	local yesno
	if test -n "${1}" -a -z "${2}"; then
		echo -n "\"${1}\" >> /etc/portage/package.keywords? [yN]"
		read yesno
	elif test -n "${1}" -a -n "${2}"; then
		echo -n "\"${1} ${2}\" >> /etc/portage/package.keywords? [yN]"
		read yesno
	else
		echo "Usage: packagekeywords cat-egory/package [[~]arch]"
	fi
	if test "${yesno}" = 'y'; then
		echo "${1} ${2}" >> /etc/portage/package.keywords
		echo "Done!"
	fi
}

packageuse() { # packageuse cat-egory/package <use flag> -- Add to package.use
	if test -n "${1}" && test -n "${2}"; then
		echo "\"${1} ${2}\" >> /etc/portage/package.use"
		echo "${1} ${2}"    >> /etc/portage/package.use
	else
		echo "Usage: packageuse cat-egory/package <use flag>"
	fi
}

packageunmask() { # packageunmask cat-egory/package -- Unmask package through package.unmask
	if test -n "${1}"; then
		echo "\"${1}\" >> /etc/portage/package.unmask"
		echo "${1}"    >> /etc/portage/package.unmask
	else
		echo "Usage: packageunmask cat-egory/package"
	fi
}

packagemask() { # packagemask cat-egory/package -- Mask package through package.mask
	if test -n "${1}"; then
		echo "\"${1}\" >> /etc/portage/package.mask"
		echo "${1}"    >> /etc/portage/package.mask
	else
		echo "Usage: packagemask cat-egory/package"
	fi
}

makealiases() { # makealiases -- Set up emerge aliases depending on machine type
		# Might have used `arch' but it returns only 'ppc' for
		# a Power Macintosh. Extend as necessary...
	case $(uname -m) in
		i586|i686)
			arch="x86"
			alias ls="ls --color=always"
			alias swapoffon="swapoff -av && swapon -av"
			;;
		parisc)
        		arch="hppa"
			alias ls="ls --color=always"
		        alias emergeunstablex86="ACCEPT_KEYWORDS=\"~x86\" emerge"
        		alias emergex86="ACCEPT_KEYWORDS=\"x86\" emerge"
			alias swapoffon="swapoff -av && swapon -av"
			;;
		'Power Macintosh')
			arch="ppc"
	        	subarch="-macos"
	        	alias emergeppc="ACCEPT_KEYWORDS=\"${arch}\" emerge"
        		alias emergeunstableppc="ACCEPT_KEYWORDS=\"~${arch}\" emerge"
	        	alias emergeppcmacos="ACCEPT_KEYWORDS=\"${arch}${subarch}\" emerge"
			alias top="top -L"
			;;
	esac
	alias emergeunstable="ACCEPT_KEYWORDS=\"~${arch}${subarch}\" emerge"

	# arch-independent emerge aliases:
	alias emergevuaDworld="{ test -f /etc/portage/package.mask && cat /etc/portage/package.mask ; emerge -vuaD world; }"
	alias emergevuaDworldN="{ cat /etc/portage/package.mask ; emerge -vuaDN world; }"

	# Do more with screen:
	alias screenemergelog="screen -t emerge.log tail -f -n 20 /var/log/emerge.log"
	alias screenemergesynclog="screen -t emerge-sync.log tail -f -n 20 /var/log/emerge-sync.log"
	alias screenless="screen less"
	alias screenman="screen man"
	alias screensujeroen="screen -t su.jeroen su - jeroen"
	alias screentailf="screen tail -n 20 -f"
	alias screenvi="screen vi"

	# '-t raw -Ub -r8000 -c1' is the magic (de|en)coding sequence
	# for playing (ivam2's) raw ISDN voicemail sound files:
	ulo="-t raw -Ub -r8000 -c1"
	alias ulawplay="play ${ulo}"
	alias ulawsox="sox ${ulo}"
}

mycommands() { # mycommands [<command>] -- Describes all commands or just <command>
	if test -z "${1}"; then
		grep ^[[:alnum:]]*\(\)\ {\ \# ${utilpath} ${privpath} \
			| sed 's/^.*# //' \
			| sort
	elif test "${1}" = "editu"; then
		${EDITOR} ${utilpath}
	elif test "${1}" = "editp"; then
		${EDITOR} ${privpath}
	else
		grep ^${1}\(\) ${utilpath} ${privpath} | sed 's/^.*# //' || echo "$Command \`{1}' not found"
	fi
	
}
myaliases() { # myaliases [<alias>] -- Describes all aliases or just <alias>
	if test -z "${1}"; then
		grep ^[[:space:]]*alias ${utilpath} ${privpatih} | \
		sed 's/^[[:space:]]*alias //'
	else
		grep "^[[:space:]]*alias ${1}" ${utilpath} ${privpath} | \
		sed 's/^.*alias //' || echo "$Command \`{1}' not found"
	fi
}

myresources() { # myresources [-v] -- Reload all commands in this file (-v for verbose)
	if test "$1" = "-v"; then
		test -f "${utilpath}" && source ${utilpath} &&
			echo "Sourcing... ${utilpath}"
		test -f "${privpath}" && source ${privpath} && \
			echo "Sourcing... ${privpath}"
	else
		test -f "${utilpath}" && source ${utilpath}
		test -f "${privpath}" && source ${privpath}
	fi
}

makealiases
test -f "${privpath}" && source ${privpath}

#ps hp ${$} -o args
#(set -o -H; !-1;)
#!/bin/sh

