# bash interactive shell twiddles by John Fremlin

export VERSION_CONTROL=numbered
vii_files=".bashrc .bash_settings .bash_logout .bash_login .bash_profile"

# If the primary group has the same name as the user
if test "$(id -ur)" == "$(id -gn)"; then
    umask 0022 # default group writable
else
    umask 0002 # default not group writable
fi

if test ! "$PS1"; then # not interactive
	return 0
fi


# programmable completion project
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ $bmajor -eq 2 ] && [ $bminor '>' 04 ] \
   && [ -f /etc/bash_completion ]; then # interactive shell
        # Source completion code
        . /etc/bash_completion
    : ;
fi

unset bash bmajor bminor

if test "x$SSH_TTY" != x; then
	vii_term_remote=ssh
fi

if test "x$vii_term_remote" == x; then
	case "x$TERM" in (xxterm|xrxvt)
		vii_term_disposable=xterm
		;;
	esac
fi

function vii_reset_colors {
    vii_user='\u@\h'

    case "x$TERM" in (xxterm|xrxvt)
	    vii_foreground() {
		    echo -en "\\033]39;$1\\007"	
	    }

	    vii_background() {
		    echo -en "\\033]49;$1\\007"
	    }
	    vii_title() {
		    echo -en "\\033]2;$1\\007"	
	    }
	    if test "x$COLORTERM" = x; then
		    COLORTERM="$TERM"	
	    fi
	    ;;
	    (*)
	    vii_foreground() { 
		    :
	    }
	    vii_background() { 
		    :
	    }
	    vii_title() {
		    : 
	    }
	    ;;
    esac


    if test "x$vii_term_remote" != x; then
	    vii_background="light steel blue"
	    vii_foreground=black
    else
	    vii_background="lavender"
	    vii_foreground=grey15
    fi

    if test "$UID" = 0; then
	    vii_foreground=grey70
	    vii_background=grey10
	    PATH=$PATH:/sbin/:/usr/sbin/:/usr/local/sbin/
    fi

    if test "x$vii_term_remote" != x; then
	    if test "x$COLORTERM" != "x"; then
		    vii_user="\[\033[31m\]$vii_user\[\033[0m\]"
	    fi
    fi

    vii_title "$(whoami)@${HOSTNAME}"
    if test "x$vii_foreground" != x; then
	vii_foreground "$vii_foreground"
    fi
    if test "x$vii_background" != x; then
	vii_background "$vii_background"
    fi

    export PS1="$vii_user:\w\\\$ "

    unset vii_user vii_foreground vii_background 
}

	
export HISTSIZE=1000000
export HISTCONTROL=ignoredups
export -n HISTFILESIZE # unlimited history file size

shopt -s checkwinsize
shopt -s cdspell
shopt -s checkhash
shopt -s extglob
shopt -s histappend
shopt -s no_empty_cmd_completion
shopt -u stat_all_in_path
shopt -s cmdhist
shopt -s lithist

vii_reset_colors

function vii_reset_terminal {
    vii_reset_colors
}

# This function makes a script to propagate and
# overwrite bash shell and ssh settings
# It is portable to Debian/Linux 3 and FreeBSD 4.7

# To use: 
# vii_propagate | ssh user@remote-host /bin/sh -s
# vii_propagate | su user -c /bin/sh

function vii_propagate {
    echo "#! /bin/sh"
    echo "mkdir -p ~/.ssh"
    echo "chmod 700 ~/.ssh"
    echo "cd ~ || exit $?"
    echo "uudecode -o /dev/stdout <<'EOF' | tar --no-same-owner -xvpf -"
    (cd ~ && tar -cpf - $vii_files .ssh/authorized_keys .ssh/known_hosts | uuencode -)
    echo EOF
}

# overrides

for cmd in su ssh; do
    eval "function $cmd { $(which $cmd) \"\$@\" ; vii_reset_terminal; }"
#    eval "function $cmd { echo \"$cmd function now calling: $cmd \$@\"; $(which $cmd) \"\$@\" ; vii_reset_terminal; }"
done

function away { mv -vi "$1" "${1}".away; }
function back { mv -vi "${1}".away "${1}"; }

# leaving vii_term_disposable vii_term_remote vii_files

###  Local Variables:
###  mode: shell-script
###  End:
