# -*- mode: sh; mode: font-lock; -*-
#zsh .zshrc file

#export LC_CTYPE=iso_8859_1

#
# Special setup for different users, common enough to be generally copied.
#
if [ "x$REMOTEUSER" = "x" ]; then
   if [ "${SSH_CLIENT% [0-9]# [0-9]#}" = "195.204.143.218" -o "${SSH_CLIENT% [0-9]# [0-9]#}" = "195.204.143.223" ]; then
	REMOTEUSER=eivind
   elif [ $"{REMOTEUSER}" = "des" -o "${SSH_CLIENT% [0-9]# [0-9]#}" = "195.204.143.216" ]; then
	REMOTEUSER=des
   elif [ "${SSH_CLIENT% [0-9]# [0-9]#}" = "195.204.143.184" -o "${SSH_CLIENT% [0-9]# [0-9]#}" = "195.204.143.185" ]; then
	REMOTEUSER=terje
   elif [ "${SSH_CLIENT% [0-9]# [0-9]#}" = "195.204.143.160" ]; then
	REMOTEUSER=geir
   elif [ "$(hostname)" = "bitbox.follo.net" ]; then
	REMOTEUSER=eivind
   fi
fi


# For Terje only
export DIRSTACKSIZE=8
setopt autopushd pushdminus pushdsilent pushdtohome
alias dh='dirs -v'
export EXINIT='set autoindent'
export CVS_RSH=ssh
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib:/usr/local/lib
export BLOCKSIZE=k
export HTTP_AUTH='basic:*'
HISTSIZE=1000
setopt HIST_IGNORE_DUPS
setopt EXTENDED_GLOB
setopt AUTO_CD
setopt COMPLETE_IN_WORD
setopt LIST_TYPES
# Get completions ABOVE the command line
setopt NOALWAYSLASTPROMPT
setopt NOAUTO_REMOVE_SLASH
unlimit

### Generic hacks
# Find out how to handle this for SGI to work
if [ "${TERM}" = "xterm" ]
then
    export TERM=xterm-color
fi

# Title-hack for xterm - put current directory in title
if [ "${TERM}" = "xterm-color" ] 
then 
    # Set xterm title to user@host:dir, and icon to hostname
    chpwd() { if [ -t 1 ]; then echo -n "\e]2; $(id -u -n)@${HOST%%.*}:$(print -P %~)${XUSERTITLE:+ [${XUSERTITLE}]}\007\e]1;$(id -u -n)@${HOST%%.*}${XUSERTITLE:+ [${XUSERTITLE}]}\007"; fi }
    chpwd			# Do it once to grab the initial dir
    export PS1="%B%n(%m)--%#%b "
    
# XXX This do, unfortunately, not work properly alongside suspension (ctrl-z).
#    SSH=$(unfunction ssh >& /dev/null; unalias ssh >& /dev/null; which ssh)
#    if [ "$?" = "0" ]
#    then
#	eval "ssh() { (${SSH} \$*; a=\$?; chpwd; exit \$a) }"
#    fi
else
    export PS1="%B%n(%m)%~%#%b "
fi

# Emacs keybindings - NOT, NOT, NOT vi!
bindkey -e
# I like CSH semantics for these.
bindkey "\ep" history-beginning-search-backward
bindkey "\en" history-beginning-search-forward

export PATH=$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:/usr/local/jdk/bin
if [ -x /etc/profile ]; then
    setopt -G
    . /etc/profile
    setopt +G
fi

if [ x$DISPLAY != x ]; then
    xset b off
fi

# Add 'less' as the pager if less exists.
if [ -x $(which less) ]
then
    export PAGER=less
    unalias less >& /dev/null
else
    alias less=more
fi

# Set up editor variable
if [ -r "$(which eedit)" -a -x "$(which eedit)" ]
then
    export EDITOR=eedit
elif [ -r "$(which jmacs)" -a -x "$(which jmacs)" ]
then
    export EDITOR=jmacs
elif [ -r "$(which jove)" -a -x "$(which jove)" ]
then
    export EDITOR=jove
elif [ -r "$(which vi)" -a -x "$(which vi)" ]
then
    export EDITOR=vi
elif [ -r "$(which emacs)" -a -x "$(which emacs)" ]
then
    export EDITOR=emacs
fi

# I want zsh now and forever after, but only need to set if this isn't
# a login-shell :)

if [ "${0:#-*}" != "" ] && TMPSHELL=$(which $0) || TMPSHELL=$(which zsh)
then
    export SHELL=$TMPSHELL
fi

###
### My beloved aliases
###

# Private
if [ -e /www/apps/mysql/bin/mysql ]
then
    alias mmysql='/www/apps/mysql/bin/mysql  -p$(cat /www/yes/etc/db_pass)'
    alias mmysqladmin='/www/apps/mysql/bin/mysqladmin  -p$(cat /www/yes/etc/db_pass)'
    alias mmysqlshow='/www/apps/mysql/bin/mysqlshow  -p$(cat /www/yes/etc/db_pass)'
    alias mmysqldump='/www/apps/mysql/bin/mysqldump  -p$(cat /www/yes/etc/db_pass)'
fi

# More generally useful
alias la="ls -a"
alias lf="ls -FA"
alias ll="ls -lA"
alias su="su -m"
alias g.='glimpse -H .'
alias gi.='glimpseindex -H . .'
alias perlfunc='perldoc -f'
alias e=\$EDITOR
        #if [ "$1" = "${1##/}" ]
        #then
