# .bash_aliases
# general-use aliases, functions and more
#
# derived from .config = general-use ksh configuration file 
# that was constructed for local sgi octanes
#
# .bash_aliases_pers
# customized personal configuration file, which if existing in $HOME
# is executed after .bash_aliases
#
# both .bash_aliases and .bash_aliases_pers are sourced from .bashrc
#
#
# NOTE----NOTE----NOTE
#	some functions/aliases may not work with macosx name with embedded 
#	spaces -- need to check for this
#
#
# set here:
# stty			#terminal stuff
# PS1			#customized line leader
# shopt -s 		#[histappend cdspell cmdhist checkwinsize xpg_echo]
# set -o 		#[vi ignoreeof]
# PROMPT_COMMAND	#history -a: each command when issued appended to file
# HISTFILE, HISTSIZE, HISTCONTROL
# HOSTFILE		#/etc/hosts for host searches
# MAILCHECK		#blank it so no mailcheck (no mail on pbg4)
# ENSCRIPT		#set pretty-printer for Courier 11 font
# DIRSTACK, DIRMAX	#variables for ksh-style directory functions
# aliases, functions
# 	.., c, d, pp, pop, push, back, cd=c	#change directory/DIRSTACK
#	lr, li, ,lit, l, l1, luc, l., lx	#list directory variants 
#	func, funcname		#list functions, function names
# 	can			#safe rm = mv deleted to ~/tmp
#	rm=can,rmxx=/bin/rm	#safe rm/std rm
#	h, r			#history/repeat command
#	H			#grep history file for string=$1
#	cx			#chmod +x
#	chmog			#set mode+owner+group of a file
#	p			#user+process info = who+pwd+ps
#	ps=ps-aux, psxx=/bin/ps	#ps -opt/std ps
#	psgrep			#ps line(s) with string=$1
#	ff			#find file name=$1 under current directory
#	watch			#tail -f $1 = watch file=$1 grow
#	j			#list jobs
#	m			#more
#	cl			#clear
#	restty			#reset tty
#	C			#compile prog.c=$1
#	top			#top with nice options
#	xt24,xt40		#set xterm screen size to 24x80 or 40x80
#	xterm24			#spawn 80x24 xterm, at lower right screen
#	apropos			#filter apropos: limit line length and sort 
#	whatis			#filter whatis: limit line length and sort 
#	dugraph			#du|odd-char-filter|dugraph
#	cdiff			#colordiff

# terminal stuff 
# set the interrupt character to ctrl-c and do clean backspacing.
# set other terminal stuff
# NB: path to tty and stty may be system dependent
if /usr/bin/tty -s
then
	/bin/stty intr '^C' echoe 
	/bin/stty kill '^U'
fi


# set the ps1 prompt - choose your poison - be careful of quoting
# PS1="${PSHEAD}${PWD}> "
# PS1="${PSHEAD}"`echo ${PWD}|sed -e 's/^.*\(\/[^/]*\/[^/]*\/[^/]*\)$/..\1/'`"> "
PS1='\u@\h [\t] `echo ${PWD}|sed -e "s%^.*\(..............................\)$%\1%" -e "s%^[^/]*\(.*\)$% \1%"` (${SHLVL}:\!)\$ '

# setup bash options
shopt -s histappend	#append new command history to history file
shopt -s cdspell	#correct minor spelling errors in cd arg
shopt -s cmdhist	#
shopt -s checkwinsize	#check window size after command exit
shopt -s xpg_echo	#expand std \x abbrev in echo arg
set -o vi		#vi key bindings in history editing/readline input
set -o ignoreeof	#don't exit on eof=^D

# append new stuff to the command history file each time the 
# PS1 prompt is issued
# this keeps current the single command history file used by each of a 
# user's terminals run under bash
# if histappend is set, then the history file is not rewritten, only appended
PROMPT_COMMAND='history -a'
export PROMPT_COMMAND

# sets of miscellaneous variables
#convenient but NOT SECURE to have HISTFILE for root
export HISTFILE=$HOME/.bash_history_$USER
export HISTSIZE=500
export HISTCONTROL=ignoredups	#do not add duplicated commands to history
export HOSTFILE=/etc/hosts	#use /etc/hosts for host lookup

# consider later depending on configuration
export MAILCHECK=""
# MAIL
#/usr/bin/mailx -H
#/usr/bin/news -n
# REMOTEHOST
# ENSCRIPT		#set pretty-printer for Courier 11 font
ENSCRIPT=" -fCourier11"
export ENSCRIPT

#----------------------------------------------------------------------
# NOTE----NOTE----NOTE
# the following is directly from my ksh configuration file;
# because bash has pushd, popd, and dirs, it could be done more simply
# and elegantly -- but do it later if ksh functions work

# at startup of each shell, setup dirstack and dirmax, from dirstore;
unset DIRSTACK
((yy = 0))
if [ \( "x$DIRSTORE" = "x\n" \) -o \( "x$DIRSTORE" = "x" \) ]
then
	DIRSTACK=~;export DIRSTACK
	((DIRMAX = 0));export DIRMAX
	DIRSTORE="${DIRSTACK[$DIRMAX]}\n";export DIRSTORE
