# -*- sh -*-
#

is_root() {
  test ${EUID:?} -eq 0
}

sourcedir() {
    if [ ! -d "$1" ]; then
	echo "no such directory: $1"
	return 1
    fi
    for f in "$1"/*(.N,@N); do
	case "$f" in
	    *~|*.bak|*.old|*.sw?|*.zwc|*.disabled) continue;;
	esac
#	echo "  sourcing file $f"
	source "$f"
    done
}

_DIR=$HOME/${(%):-%1N}.d
sourcedir $_DIR

alias mv='nocorrect mv'       # no spelling correction on mv
alias cp='nocorrect cp'       # no spelling correction on cp
alias mkdir='nocorrect mkdir' # no spelling correction on mkdir
alias ssh='ssh-add -l > /dev/null 2>&1 || ssh-add ; \ssh'

bindkey -e      # Emacs-style commandline editing

if [ $ZSH_VERSION = 4.3.6 ]; then
    # Lenny's zsh doesn't have %F
    FG_RED=$(tput setf 4)
    FG_BLACK=$(tput setf 0)
    PROMPT=': %(!.%{'"$FG_RED"'%}.)%U%n@%m%u%(!.%{'"$FG_BLACK"'%}.)'${CHROOT}' ${vcs_info_msg_0_}%B%30<..<%~%b %(!.#.>) '
else
    FG_HOST="$(printf "%.3d" 0x"$(expr substr "$(echo "${SSH_CONNECTION%% *}" | md5sum)" 1 2)")"
    BG_HOST="$(printf "%.3d" "$(( 256 - $FG_HOST ))")"

    PROMPT=': %(!.%F{red}.)%U%n%(!.%f.)@%{'"$FG[$FG_HOST]$BG[$BG_HOST]"'%}%m%u%f%k'${CHROOT}' ${vcs_info_msg_0_}%B%30<..<%~%b %(!.#.>) '
fi

case `uname -s` in
    "FreeBSD")
        :
        ;;
    *)
        case "$TERM" in
            linux|*xterm*|ansi|linux-lat|screen)
                alias ls="ls --color=tty -FN $LS_OPTIONS"
                ;;
            *) 
                alias ls="ls -FN $LS_OPTIONS"
                ;;
        esac
        ;;
esac

alias mirror="wget --mirror --no-parent --convert-links -P $HOME/data/www-mirror "
# The following lines were added by compinstall

zstyle ':completion:*' completer _list _expand _complete _ignored _match _prefix
zstyle ':completion:*' condition 'NUMERIC != 1'
zstyle ':completion:*' matcher-list '' 'r:|[._-]=* r:|=*' 'm:{a-z}={A-Z} l:|=* r:|=*'
zstyle ':completion:*' match-original both
zstyle ':completion:*' max-errors 1
zstyle :compinstall filename '/home/tfheen/.zshrc'

autoload -U url-quote-magic
zle -N self-insert url-quote-magic

CHPWD_PROFILE='default'

function chpwd_profiles() {
    # Say you want certain settings to be active in certain directories.
    # This is what you want.
    #
    # zstyle ':chpwd:profiles:/usr/src/grml(|/|/*)'   profile grml
    # zstyle ':chpwd:profiles:/usr/src/debian(|/|/*)' profile debian
    #
    # When that's done and you enter a directory that matches the pattern
     # in the third part of the context, a function called chpwd_profile_grml,
     # for example, is called (if it exists).
     #
     # If no pattern matches (read: no profile is detected) the profile is
     # set to 'default', which means chpwd_profile_default is attempted to
     # be called.
     #
     # A word about the context (the ':chpwd:profiles:*' stuff in the zstyle
     # command) which is used: The third part in the context is matched against
     # ${PWD}. That's why using a pattern such as /foo/bar(|/|/*) makes sense.
     # Because that way the profile is detected for all these values of ${PWD}:
     #   /foo/bar
     #   /foo/bar/
     #   /foo/bar/baz
     # So, if you want to make double damn sure a profile works in /foo/bar
     # and everywhere deeper in that tree, just use (|/|/*) and be happy.
     #
     # The name of the detected profile will be available in a variable called
     # 'profile' in your functions. You don't need to do anything, it'll just
     # be there.
     #
     # Then there is the parameter $CHPWD_PROFILE is set to the profile, that
     # was is currently active. That way you can avoid running code for a
     # profile that is already active, by running code such as the following
     # at the start of your function:
     #
     # function chpwd_profile_grml() {
     #     [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
     #   ...
     # }
     #
     # The initial value for $CHPWD_PROFILE is 'default'.
     #
     # Version requirement:
     #   This feature requires zsh 4.3.3 or newer.
     #   If you use this feature and need to know whether it is active in your
     #   current shell, there are several ways to do that. Here are two simple
     #   ways:
     #
     #   a) If knowing if the profiles feature is active when zsh starts is
     #      good enough for you, you can put the following snippet into your
     #      .zshrc.local:
     #
     #   (( ${+functions[chpwd_profiles]} )) && print "directory profiles active"
     #
     #   b) If that is not good enough, and you would prefer to be notified
     #      whenever a profile changes, you can solve that by making sure you
     #      start *every* profile function you create like this:
     #
     #   function chpwd_profile_myprofilename() {
     #       [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
     #       print "chpwd(): Switching to profile: $profile"
     #     ...
     #   }
     #
     #      That makes sure you only get notified if a profile is *changed*,
     #      not everytime you change directory, which would probably piss
     #      you off fairly quickly. :-)
     #
     # There you go. Now have fun with that.
     local -x profile
 
     zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default'
     if (( ${+functions[chpwd_profile_$profile]} )) ; then
         chpwd_profile_${profile}
     fi

     CHPWD_PROFILE="${profile}"
     return 0

}

zstyle ':chpwd:profiles:'${HOME}'/debian(|/|/*)'	profile debian
zstyle ':chpwd:profiles:(|/|*)(varnish|libvmod)*(|/|/*)'		profile varnish
zstyle ':chpwd:profiles:'${HOME}'/VS(|/|/*)'	profile varnish

function chpwd_profile_varnish() {
    [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
    print "chpwd(): Switching to profile: $profile"
    export GIT_AUTHOR_EMAIL=tfheen@varnish-software.com
}

function chpwd_profile_default() {
    [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
    print "chpwd(): Switching to profile: $profile"
    export GIT_AUTHOR_EMAIL=tfheen@err.no    
}

function chpwd_profile_debian() {
    [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
    print "chpwd(): Switching to profile: $profile"
    export GIT_AUTHOR_EMAIL=tfheen@err.no    
}

chpwd_functions=( ${chpwd_functions} chpwd_profiles )

chpwd_profiles
fpath=( "$HOME/.zshfunc" "$fpath[@]" )
export FPATH
# Only unique entries please.
typeset -U fpath

for paths in "$fpath[@]"
  do
    autoload -U "$paths"/*(N:t) >/dev/null
done
unset paths

# Don't beep.  Ever.
setopt -B

hosts=( $DEBHOSTS )

if [ -e ~/.ssh/known_hosts ]; then
    hosts=($hosts `awk -F "[, ]" '{print $1}' ~/.ssh/known_hosts`)
fi
if [ -e ~/.ssh/known_hosts2 ]; then
    hosts=($hosts `awk -F "[, ]" '{print $1}' ~/.ssh/known_hosts2`)
fi
if [ -e ~/.ssh/config ]; then
    hosts=($hosts `grep ^Host ~/.ssh/config | sed s/Host\ // | egrep -v '^\*$'`)
fi
if [ "$hosts" ]; then
zstyle ':completion:*:hosts' hosts $hosts
fi

autoload -U compinit
compinit
# End of lines added by compinstall

test -x /usr/games/fortune && /usr/games/fortune
