# If not running interactively, don't do anything
if ! echo $- | grep 'i' > /dev/null; then
return
fi
is_ssh() {
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_CONNECTION" ] || [ -n "$SSH_TTY" ]; then
return 0
else
return 1
fi
}
# The shell might not start in the home directory.
if [ "$(pwd)" = "/" ]; then
cd "$HOME"
fi
# Signal to some programs that vi is the editor that should be launched.
export EDITOR=nvim
# Set shell keybindings to vi
set -o vi
#
# The dotfile directory; used by sd()
#
export DOTDIR="$HOME/Dotfiles"
#
# Local user software. Add software to the software directory
# and its bin directory will be automatically loaded when a shell
# is launched.
#
SOFTWARE="$HOME/Software"
# Add some extra directories to the PATH
PATH="$HOME/bin:$PATH"
export PATH HOME TERM
# Store the original path because if scansoft is called, it rebuilds
# the path, starting with the original path.
PATH_BAK="$PATH"
available() {
type $1 > /dev/null 2>&1
return $?
}
scansoft() {
PATH="$PATH_BAK"
if [ ! -d "$SOFTWARE" ]; then
mkdir -p "$SOFTWARE"
else
# Sort -n to sort entries numerically (to allow precedence)
for D in `find "$SOFTWARE" -maxdepth 1 -type d | sort -n`; do
if [ -d "$D/bin" ]; then
echo "Include '$(basename $D)'"
export PATH="$D/bin:$PATH"
fi
done
fi
}
ensoft() {
if [ -z "$1" ]; then
echo "Usage: ensoft
"
return
fi
if [ -d "$SOFTWARE/$1" ]; then
if [ -d "$SOFTWARE/$1/_bin" ]; then
echo "Enabling '$1'..."
mv "$SOFTWARE/$1/_bin" "$SOFTWARE/$1/bin"
echo "Enabled. Run 'scansoft' to apply changes."
elif [ -d "$SOFTWARE/$1/bin" ]; then
echo "Already enabled."
else
echo "'$1' exists, but is not a software directory."
fi
else
echo "'$1' does not exist in the software directory."
fi
}
disoft() {
if [ -z "$1" ]; then
echo "Usage: disoft "
return
fi
if [ -d "$SOFTWARE/$1" ]; then
if [ -d "$SOFTWARE/$1/bin" ]; then
echo "Disabling '$1'..."
mv "$SOFTWARE/$1/bin" "$SOFTWARE/$1/_bin"
echo "Disabled. Run 'scansoft' to apply changes."
elif [ -d "$SOFTWARE/$1/_bin" ]; then
echo "Already disabled."
else
echo "'$1' exists, but is not a software directory."
fi
else
echo "'$1' does not exist in the software directory."
fi
}
mod_time() {
if [ -n "$1" ] && [ -f "$1" ]; then
case "$(uname)" in
Linux) stat -c %Y "$1";;
*BSD) stat -f %m "$1";;
*) echo "0";; # Force compilation every time.
esac
else
echo "0"
fi
}
#
# Dotfile Syncing - If changes are made to any of my dotfiles in git, they're
# synced to the home directory.
#
sd() {
if [ -z "$DOTDIR" ]; then
echo "sd: No dotfile directory specified. Please define \$DOTDIR."
return 1
fi
if [ ! -d "$DOTDIR" ]; then
echo "sd: The dotfile directory '$DOTDIR' does not exist."
return 1
fi
find "$DOTDIR" -type f -not -path "$DOTDIR/.git/*" \
-not -name '*.md' -not -name '*.swp' \
-not -path '*CVS*' -not -name '.#*' \
| while IFS= read -r listing; do
local="$HOME/$(echo "$listing" | sed "s|$DOTDIR/||g")"
docopy=0
if [ -f "$local" ]; then
diff -N "$listing" "$local" > /dev/null
docopy=$?
else
docopy=1
fi
if [ $docopy -eq 1 ]; then
localmtime=$(mod_time "$local" 2> /dev/null)
if [ -z "$localmtime" ]; then
localmtime=0
fi
listingmtime=$(mod_time "$listing" 2> /dev/null)
if [ -z "$listingmtime" ]; then
listingmtime=0
fi
if [ $listingmtime -gt $localmtime ]; then
dir=$(dirname "$local")
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
echo "Created directory '$dir'."
fi
cp -v "$listing" "$local"
else
cp -v "$local" "$listing"
fi
fi
done
}
#
# Shell Prompt
#
# ANSI Colors - Used for Prompt
ANSI_RESET="\[\033[0m\]" # reset
ANSI_HICOLOR="\[\033[1m\]" # hicolor
ANSI_UNDERLINE="\[\033[4m\]" # underline
ANSI_INVERT="\[\033[7m\]" # inverse background and foreground
ANSI_BLACK="\[\033[30m\]" # foreground black
ANSI_RED="\[\033[31m\]" # foreground red
ANSI_GREEN="\[\033[32m\]" # foreground green
ANSI_YELLOW="\[\033[33m\]" # foreground yellow
ANSI_BLUE="\[\033[34m\]" # foreground blue
ANSI_MAGENTA="\[\033[35m\]" # foreground magenta
ANSI_CYAN="\[\033[36m\]" # foreground cyan
ANSI_FWHT="\[\033[37m\]" # foreground white
ANSI_WHITE="\[\033[40m\]" # background black
ANSI_BG_RED="\[\033[41m\]" # background red
ANSI_BG_GREEN="\[\033[42m\]" # background green
ANSI_BG_YELLOW="\[\033[43m\]" # background yellow
ANSI_BG_BLUE="\[\033[44m\]" # background blue
ANSI_BG_MAGENTA="\[\033[45m\]" # background magenta
ANSI_BG_CYAN="\[\033[46m\]" # background cyan
ANSI_BG_WHITE="\[\033[47m\]" # background white
trim() {
awk '{print $1}'
}
generate_prompt() {
LAST_EXIT=$?
printf "\n%s" "$ANSI_HICOLOR"
printf "%s " "$(date '+%Y-%m-%d %H:%M')"
if [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" = "true" ]; then
printf ' %s(%s)' "$ANSI_YELLOW" "$(git rev-parse --abbrev-ref HEAD)"
fi
printf "%s\n" "$ANSI_RESET"
printf "%s" "$ANSI_GREEN"
printf "%s" "$USER"
printf "%s" "$ANSI_RESET"
printf "@"
printf "%s" "$ANSI_GREEN"
printf "%s" "$(hostname -s)"
printf "%s" "$ANSI_RESET"
printf ":"
printf "%s" "$ANSI_HICOLOR"
DIR=$(echo $PWD | sed "s|$HOME|~|g")
printf "%s" "$DIR"
printf "%s\n" "$ANSI_RESET"
if [ "$LAST_EXIT" != "0" ]; then
printf "%s(%s)%s " "$ANSI_RED" "$LAST_EXIT" "$ANSI_RESET"
fi
if [ "$(whoami)" = "root" ]; then
printf "# "
else
printf "$ "
fi
}
PS1="\$(generate_prompt)"
#
# Startup functions - these are executed on every shell start.
#
# Display a line from the fortunes file.
cols=$(stty size | cut -d ' ' -f 2)
shuf -n 1 "$HOME/.fortunes" | fold -s -w $(($cols / 2))
# Run scansoft on shell start
scansoft
if [ -f "$HOME/.aliases" ]; then
. "$HOME/.aliases"
fi