#!/bin/sh
if [ $# -eq 0 ] ; then
set -- -h # print usage on 0 arguments
fi
if [ $# -ge 1 ] ; then
case $1 in
-h|--help|-help|-H)
echo "Usage: aka alias show stored aliases"
echo " aka alias NAME CMD... store an alias for CMD"
echo " aka NAME [...] run CMD, optionally with more arguments"
exit 0
;;
esac
fi
aliasFile=${AKA_FILE:-./.aka}
# Is this an alias creation? If so, we should have at least 3 arguments,
# such as:
#
# $ aka alias pager more
# \ \ \ \
# $0 $1 $2 $3
#
#
if [ $# -ge 3 ] && [ $1 = alias ] ; then
# cmd='pager'
cmd=$2
# make 'more' the new $1
shift 2
# remove previous aliases for 'pager'
[ -e "$aliasFile" ] && grep -v "alias $cmd=" "$aliasFile" > "$aliasFile"~
echo "alias $cmd='$*'" >> "$aliasFile"~ && mv "$aliasFile"~ "$aliasFile"
exit 0
fi
# Load up aliases...
[ -e "$aliasFile" ] && . "$aliasFile"
# ...and execute command
eval $*