# .bashrc
# $Id: .bashrc,v 1.9 2006/01/09 20:27:53 jlarsen Exp $
# Description:  This bashrc file is setup to work with Linux, Solaris, and cygwin.  The same file
# is used on all these platforms so there is only one file to maintain.  This file lives in the
# ~/bin directory.  A symbolic link ~/.bashrc is linked to this file.  This script calls
# ~/bin/.bashrc.site if it exists for site specific setup.  This should also be a symbolic link
# to the appropriate file, such as bashrc.work, bashrc.home, etc.

#######################################################################################
# Beginning of DO ONLY ONCE section
# The following stuff only gets done the first time the login shell is started
if [  ! $ENV_DONE ]; then
   export ENV_DONE=TRUE

# Configure path based on OS type
CHECK_OS=`uname -a | awk '{print $3}'`
case $CHECK_OS in

5.*) #Solaris 2.* found
export PATH=".\
:/h/jlarsen/bin\
:/h/jlarsen/vim/bin\
:/usr/local/bin\
:/usr/bin\
:/bin\
:/usr/sbin\
:/opt/bin\
:/usr/ucb\
:/opt/rational/clearcase/bin\
:/usr/atria/bin\
:/u/scripts/testscripts\
:/u/scripts\
:/usr/local/bin"
;;

2.*) #Linux 2.* kernels
export PATH=".\
:~/bin\
:/usr/sbin\
:/sbin\
:/usr/local/bin\
:/bin\
:/usr/bin\
:/usr/X11R6/bin\
:/usr/games\
:$PATH"
;;

3.*) #Linux 3.* kernels
export PATH=".\
:~/bin\
:/usr/sbin\
:/sbin\
:/usr/local/bin\
:/bin\
:/usr/bin\
:/usr/X11R6/bin\
:/usr/games\
:$PATH"
;;

4.*) #Linux 4.* kernels
export PATH=".\
:~/bin\
:/usr/sbin\
:/sbin\
:/usr/local/bin\
:/bin\
:/usr/bin\
:/usr/X11R6/bin\
:/usr/games\
:$PATH"
;;

5.*) #Linux 5.* kernels
export PATH=".\
:~/bin\
:/usr/sbin\
:/sbin\
:/usr/local/bin\
:/bin\
:/usr/bin\
:/usr/X11R6/bin\
:/usr/games\
:$PATH"
;;

1.5.*) # cygwin 1.5.* running on a Windows host
export PATH=".\
:~/bin\
:/usr/sbin\
:$PATH"
;;

*) #Unknown OS 
echo "Unknown OS type detected in .bashrc: `uname -a | awk '{print $1,$3}'`"
;;

esac

fi
# End of DO ONLY ONCE section
#######################################################################################

# Configure bash to use vi editing of the command line
set -o vi

# Configure CVS so that :ext: uses ssh and configure default value for repository location
export CVS_RSH=ssh
export CVSROOT=":ext:jlarsen@larsen-family.us:/home/cvsroot"

# Declare that vi is the default editor
export EDITOR=vi

# Set bash command history to ignore duplicate command lines and not put commands starting with blank into history.
export HISTCONTROL=ignoreboth

# Initialize HISTIGNORE with commands we don't want going into the bash history.  Only exact matches are filtered.
HISTIGNORE="l:ll:llt:llh:cdb:b:r:exit:env:date:.:..:...:....:.....:pwd:cfg:rb:eb:!!:ls:fg:bg:cd ..:h:mc:cd:dfh:df:w:sudo bash:xclock&"

# Clear any aliases setup by the system.  We only want our own, but only do it once
# in case some are created on the command line that we want to keep.
unalias -a	#Remove all aliases

# Setup where man pages are located
export MANPATH="/usr/share/man:/usr/local/man:/opt/man"

# Include the dir_array utility.  This utility required for many of the aliases to work.
# dir_array requires bash version 2.x or higher.  Only source it if on bash 2.x.  Set
# $HISTIGNORE before calling dir_array because it adds to $HISTIGNORE
case $BASH_VERSION in
	2*) # bash 2.x
		if [ -e ~/bin/dir_array ]; then
			source ~/bin/dir_array
		else
			source ~jlarsen/bin/dir_array
		fi
	;;

	3*) # bash 3.x
		if [ -e ~/bin/dir_array ]; then
			source ~/bin/dir_array
		else
			source ~jlarsen/bin/dir_array
		fi
	;;

	4*) # bash 4.x
		if [ -e ~/bin/dir_array ]; then
			source ~/bin/dir_array
		else
			source ~jlarsen/bin/dir_array
		fi
	;;

	5*) # bash 5.x
		if [ -e ~/bin/dir_array ]; then
			source ~/bin/dir_array
		else
			source ~jlarsen/bin/dir_array
		fi
	;;
esac


# Setup my favorite aliases of a global nature.  Site specific aliases are done later.
alias dfh='date; df -h | grep -v loop | grep -v tmpfs | grep -v udev'
alias cdb="a \".\" no_show; a ~/bin no_show; l"
alias cls=clear
alias vbm="VBoxManage"
alias cp="cp -p"
alias date="date '+%a-%h-%d%t%r'"
alias du="du -k"
alias eb="vi ~/.bashrc"
alias ec="vi ~/.cshrc"
alias rb="exec bash"
alias ps=/bin/ps
alias uname="uname -a"
alias who="who -HT"
alias traceroute="traceroute -I"
# RCS uses UTC by default.  Override that to use local time for checkins.  See manpage for "co"
alias ci="ci -zLT"

# Source site specific file if it exists
if [ -e ~/bin/.bashrc.site ]; then
   source ~/bin/.bashrc.site
fi


# Setup the prompt and other aliases based on OS version and ClearCase use
if [ $CLEARCASE_ROOT ]; then
	VIEW="`basename $CLEARCASE_ROOT` - "
else
	VIEW=""
fi
CHECK_OS=`uname -a | awk '{print $3}'`
#echo "debug> setting up the prompt: $CHECK_OS"
case $CHECK_OS in
	5.8) #Solaris 2.8 found
		stty erase 
      # Check if this is a root shell
		UID_VALUE=`id | awk '{print $1}'`
		if [ "$UID_VALUE" = "uid=0(root)" ]; then
			PS1='\e[1;31m\]\e[7m[\h - ${VIEW}\u ] [\w]\e[m\]\e[27m\n[\!]$ '
		else
			PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
		fi
		alias v="/usr/bin/vi"
		alias vi="~/vim/bin/gvim"
		alias vd="~/vim/bin/gvim -d"
		alias vim="~/vim/bin/vim"
	;;

	5.7) #Solaris 2.7 found
		stty erase 
		PS1='`tput smso`[\h - ${VIEW}\u ] [\w]`tput rmso`\n[\!]$ '
		alias v="/usr/bin/vi"
		alias vi="~/vim/bin/gvim"
		alias vd="~/vim/bin/gvim -d"
		alias vim="~/vim/bin/vim"
	;;

	5.5.1) #SunOs 5.5.1 ie. outback
		stty erase 
		PS1='`tput smso`[\h - ${VIEW}\u ] [\w]`tput rmso`\n[\!]$ '
		alias v="/usr/bin/vi"
		alias vi="~/vim/bin/gvim"
		alias vd="~/vim/bin/gvim -d"
		alias vim="~/vim/bin/vim"
	;;

	2.4.*) #Linux 2.4 kernels
#echo "debug> Inside 2.4"
		stty erase 
      # Check if this is a root shell
		UID_VALUE=`id | awk '{print $1}'`
		if [ "$UID_VALUE" = "uid=0(root)" ]; then
			PS1='\e[1;31m\]\e[7m[\h - ${VIEW}\u ] [\w]\e[m\]\e[27m\n[\!]$ '
		else
			PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
		fi
	;;

	2.6.*) #Linux 2.6 kernels
