Source

scripts / overload / overload

#!/bin/bash
## Overload allow you to overload command functionality on top of standard linux commands.
## Configuration is done by definining functions in ~/.overloadrc or even just in bashrc.
## @author khamer

SCRIPT="$0"
CMD="${SCRIPT##*/}"
if [ $CMD == 'overload' ]; then
	CMD="$1"
	shift
	ORIGINAL="$CMD"
else
	ORIGINAL=$(which -a $CMD | grep -v "$SCRIPT" | head -n 1);
fi

## Converts FUNC to underscore_format.
FUNC="$CMD"
for ARG in "$@"; do
	FUNC="${FUNC}_$ARG"
done

OLDFUNC="OLDFUNC"
SHIFTS=0
START=$#

if [ -e ~/.overloadrc ]; then
	source ~/.overloadrc
fi

while [[ "$(type -t $FUNC)" != "function" ]] && [[ "$FUNC" != "$OLDFUNC" ]]; do
	OLDFUNC="$FUNC"
	FUNC=${FUNC%_*}
	SHIFTS=$(($SHIFTS+1))
	START=$(($#-$SHIFTS))
done
START=$(($START+1))

if [ $FUNC == $OLDFUNC ]; then
	$ORIGINAL "$@"
else
	$FUNC "${@:$START:$SHIFTS}"
fi