#!/bin/zsh
# I got this file from someone (don't remember who though) 
# and modified it :).
#
# Arne Schwabe <arne@rfc2549.org>
#
# But some credit still goes to (and others):
#
# zshrc - by trey tabner. email:  trey@epicsol.org   irc:  icetrey@efnet
# not all of this is mine, a few of the completions are taken from old examples :)

hosts=( news mailrapid mailgate mailgate2 mailserv \
	leros atreus monkey-island \
    ilum r2d2 kamino geonosis coruscant.rfc1149.org \
	info-stud info-e info-f angkor-vat athene webserv \
	naxos simon-the-sorcerer the-little-computer-project pissarro \
	kevin feivel orang-utan-klaus \
	queaker debian.ventourizen.de fireball \
	debian.ventourizen.de queaker )

umask 022
alias dos2unix='recode ibmpc:lat1'
alias unix2dos='recode la1:imbpc'
alias dp=display
if [[ -e /usr/local/maple8-8.01/bin/ ]]; then
	alias maple=/usr/local/maple8-8.01/bin/maple
	alias xmaple=/usr/local/maple8-8.01/bin/xmaple
fi

#alias kernel='finger @finger.kernel.org'

alias j='jobs -l'
alias h='history'

lynx='lynx -accept_all_cookies'

alias lowercase='for i in [A-Z][A-Z]*(.); do mv "$i" "${i:l}" ;done'
alias bt='PORT=$RANDOM; btdownloadcurses.py --max_upload_rate 3 --minport $(($PORT+1400)) --maxport $(($PORT + 1410))'
alias btu='PORT=$RANDOM; btdownloadcurses.py --max_upload_rate 100 --minport $(($PORT+1400)) --maxport $(($PORT + 1410))'
alias bto='PORT=$RANDOM; btdownloadcurses.py --max_upload_rate 13 --minport $(($PORT+1400)) --maxport $(($PORT + 1410))'
alias mgqueue='ssh mailgate /home/mail/exim/bin/exim -C /home/mail/exim/configure.outgoing -bpc'
alias acroread="LANG=de_DE acroread"

export HISTFILE=$HOME/.zshhistory
export HISTSIZE=10000
export SAVEHIST=5000

export EDITOR=emacs
export PAGER=less

export LANG=de_DE.UTF-8
export LC_MESSAGES=C

setopt \
        NO_all_export \
        always_last_prompt \
        NO_always_to_end \
        append_history \
        NO_auto_cd \
        auto_list \
        auto_menu \
        NO_auto_name_dirs \
        auto_param_keys \
        auto_param_slash \
        auto_pushd \
        auto_remove_slash \
        NO_auto_resume \
        bad_pattern \
        bang_hist \
        NO_beep \
        bg_nice \
        brace_ccl \
		bsd_echo \
        correct_all \
        cdable_vars \
        NO_chase_links \
        no_clobber \
        complete_aliases \
        complete_in_word \
        correct \
        NO_correct_all \
        csh_junkie_history \
        NO_csh_junkie_loops \
        NO_csh_junkie_quotes \
        NO_csh_null_glob \
        equals \
        extended_glob \
        extended_history \
        function_argzero \
        glob \
        NO_glob_assign \
        glob_complete \
        glob_dots \
        glob_subst \
        hash_cmds \
        hash_dirs \
        hash_list_all \
        NO_hist_allow_clobber \
        NO_hist_beep \
        hist_ignore_dups \
        hist_ignore_space \
        hist_no_store \
        no_hist_verify \
        NO_hup \
        NO_ignore_braces \
        NO_ignore_eof \
        interactive_comments \
        NO_list_ambiguous \
        NO_list_beep \
        list_types \
        long_list_jobs \
        magic_equal_subst \
        NO_mail_warning \
        NO_mark_dirs \
        NO_menu_complete \
        multios \
        nomatch \
        notify \
        NO_null_glob \
        numeric_glob_sort \
        NO_overstrike \
        path_dirs \
        posix_builtins \
        print_exit_value \
        NO_prompt_cr \
        prompt_subst \
        pushd_ignore_dups \
        NO_pushd_minus \
        NO_pushd_silent \
        pushd_to_home \
        rc_expand_param \
        NO_rc_quotes \
        NO_rm_star_silent \
        NO_sh_file_expansion \
	    sh_option_letters \
		no_sh_glob \
        short_loops \
        NO_sh_word_split \
        NO_single_line_zle \
        NO_sun_keyboard_hack \
        unset \
        NO_verbose \
        NO_xtrace \
        zle



# From zefram Prompt and heavily modified (you did not expect anything else, did you)
# The screen and preexec thing came from www.zshwiki.org/cgi-bin/wiki.pl?ZshHardStatus

 case $TERM in
	 xterm*)
		 function title () {print -nP '\e]0;'$*'\a'}
         ;;
	 screen*)
		 function title () {
		   print -nPR $'\033k'$1$'\033'\\ 
		   print -nPR $'\033]0;'$2$'\a'
		   }
		 ;;
	 *)
		 function title() {}
		 ;;
		 
 esac


function prompt_arne_zefram_precmd {
        local exitstatus=$?
        psvar=(SIG)
        [[ $exitstatus -gt 128 ]] && psvar[1]=SIG$signals[$exitstatus-127]
        [[ $psvar[1] = SIG ]] && psvar[1]=$exitstatus
        jobs % >/dev/null 2>&1 && psvar[2]=

		title "%n@%m:%20<...<%3~%<<"
}