#echo "debug> Inside 2.6"
#		stty erase 
      # Check if this is a root shell
		UID_VALUE=`id | awk '{print $1}'`
		if [ "$UID_VALUE" = "uid=0(root)" ]; then
			PS1='\e[1;31m\]\e[7m[\h - ${VIEW}\u ] [\w]\e[m\]\e[27m\n[\!]$ '
			#PS1='\e[7m\e[1;31m\][\h - ${VIEW}\u ] [\w]\e[27m\e[m\]\n[\!]$ '
         #PS1='`tput smso``tput setf 4``tput setb 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setb 7``tput setf 0`\n[\!]$ '
         #PS1='`tput smso``tput setaf 1``tput setab 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setab 7``tput setaf 0`\n[\!]$ '
		else
#echo "debug> Inside 2.6 non root"
			PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
		fi
	;;

	3.*) #Linux 3.* kernels
#		stty erase 
      # Check if this is a root shell
		UID_VALUE=`id | awk '{print $1}'`
		if [ "$UID_VALUE" = "uid=0(root)" ]; then
			PS1='\e[1;31m\]\e[7m[\h - ${VIEW}\u ] [\w]\e[m\]\e[27m\n[\!]$ '
			#PS1='\e[7m\e[1;31m\][\h - ${VIEW}\u ] [\w]\e[27m\e[m\]\n[\!]$ '
         #PS1='`tput smso``tput setf 4``tput setb 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setb 7``tput setf 0`\n[\!]$ '
         #PS1='`tput smso``tput setaf 1``tput setab 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setab 7``tput setaf 0`\n[\!]$ '
		else
#echo "debug> Inside 2.6 non root"
			PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
		fi
	;;

	4.*) #Linux 4.* kernels
#		stty erase 
      # Check if this is a root shell
		UID_VALUE=`id | awk '{print $1}'`
		if [ "$UID_VALUE" = "uid=0(root)" ]; then
			PS1='\e[1;31m\]\e[7m[\h - ${VIEW}\u ] [\w]\e[m\]\e[27m\n[\!]$ '
			#PS1='\e[7m\e[1;31m\][\h - ${VIEW}\u ] [\w]\e[27m\e[m\]\n[\!]$ '
         #PS1='`tput smso``tput setf 4``tput setb 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setb 7``tput setf 0`\n[\!]$ '
         #PS1='`tput smso``tput setaf 1``tput setab 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setab 7``tput setaf 0`\n[\!]$ '
		else
#echo "debug> Inside 2.6 non root"
			PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
		fi
	;;

	5.*) #Linux 5.* kernels
#		stty erase 
      # Check if this is a root shell
		UID_VALUE=`id | awk '{print $1}'`
		if [ "$UID_VALUE" = "uid=0(root)" ]; then
			PS1='\e[1;31m\]\e[7m[\h - ${VIEW}\u ] [\w]\e[m\]\e[27m\n[\!]$ '
			#PS1='\e[7m\e[1;31m\][\h - ${VIEW}\u ] [\w]\e[27m\e[m\]\n[\!]$ '
         #PS1='`tput smso``tput setf 4``tput setb 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setb 7``tput setf 0`\n[\!]$ '
         #PS1='`tput smso``tput setaf 1``tput setab 7`[\h - ${VIEW}\u ] [\w]`tput rmso``tput setab 7``tput setaf 0`\n[\!]$ '
		else
#echo "debug> Inside 2.6 non root"
			PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
		fi
	;;

	1.5.*) #cygwin shell.  Some alias paths are different on Windows.  Change those here.
		PS1='\e[7m[\h - ${VIEW}\u ] [\w]\e[27m\n[\!]$ '
	;;

	*) #Unknown OS.  Be safe and use the slower way for PS1
		stty erase 
		echo "Unknown OS type detected in .bashrc: `uname -a | awk '{print $1,$3}'`"
		PS1='`tput smso`[\h - ${VIEW}\u ] [\w]`tput rmso`\n[\!]$ '
	;;
esac



#-------------------------------------------------------------
# short list function
function l () {

   # Check for the *.* case and handle it separately
   case $1 in
      *.*)
         ls -adF $*
         return
         ;;
   esac

   # Check if $1 is non null
   pattern=${1:-""}
   if [ "$pattern" != "" ]; then
      # If $1 is nonzero length then add an asterisk to the end
      # so that all files matching $** will be listed
      local pattern=$*
      ls -adF ${pattern:-}*
   else
      ls -aF
   fi

} # l


#-------------------------------------------------------------
# long list function
function ll () {
   # Check for the *.* case and handle it separately
   case $1 in
      *.*)
         ls -aldF $* | more
         return
         ;;
   esac

   # Check if $1 is non null
   pattern=${1:-""}
   if [ "$pattern" != "" ]; then
      # If $1 is nonzero length then add an asterisk to the end
      # so that all files matching $** will be listed
      local pattern=$*
      ls -aldF ${pattern:-}* | more
   else
      ls -alF | more
   fi

} # ll


#-------------------------------------------------------------
# long list function
function llh () {
   # Check for the *.* case and handle it separately
   case $1 in
      *.*)
         ls -aldFh $* | more
         return
         ;;
   esac

   # Check if $1 is non null
   pattern=${1:-""}
   if [ "$pattern" != "" ]; then
      # If $1 is nonzero length then add an asterisk to the end
      # so that all files matching $** will be listed
      local pattern=$*
      ls -aldFh ${pattern:-}* | more
   else
      ls -alFh | more
   fi

} # llh


#-------------------------------------------------------------
# long list time sorted newest first function
function llt () {
   # Check for the *.* case and handle it separately
   case $1 in
      *.*)
         ls -altdF $* | more
         return
         ;;
   esac

   # Check if $1 is non null
   pattern=${1:-""}
   if [ "$pattern" != "" ]; then
      # If $1 is nonzero length then add an asterisk to the end
      # so that all files matching $** will be listed
      local pattern=$*
      ls -altdF ${pattern:-}* | more
   else
      ls -altF | more
   fi

} # llt


#-------------------------------------------------------------
# long list time sorted newest first function
function llth () {
   # Check for the *.* case and handle it separately
   case $1 in
      *.*)
         ls -altdFh $* | more
         return
         ;;
   esac

   # Check if $1 is non null
   pattern=${1:-""}
   if [ "$pattern" != "" ]; then
      # If $1 is nonzero length then add an asterisk to the end
      # so that all files matching $** will be listed
      local pattern=$*
      ls -altdFh ${pattern:-}* | more
   else
      ls -altFh | more
   fi

} # llth


if [ $WINDOWID ] || [ $SSH_TTY ]; then
#150303 - The latest software update for ubuntu 14.04.02 won't go into my
#         desktop if this function is in .bashrc.  They must be using the "."
#         somehow now that breaks because it tries to use this function.  It
#         works fine if I comment out this function.  Dang it.  I use this
#         all the time.
#150304 - Examined my environment. When a terminal is launched within gnome
#         then $WINDOWID exists, but it doesn't when gnome is starting up. So,
#         looking for that or for $SSH_TTY lets me include this function for
#         active terminals and not when gnome is starting up.
#-------------------------------------------------------------
# Backup one directory level
function . () {
	if [ $1 ]; then
		source $1
	else
		cd ..; l
	fi
}
fi

#-------------------------------------------------------------
# Backup two directory levels
function .. () {
	cd ../..; l
}

#-------------------------------------------------------------
# Backup three directory levels
function ... () {
	cd ../../..; l
}

#-------------------------------------------------------------
# Backup four directory levels
function .... () {
	cd ../../../..; l
}

#-------------------------------------------------------------
# Backup five directory levels
function ..... () {
	cd ../../../../..; l
}