else
	zz="$DIRSTORE"
	while [ \( "x$zz" != "x\n" \) -a \( "x$zz" != "x" \) -a \( $yy -le 500 \) ]
	do 
		DIRSTACK[$yy]="${zz%%\\n*}"
		DIRMAX=$yy
#debug		echo $yy ${DIRSTACK[$yy]}	
#debug		echo ${zz}
		((yy = yy + 1))
		zz="${zz#*\\n}"
	done 
fi

#pop \- pop [n [m]] - function for pop directory stack item n, or n thru m [default n = m = top]
pop() {
aa=${1:-$DIRMAX}
bb=${2:-$aa}
if [ $DIRMAX -eq 0 ]
then
	echo "Cannot pop - only one entry in DIRSTACK"
	return 1
elif [ \( $DIRMAX -ge 500 \) -o \( $DIRMAX -lt 0 \) ]
then
	echo "Dirstack full or weird value for DIRMAX - reset it and DIRSTACK"
	DIRSTACK=~;export DIRSTACK
	((DIRMAX = 0));export DIRMAX
	DIRSTORE="${DIRSTACK[$DIRMAX]}\n";export DIRSTORE
	return 1	
fi
((zz = 0))
((yy = 0))
DIRSTORE=""
while [ $zz -le $DIRMAX ]
do
	if [ \( $zz -ge $aa \) -a \( $zz -le $bb \) ]
	then
		((zz = zz + 1))
	else
		DIRSTACK[$yy]=${DIRSTACK[$zz]}
		DIRSTORE="${DIRSTORE}${DIRSTACK[$yy]}\n"
		((yy = yy + 1))
		((zz = zz + 1))
	fi
done
if [ $yy -le 0 ]
then
	((DIRMAX = 0))
	DIRSTORE="${DIRSTACK}\n"
else
	((DIRMAX = yy - 1))
fi
}