function prompt_arne_zefram_setup {
  PS1='[%(00t.DING%(0T. DONG.)!.%T)]%(?..%U{%v}%u)%(!..%n%(2v.%B@%b.@))%m:%20<...<%3~%<<%# '
  PS2='%(4_:... :)%3_> '

  prompt_opts=( cr subst percent )
  precmd () { prompt_arne_zefram_precmd }
  function preexec {
      emulate -L zsh
      local -a cmd; cmd=(${(z)1})
      title %n@%m:$cmd[1]:t "$cmd[2,-1]"
  }
}

function prompt_arne_zefram_setup3 {
	prompt_arne_zefram_setup
	function preexec() {}
}

if [[ $USER != root ]]; then
    if [[ "$SSH_AUTH_SOCK" == "" && -f ~/.ssh/.agent-$HOST ]]; then
	echo -n "Reused PID: "
	source ~/.ssh/.agent-$HOST
    fi


    ssh-add -l  2> /dev/null # | cut -d" " -f1,3,4

    if [[ $? == 1 || $? == 0 ]] then
	#echo ssh agent reused
	:
    else
	if [[ -f ~/.ssh/.agent-$HOST ]]; then
	    rm ~/.ssh/.agent-$HOST
	fi
	ssh-agent > ~/.ssh/.agent-$HOST
	source ~/.ssh/.agent-$HOST
    fi
    unset tmp
fi

if [[ $OSTYPE = 'linux-gnu' ]]; then
	filesystems="${${(f)$(</proc/filesystems)}#*  }"
	eval "$(dircolors)"
	setterm -hbcolor bright white
	setterm -ulcolor cyan

	alias ls='ls --color=auto'
	alias df='df -T'
	

	# FreeBSD seems to be more clever, so I move this to Linux
	export PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH
	if [ -d ~/bin ]; then
	   PATH="$HOME/bin:${PATH}"
	fi

	# Sigh, I have far _too_ many GNUism in my config
elif test `uname` = 'FreeBSD'; then
	# No PATH setting :)))
	alias ls='ls -GF'


	
	function temp () {
		echo $(($(sysctl hw.acpi.thermal.tz0.temperature \
			| cut -d " " -f 2) / 10.0 - 273.15))
	}
elif test `uname` = 'SunOS'; then
	PATH=/usr/sbin/:/usr/bin
	if [ -e /usr/pkg ]; then
		PATH=${PATH}:/usr/pkg/bin:/usr/pkg/sbin
		MANPATH=${MANPATH}:/usr/pkg/man
	fi
	PATH=${PATH}:/opt/SUNWspro/bin
	PATH=${PATH}:/usr/local/bin
	PATH=${PATH}:/usr/ccs/bin/:usr/ccs/lib
	PATH=${PATH}:/opt/sfw/bin

	if [ "DISPLAY" != "" ]; then
		PATH=${PATH}:/usr/dt/bin
		PATH=${PATH}:/usr/openwin/bin
	fi
	export PATH
fi


if [[ $HOST == "coruscant" ]]; then
	export CVSROOT="/var/lib/cvs"
else
	export CVSROOT=":pserver:arne@coruscant.rfc1149.org:/var/lib/cvs"
fi

	
if test $HOST = "mailrapid" || test $HOST = "mailgate"  || test $HOST = "mailgate2" || test $HOST = "mailrapid2"  ; then
	export PATH=/home/mail/bin:$PATH
	if test $HOST = "mailrapid" ; then
		export PATH=$PATH:/var/mailman/bin
	fi
fi



bindkey -me
bindkey -s '^X^Z' '%-^M'
bindkey '^[e' expand-cmd-path
bindkey -s '^X?' '\eb=\ef\C-x*'
bindkey '^[^I' reverse-menu-complete
bindkey '^[p' history-beginning-search-backward
bindkey '^[n' history-beginning-search-forward

bindkey '^p' history-beginning-search-backward
bindkey '^n' history-beginning-search-forward
bindkey '^[[A' history-beginning-search-backward
bindkey '^[[B' history-beginning-search-forward

bindkey '^W' kill-region
bindkey '^I' expand-or-complete-prefix
bindkey -s '^[[Z' '\t'


bindkey "\e[4~" end-of-line
bindkey "^[[8~"  end-of-line

bindkey "^[[1~" beginning-of-line
bindkey "^[[7~" beginning-of-line

if [[ ${ZSH_VERSION%%.*} -ge 4 ]]; then
	zstyle :compinstall filename '/home/arne/.zshrc'
	zstyle '*' hosts $hosts
	prompt_arne_zefram_setup

	autoload -U compinit
	compinit
	
#	setopt \
#		share_history
 else 
	prompt_arne_zefram_setup3
fi

## Orginal Source Code of the Clock:
## Thomas Khler also has a .zshrc: http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/zsh/u
# Modified to fit to my prompt
# Does not work so good :/

#trap uhrzeit ALRM
#TMOUT=60

#function uhrzeit() {
#   print -nP "\e7\e[1;G"
#   # echo the date
#   print -nP "[%(00t.DING%(0T. DONG.)!.%T)]"
#   # restore cursor position
#   print -nP "\e8"
#}





function kernel()
{
	printf 'GET /kdist/finger_banner HTTP1.0\n\n' | nc www.kernel.org 80 | grep latest
}

function http_header()
{
       # This is zsh but still no gnu echoism ;P
       if [[ $2 != "" ]] { port=$2 } else { port=80 }
       if [[ $3 != "" ]] { vhost="$3" } else { vhost=$1 }
 
       printf "HEAD /$4 HTTP/1.1\\nHost: $vhost\nConnection: close\n\n" \
        | nc $1 $port
}


function setenv() { export $1="$2" }
function unsetenv() { unset $1 }