#	    pathlist=(${(s:/:)1})
	#else
	#    pathlist=(/ ${(s:/:)1})
	#fi
#
# List all directories leading up to a filename; this is useful to see
# if some permissions are blocking access to a file.
#
lspath () {
        if [ "$1" = "${1##/}" ]
        then
	    pathlist=(/ ${(s:/:)PWD} ${(s:/:)1})
	else
	    pathlist=(/ ${(s:/:)1})
	fi
        allpaths=()
        filepath=$pathlist[0]
        shift pathlist
        for i in $pathlist[@]
        do
                allpaths=($allpaths[@] $filepath) 
                filepath="${filepath%/}/$i" 
        done
        allpaths=($allpaths[@] $filepath) 
        ls -ld "$allpaths[@]"
}

#
# EE and DES have commit privileges for FreeBSD
#
if [ "${REMOTEUSER}" = "eivind" -o "${REMOTEUSER}" = "des" ]; then
   alias ncvs='cvs -d ${REMOTEUSER}@freefall.freebsd.org.:/e/NetBSD/NetBSD-CVS'
   alias fcvs='cvs -d ${REMOTEUSER}@freefall.freebsd.org.:/home/ncvs'
fi

#
# Set up so I always get some 'local' FreeBSD CVS repository.  This
# use the local repository if available, but switch to the repository
# on bitbox if there isn't one.
#
if [ -e "/home/ncvs/src/Makefile.inc1,v" ]; then
    alias lcvs='cvs -d /home/ncvs'
else
    alias lcvs='cvs -d ${REMOTEUSER}@bitbox.follo.net.:/home/ncvs'
fi
if [ "x${REMOTEUSER}" != "x" ]; then
	alias ycvs='cvs -d ${REMOTEUSER}@panic.follo.net:/home/ncvs'
else
	alias ycvs='cvs -d panic.follo.net:/home/ncvs'
fi

#
# Usage: pkg_kill <path-to-package-description>
#
# Will delete the relevant package.  Without doing su, I can just do
# 'pkg_kill /var/db/pkg/emacs-19.34', and it will prompt me for
# password and remove the thing.  Interacts nicely with tab-completion
# :-)
#
pkg_kill() { echo pkg_delete $(basename $1) | su; }
compctl -g '/var/db/pkg/*(/:t)' pkg_delete pkg_info pkg_kill


###
### Misc completion control
###

# Manual page completion
man_glob () {
  local a
  read -cA a
  if [[ $a[2] = [0-9]* ]] then	# BSD section number
    reply=( $^manpath/man$a[2]/$1*$2(N:t:r) )
  elif [[ $a[2] = -s ]] then	# SysV section number
    reply=( $^manpath/man$a[3]/$1*$2(N:t:r) )
  else
    reply=( $^manpath/man*/$1*$2(N:t:r) )
  fi
}
compctl -K man_glob man

# Complete commmands after .
compctl -c .

# Complete correct commands on the first word after 'cvs'.  'admin'
# and 'init' are not included in the completions, due to their
# infrequent use and blocking for other completions.
compctl -f -x 'p[1]' -k "(add annotate checkout commit diff edit \
    editors export history import log login logout rdiff release remove \
    rtag status tag unedit update watch watchers)" - -- cvs

compctl -k hosts ssh telnet rlogin finger rsh ftp ncftp ping traceroute cmd
compctl -j -P % -x 's[-] p[1]' -k signals -- kill
#compctl -x 'S[/][~][./][../]' -g '*(-/)' - \
#        'n[-1,/], s[]' -K cdmatch -S '/' -- cd pushd
compctl -D -f + -U -K multicomp
compctl -f -x 'p[1,1]' -u -- chown                            


# Set up linuxls so that I can get colors.  Don't break ls if linuxls
# isn't available, though.

if [ "${TERM}" != "emacs" ]
then
    if which gnuls 2> /dev/null > /dev/null; then
	alias ls="gnuls --color=tty -F"
    elif which linuxls 2> /dev/null > /dev/null; then
	alias ls="linuxls --color=tty -F"
    fi
fi

# Add some obscure path components if they are available.  This is
# useful on e.g. SGI.  I do it conditionally because I don't want to
# bear the expense of having these in my path if they are not used -
# I'd rather eat a little extra startup cost.
if [ -x /usr/lib/sendmail ]
then
    PATH=$PATH:/usr/lib
fi

if [ -d /usr/bsd ]
then
    PATH=$PATH:/usr/bsd
fi

if [ -d /usr/games ]
then
    PATH=$PATH:/usr/games
fi

# Select the correct sound device
if [ -c /dev/rwcd0c ]
then
	export MUSIC_CD=/dev/rwcd0c
else
	if [ -c /dev/rcd0c ]
	then
		export MUSIC_CD=/dev/rcd0c
	fi
fi

export XFILESEARCHPATH='/usr/X11R6/lib/X11/%T/%N%C%S:/usr/X11R6/lib/X11/%T/%N%S'

#setopt autolist
setopt nobeep

#
# Handle local extras
#
if [ -r ${HOME}/.zshrc-local ]
then
    source ${HOME}/.zshrc-local
fi
if [ -r ${HOME}/.zshrc-${REMOTEUSER} ]
then
    source ${HOME}/.zshrc-${REMOTEUSER}
fi