#push \- push dirname = function for push directory onto stack
push() {
if [ \( $DIRMAX -ge 500 \) -o \( $DIRMAX -lt 0 \) ]
then
	echo "DIRSTACK full or has weird value - reset DIRMAX and DIRSTACK"
	DIRSTACK=~;export DIRSTACK
	((DIRMAX = 0));export DIRMAX
	DIRSTORE="${DIRSTACK[$DIRMAX]}\n";export DIRSTORE
	return 1	
fi
if [ $# -eq 0 ]
then
	((DIRMAX = DIRMAX + 1))
	DIRSTACK[$DIRMAX]=$PWD
	DIRSTORE="${DIRSTORE}${DIRSTACK[$DIRMAX]}\n"
else
	for i in $@
	do
		if [ -d $i ]
		then
			((DIRMAX = DIRMAX + 1))
			DIRSTACK[$DIRMAX]=$i
			DIRSTORE="${DIRSTORE}${DIRSTACK[$DIRMAX]}\n"
		fi
	done
fi
}

#d \- function for list directory stack, current directory last
d() {
((zz = DIRMAX - 15))
zz=${1:-$zz}
if [ \( $zz -gt $DIRMAX \) -o \( $zz -lt 0 \) ]
then
	((zz = 0))
fi
while [ $zz -le $DIRMAX ]
do
	echo $zz ${DIRSTACK[$zz]}
	((zz = zz + 1))
done
}

#c \- c [arg1 [arg2]] = function for cd [arg1 [arg2]] and push directory
c() {
if [ $# -eq 0 ]
then
	builtin cd
else
	builtin cd "$*"
fi
if [ $? -eq 0 ]
then
	push
fi
}

#pp \- pp [n] = function for move directory n to top of stack, and cd to it [default = exchange current and previous directories]
pp() {
((xx = DIRMAX - 1))
xx=${1-$xx}
if [ \( $xx -gt $DIRMAX \) -o \( $xx -lt 0 \) -o \( $DIRMAX -eq 0 \) ]
then
	echo "DIRSTACK has only $DIRMAX entry(ies)."
	return 1
else
	temp=${DIRSTACK[$xx]}
	pop $xx
	c $temp
fi
}

#back \- function for pop top off stack, cd to new top = previous directory
back() {
pp
((temp = DIRMAX - 1))
pop $temp
}
#----------------------------------------------------------------------

#can \- can [-scrfi] filelist = function for front end for rm; protected by query or mv to ../tmp
#modified from: sage, "tricks of the unix masters", sams, 1987, p81
can() {
CAN=$HOME/tmp
if [ ! -d $CAN ]
then
	/bin/mkdir $CAN
fi
usage="usage:    $0  [-s | -c | -ifr ]  filelist"
#	no options = mv filelist to $HOME/tmp
#	-s = show contents of /tmp
#	-c = clean out the /tmp directory
#	-f = rm -f		with query & pause
#	-r = rm -r		with query & pause
#	-i = rm -i"
query="Do you want to do this:
	$0  $@
to exit, hit delete key within 5 seconds"
if [ $# = 0 ]
then
	echo "$usage" >&2
	return 1
fi
case $1 in
	-s)	echo "$CAN:";/bin/ls -al $CAN;return 0;;
	-c)	echo "removing $CAN/*:";/bin/rm -rf $CAN/*;return 0;;
	-i*)	/bin/rm "$@";return 0;;
	-[rf]*)	echo "$query" >&2
		/bin/sleep 5
		/bin/rm "$@";return 0;;
	-*)	echo "$usage" >&2;return 1;;
esac
/bin/mv $@ $CAN
}


# p \- alias for "who + pwd + ps" = user and process info
alias p="/usr/bin/who am i;pwd;/bin/ps"

# cx \- alias for "chmod +x" = change mode to executable
# kernighan-pike, "the unix programming environment", prentice-hall, 1984, p 82
alias cx="/bin/chmod +x"

#h \- alias for "fc -l", shorter mnemonic for history
alias h='fc -l'

#r \- alias for "fc -s", mnemonic for run previous [history-numbered] command
alias r='fc -s'

#restty \- alias for reset of stty to .profile values
alias restty='/bin/stty sane;/bin/stty erase  kill  echoe'

#rm, rmxx \- alias rm to "can", rmxx to /bin/rm
alias rm=can
alias rmxx=/bin/rm

#cd \- alias std UNIX cd command to local variant, c
alias cd="c"

#lr \- alias for "ls -RF", for nice listing of tree
alias lr="/bin/ls -RF"

#li \- alias for "ls -il", for long ls listing
alias li="/bin/ls -l"

#lit \- alias for "ls -ilt", for time-sorted long ls listing
alias lit="/bin/ls -lrt"

#l \- alias for "ls -F", listing with dir:exec marked /:*
alias l="/bin/ls -F"

#l1 \- alias for "ls -adF [$*]", like "l" but no descent into subdir if $*
alias l1="/bin/ls -adF"

#luc \- function for "ls -adF .* [!a-z]*" = list . and uc files
luc() { /bin/ls -adF $@ .* [!a-z]* ;}

# various aliases and functions from bash sources examples, etc., by:
# b. fox, w. sanchez, c. ramey

#j \- alias for "jobs -l"
alias j='jobs -l'

#.. \- alias for "cd .."
alias ..='cd ..'

#m \- alias for "more", with color esc seq recognized
alias m='more -R'

#cl \- alias for "clear"
alias cl='clear'

#l. \- function for "ls -dF $1 .[a-zA-Z0-9-_]*" = ls for dot files
l.() { ls -dF $* .[a-zA-Z0-9-_]* ;}

#ps \- alias for "ps -auxc" = full ps; psxx = /bin/ps
alias ps='/bin/ps -auxc'
alias psxx='/bin/ps'

#psgrep \- function for extraction of ps (process) info for file=$1
psgrep() { ps -auxc | egrep "$1" | egrep -v egrep ;}

#ff \- function for find name=$1 under current directory
ff() { find -x . -name "$1" -print 2>/dev/null ; }

#watch \- function for "tail -f[$1 | nohup.out]"
watch() { if [ $# -ne 1 ] 
	then tail -f nohup.out
	else tail -f $1
	fi }

#chmog \- function for set of mode+owner+group of a file
chmog() { if [ $# -ne 4 ]
	then echo "usage: chmog mode owner group file"; return 1
	else chmod "$1" "$4"; chown "$2" "$4"; chgrp "$3" "$4"
	fi }

#lx \-  alias for list executable files, 5 columns, sorted across 80-wide line:
alias lx='l -1 | egrep \\* | pr -t -5 -l1 -w80'

#C \- function for compile C programs (C adm.c expands to cc adm.c -o adm).
C() { cc "$1" -o `basename "$1" .c` ;}

#H \- alias for grep of history list
alias H='history | egrep '

#top \- alias for nice set of options
alias top='top -t -F -ocpu -Orsize -R -s2  -n20'

#xt40 \- alias for change to 80x40 xterm window size
alias xt40='echo "[8;40;80t\c"'

#xt24 \- alias for change to 80x24 xterm window size
alias xt24='echo "[8;24;80t\c"'

#xterm24 \- alias for spawn of 80x24 xterm window size, at lower right screen
alias xterm24='xterm -geo 80x24-60-40 &'

#apropos \- function for filter of apropos output: limit line length and sort 
apropos() {
/usr/bin/apropos $* | whatis-filter-jar
}

#whatis \- function for filter of whatis output: limit line length and sort 
whatis() {
/usr/bin/whatis $* | whatis-filter-jar
}

#dugraph \- alias for du|macosx-odd-char-filter|dugraph
alias dugraph='du | tr "\200-\377" "?"  | dugraph'

#cdiff \- alias for colordiff
alias cdiff=colordiff

#func \- alias for display of function body
alias func='typeset -f'

#funcname \- alias for display of function name only
alias funcname='typeset -F'