#120721 jrl - Removed this function
##-------------------------------------------------------------
## h
## History manipulation function.  It displays the command history in reverse order.
## The number of commands displayed is set by $list_size.  After the initial listing
## a "> " prompt appears.  The user has three choices: 1) Press dot "." to exit 
## doing nothing more, 2) Press "enter" to display the next listing of commands,
## or 3) Enter the number of a command which is executed.  Since the command is
## executed from this function it isn't added to the command history.
#function h () {
#
#   local list_size=10
#
#   # If $1 is a number then just execute it without displaying list
#   if [ $1 ]; then
#      local command=`fc -ln  $1 $1`
#      echo $command
#      local command="eval `fc -ln  $1 $1`"
#      $command
#      return
#   fi
#
#   # Initialize the bottom of the list
#	local bottom=$HISTSIZE
#	let bottom=HISTCMD-HISTSIZE
#	if [ $bottom -lt 1 ];then
#		bottom=1
#	fi
#
#   # Setup the start of the listing
#	local start=$HISTCMD
#	let start=start-list_size
#	if [ $start -lt $bottom ];then
#		start=$bottom
#	fi
#
#   # Setup the end of the listing
#	local stop=$HISTCMD
#	let stop=stop-1
#
#   # Display the first listing
#	fc -lr  $start $stop
#
#   # Get user input on what to do next
#	local response=""
#	while [ "$response" = "" ]; do
#		read -a response -p ". to exit | enter for more | NNN to execute> "
#      # Display next listing if enter pressed
#		if [ "$response" = "" ]; then
#			let stop=start-1
#			if [ $stop -le $bottom ];then
#				stop=$bottom
#			fi
#			let start=start-list_size
#			if [ $start -lt $bottom ];then
#				start=$bottom
#			fi
#			fc -lr  $start $stop
#		fi
#	done
#
#	# Exit if "." pressed
#	if [ "$response" = "." ]; then 
#		return
#	fi
#
#   # Try to execute the line number entered by user
#	local command=`fc -ln  $response $response`
#	echo $command
#	local command="eval `fc -ln  $response $response`"
#   $command
#
#   # 051205 jrl - The following 4 lines used a temp file to execute the command
#	#echo "$command" >| /tmp/$$.command
#	#chmod 700 /tmp/$$.command
#	#/tmp/$$.command
#	#rm -f /tmp/$$.command
#} # h


#-------------------------------------------------------------
# Find word passed in $1 in all files recursively starting in pwd
# This prints out relative path to file, line number, and text on that line.
function wf () {
   # Use sed to replace field divider ":" with "  |  " so the output is easier to read
   find . -type f \( -name "*" -o -name ".*" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/  |  /g'
}

#-------------------------------------------------------------
# Find word passed in $1 in *.c files recursively starting in pwd
function wfc () {
   # Use sed to replace field divider ":" with "  |  " so the output is easier to read
   find . -type f \( -name "*.c" -o -name ".*.c" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/  |  /g'
}

#-------------------------------------------------------------
# Find word passed in $1 in *.h files recursively starting in pwd
function wfh () {
   # Use sed to replace field divider ":" with "  |  " so the output is easier to read
   find . -type f \( -name "*.h" -o -name ".*.h" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/  |  /g'
}

#-------------------------------------------------------------
# Find word passed in $1 in *.h and *.c files recursively starting in pwd
function wfch () {
   # Use sed to replace field divider ":" with "  |  " so the output is easier to read
   find . -type f \( -name "*.[ch]" -o -name ".*.[ch]" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/  |  /g'
}

#-------------------------------------------------------------
# Find word passed in $1 in *.h and *.c files recursively starting in pwd
# Reverse the name from wfch to wfhc for the dislexically minded
function wfhc () {
   # Use sed to replace field divider ":" with "  |  " so the output is easier to read
   find . -type f \( -name "*.[ch]" -o -name ".*.[ch]" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/  |  /g'
}

#-------------------------------------------------------------
# Find files recursively whose names contain the string in $1 starting in current directory
function ff () {
   if [[ -z $1 ]]; then
      echo "Find files recursively starting at \`pwd\` - usage: ff pattern"
      return
   fi

   find -L . -type f \( -name "*$1*" -o -name ".$1*" -o -name ".*.*$1*" -o -name "*$1*.*" \) -print
}

#-------------------------------------------------------------
# Find directories recursively whose names contain the string in $1 starting in current directory
function fd () {
   if [[ -z $1 ]]; then
      echo "Find directories recursively starting at \`pwd\` - usage: fd pattern"
      return
   fi

   find . -type d \( -name "*$1*" -o -name ".$1*" -o -name ".*.*$1*" -o -name "*$1*.*" \) -print
}

