#!/bin/bash
# linux /etc/profile:
#
# sections: 
#
# SectA: limits, shell's self-control.
# SectB: environment: host-specifics are at the bottom.
# SectC: extended env: env, set up by evaluating output from commands.
# SectD: aliases and functions.
# SectE: prompt settings.
# SectF: local settings.
#-------------------------------------------------------------------------

DEBUG_MODE="no"
if [ "$DEBUG_MODE" = "yes" ]; then
    echo "debug mode of /etc/profile. SHELL = $SHELL"
fi


#-------------------------------------------------------------------------
# SectA: limits and shell's self-control
#-------------------------------------------------------------------------
if [ "$DEBUG_MODE" = "yes" ];then
echo SectA /etc/profile
echo SHELL = $SHELL shell = $shell
fi

#PROFILEREAD=true		# should be done from ~/.profile.
#export PROFILEREAD

# stuff that we need for the following:
HOSTNAME="`hostname -f`"
HNAME="`hostname|cut -f1,2 -d.`"
HOST="`hostname|cut -f1 -d.`"
DISPNUM=`echo "$DISPLAY"|cut -f2 -d:`
export HOSTNAME HOST


logger -t etc.profile $LOGNAME logged on `/usr/bin/tty` host $HOSTNAME SHELL $SHELL HOME $HOME
ulimit -c unlimited            # 

case "$LOGNAME" in
nobody|root|draht|content|nexstat|dcu|nexuiz|nexctf|nex1v1|jp|nexdbc|nextst|munin|dummy|mine|bitcoin|news|mysql|wwwrun|man|suse-ca|lp|dummy2|tv|boinc|lighttpd|clamavdb|openttd)    ;;
*)
    #umask 022
    # adjust some limits (see bash(1))
    # this needs to be adjusted to suit ksh which claims to understand ulimit -v.
    if [ "`ulimit -v 2>/dev/null`" = "unlimited" ]; then
#        ulimit -v 4096000   >/dev/null 2>&1  # virtual memory limit
:
    fi
    ulimit -c unlimited            # only core-files less than 20 MB are written
    #ulimit -d 15000            # max data size of a program is 15 MB
    #ulimit -s 15000            # max stack size of a program is 15 MB
    #ulimit -m 30000            # max resident set size is 30 MB
#    ulimit -p 256     # max num of proc.
#    ulimit -t 10800 >/dev/null 2>&1     # cpu-time limit in seconds
#    ulimit -n 1023 > /dev/null 2>&1 # fd limit
#    ulimit -t 3600
    #ulimit -c 0                     # don't create core files
    #ulimit -c unlimited
    #ulimit -d unlimited
    #ulimit -s unlimited
    #ulimit -m unlimited
    case "$HOME" in
    /home/*|/var/home/*|/root/*);;
    #*)      exit 1;;
    esac
esac


# shell detection: We set $SHELL dependent on what shell we have. Not all shells
# set it automatically.
OSHELL="$SHELL"

# zsh
if [ "$ZSH_NAME" = "zsh" ]; then
    RUNNING_SHELL=/usr/bin/zsh
fi

# bash
if [ ! -z "$BASH_VERSION" ]; then
    RUNNING_SHELL=/bin/bash
fi

# ksh
if [ ! -z "$KSH_VERSION" ]; then
    RUNNING_SHELL=/usr/bin/ksh
fi

if [ "$DEBUG_MODE" = "yes" ]; then
    echo "executing shell is $RUNNING_SHELL SHELL is $SHELL"
fi

case "$RUNNING_SHELL" in
*tcsh)
;;
*ksh)
    export SHELL=/usr/bin/ksh
    export ENV=/etc/kshrc
    if [ -t 0 -a -d $HOME/.ksh ]; then
    export HISTFILE=$HOME/.ksh/v2.$LOGNAME@"$HOSTNAME"
    fi
    test -r ${HOME}/.kshrc && ENV=${HOME}/.kshrc
;; #ksh
*bash)
    if [ "$DEBUG_MODE" = "yes" ];then
    echo "SectA HISTFILE SHELL is *bash"
    fi
    if [ -t 0 -a ! -d $HOME/.bash -a "$LOGNAME" != "vdr" ]; then
        mkdir $HOME/.bash # >/dev/null 2>&1
    fi

    if [ -t 0 -a -d $HOME/.bash ]; then
        export HISTFILE=$HOME/.bash/$LOGNAME@"$HOSTNAME"
    else
        export HISTFILE=$HOME/.bash_history
    fi
    touch $HISTFILE >/dev/null 2>&1        # make sure it already exists
    shopt -s histappend
    alias ,root='history -a;command ,root;history -r'
    test -f $HOME/.profile || cp /etc/skel/.profile $HOME
    test -f $HOME/.bashrc  || cp /etc/skel/.bashrc  $HOME

;; # bash
*zsh)
    if [ "$DEBUG_MODE" = "yes" ];then
        echo "SectA HISTFILE SHELL is *zsh"
    fi
    : # $?=0 now
    if [ -t 0 -a ! -d $HOME/.zsh ]; then
	mkdir $HOME/.zsh # >/dev/null 2>&1
    fi
    if [ -t 0 -a -d $HOME/.zsh ]; then
        export HISTFILE=$HOME/.zsh/"$LOGNAME"@"$HOSTNAME"
    else
        export HISTFILE=$HOME/.zsh_history
    fi
    HISTSIZE=5000
    SAVEHIST=5000
    # No CR to screw output
    unsetopt promptcr
    # Correction sucks!
    setopt nocorrect
    setopt nocorrectall
    setopt auto_cd
    unset RPROMPT
    unsetopt print_exit_value
    unsetopt bg_nice
    unsetopt hup
    # History completion
    autoload -U history-search-end
    zle -N history-beginning-search-backward-end history-search-end
    zle -N history-beginning-search-forward-end history-search-end
    #bindkey "^[[A" history-beginning-search-backward-end
    #bindkey "^[[B" history-beginning-search-forward-end

    # Kill parts of a path with ESC-backspace
    autoload -U bash-backward-kill-word
    zle -N bash-backward-kill-word
    bindkey "^[^?" bash-backward-kill-word

    # The nice ZSH completion
    autoload -U compinit
    compinit
    zstyle -e ':completion:*:ssh:*' hosts 'reply=($(sed -e "/^#/d" -e "s/ .*\$//" -e "s/,/ /g" /etc/ssh_known_hosts ~/.ssh/known_hosts ~/.ssh/known_hosts2 2>/dev/null))'

    # allow for a bash to be executed as such:
    alias bash='SHELL=/bin/bash SETSHELLINPROFILE=no bash'

;; # zsh
esac


if [ "$SETSHELLINPROFILE" = "yes" -a "$RUNNING_SHELL" != "$SHELL" ]; then
# once we end up here with a tcsh, just pretend to be dumb:
if [ "$DEBUG_MODE" = "yes" ];then
  echo Warning: \$SHELL is set to $SHELL.
fi
    OSHELL="$SHELL"
    SHELL="$RUNNING_SHELL"
    echo "\$SHELL changed from $OSHELL to $SHELL"
    unset OSHELL
  fi


if [ "$DEBUG_MODE" = "yes" ];then
    echo SectA case SHELL in end
fi

if [ -d $HOME/tmp ]; then
  export TMPDIR=$HOME/tmp
else
  export TMPDIR=$HOME
fi
export TMP=$TMPDIR
export TEMP=$TMPDIR

#-------------------------------------------------------------------------
# SectB: env.
#-------------------------------------------------------------------------
if [ "$DEBUG_MODE" = "yes" ];then
echo SectB
fi


export KDEDIR=/opt/kde3

if [ "$LANG" = "" -o "$LANG" = "en_US.UTF-8" ]; then
	export LANG=C
fi

PATH=/usr/local/bin:/usr/local/sbin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/opt/kde/bin:/opt/kde3/bin
for DIR in /usr/openwin/bin /usr/lib/java/bin ~/bin ~/usr/bin ; do
    test -d $DIR && PATH=$PATH:$DIR
done
test "$UID" = 0 || PATH="$PATH:."
export PATH

MANPATH=/usr/man:/usr/share/man:/usr/X11R6/man:/usr/local/man:/opt/kde/man
for DIR in /usr/openwin/man /usr/man/allman /usr/man/de /usr/man/allman/de ; do
    test -d $DIR && MANPATH=$MANPATH:$DIR
done
export MANPATH

if [ -d /usr/openwin ] ; then
    OPENWINHOME=/usr/openwin
    export OPENWINHOME
fi

EDITOR='vi'
export EDITOR
PAGER=less
export PAGER
MAILCHECK=60
export MAILCHECK
#LC_CTYPE=ISO-8859-1
#export LC_CTYPE
POVRAYOPT=-l/usr/lib/povray/include
export POVRAYOPT
TEXINPUTS="$TEXINPUTS:~/.TeX:/usr/doc/.TeX"
export TEXINPUTS
XADIDIR="/usr/X11R6/lib/xad"
export XADIDIR
DV_IMMED_HELP=/usr/lib/dvgt_help
export DV_IMMED_HELP
MAPLE=/usr/lib/maple
export MAPLE
RASMOLPATH="/usr/lib/rasmol"
export RASMOLPATH
GR_HOME="/usr/lib/xmgr-3.00"
export GR_HOME
DMARSCONF=/usr/lib/mars_e
export DMARSCONF
CRPATH=/usr/lib/crisp/macros
export CRPATH
QTDIR=/usr/X11R6/lib/qt
export QTDIR  
LESSCHARSET=latin1
export LESSCHARSET
LESS=-M
export LESS
LESSKEY=/etc/lesskey.bin
export LESSKEY
LESSOPEN="|lessopen.sh %s"
export LESSOPEN
#MINICOM="-c on"
#export MINICOM
# Midnight Commander needs this to run in color mode
COLORTERM=1
export COLORTERM
# set INFOPATH to tell xemacs where he can find the info files
INFODIR=/usr/info:/usr/local/info
INFOPATH=$INFODIR
export INFODIR INFOPATH

export RSYNC_RSH="/usr/bin/ssh -a -x"
#export blackhole=blackhole.pca.dfn.de
export ispriv='^(-r.s.*|-r....s.*)'


# ================= Parking ====================
# some applications do not handle the XAPPLRESDIR environment correctly,
# when it contains more than one directory. More than one directory only
# makes sense, when you have a client with a nfs mounted /usr and want
# to configure applications machine dependend. Uncomment if you want this.
#
#XAPPLRESDIR="$XAPPLRESDIR:/var/X11R6/app-defaults:/usr/X11R6/lib/X11/app-defaults"
#export XAPPLRESDIR
#TOP_COLORS="2,$UID,01;33;40%3,S,08;37;40%3,R,08;36;40%3,T,05;31;40%3,N,08;32;40"
#export TOP_COLORS
#export LD_LIBRARY_PATH=/opt/kde/lib
#export PATH=$PATH:/opt/kde/bin
# this stuff is recommended for old motif apps. (like netscrap)
#XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB
#export XKEYSYMDB
#XNLSPATH=/usr/X11R6/lib/X11/nls
#export XNLSPATH
# time until a complete key sequence must have arrived
# ESCDELAY=2000
# export ESCDELAY
# See metamail(1). Run metamail also as root.
#MM_RUNASROOT=true
#export MM_RUNASROOT
# for rcs
# export VERSION_CONTROL=numbered
# ================= End Parking ====================


#-------------------------------------------------------------------------
# SectC: extended env:
#-------------------------------------------------------------------------
if [ "$DEBUG_MODE" = "yes" ];then
echo SectC
fi

if [ -f /etc/nntpserver ]; then
    NNTPSERVER=`cat /etc/nntpserver 2> /dev/null`
    export NNTPSERVER
fi

# for all programs that use the GNU readline library (bash, gdb)
if ! test -f ~/.inputrc ; then
  INPUTRC=/etc/inputrc
  export INPUTRC
fi


if [ -x /usr/bin/dircolors ] ; then
  # set up the color-ls environment variables:
  if test -f ~/.dir_colors ; then
    eval `dircolors -b ~/.dir_colors`
  elif test -f /etc/DIR_COLORS ; then
    eval `dircolors -b /etc/DIR_COLORS`
  fi
fi

# do not save dupes in the bash history file
HISTCONTROL=ignoredups
export HISTCONTROL

# make sure that teTeX is found.
if [ -f /usr/lib/teTeX/texmf.cnf ] ; then
    TETEXDIR=/usr/lib/teTeX
    PATH=$PATH:/usr/bin/TeX
    export TETEXDIR PATH
fi




#-------------------------------------------------------------------------
# SectD: aliases and functions.
#-------------------------------------------------------------------------
if [ "$DEBUG_MODE" = "yes" ];then
echo SectD
fi
# Further options for the 'ls' command are in /etc/DIR_COLORS.
alias ls='ls --color=tty'
alias dir='ls -l'
alias ll='ls -l'
alias la='ls -la|more'
alias l='ls -alF'
alias ..='cd ..'
alias ...='cd ../..'
alias unix2dos='recode lat1:ibmpc'
alias dos2unix='recode ibmpc:lat1'
alias unzip='unzip -L'
alias which='type -p'
alias c='clear'
alias kk='ls -la'
alias oo='ls -l'
alias f='finger'
alias iosc='osc -A https://api.suse.de'

alias pp='ps faux'
alias alpp='ps fauxw|grep -v "^root  "'
alias mypp='ps fauxw|grep "^$USER "'
alias buffer='/usr/bin/buffer -s 500k'
alias jpico='jpico -backpath ~/backup/ -tab 4 -indentc 32 -istep 4 -linums'

function cds {
    cd $1 && ls -la 
}

function startx { 
    exec /usr/X11R6/bin/startx $* 2>&1 | tee ~/.X.err ; 
}

# I had problems using 'eval tset' instead of 'TERM=', but you might want to 
# try it anyway. I think with the right /etc/termcap it would work great.
# eval `tset -sQ "$TERM"`
if [ "$TERM" = "" -o "$TERM" = "unknown" ]; then
 TERM=linux
fi

if [ -f /etc/organization ]; then
    ORGANIZATION="`cat /etc/organization`"
    export ORGANIZATION
fi


export ispriv='^(-r.s.*|-r....s.*)'

#-------------------------------------------------------------------------
# SectE1: prompt settings. xterm title hacks in SectE2!
#-------------------------------------------------------------------------
if [ "$DEBUG_MODE" = "yes" ];then
echo SectE
fi

bel=""
cr="
"
esc=""
id="`/usr/bin/id`"

case "$id" in
*id\=0*) ps="\# ";;
     *) ps=" ";;
esac


BOLD_M_ON="${esc}[1m"
BOLD_M_OFF="${esc}[m"
blue="${esc}[34m"
magenta="${esc}[35m"
red="${esc}[31m"
green="${esc}[32m"
cyan="${esc}[36m"
col_rmall="${esc}[m"


#export bel cr esc id ps BOLD_M_ON BOLD_M_OFF



#   sequence: switch ( $SHELL ) 
#               switch ( $UID )
#                 switch ( $TERM )
#                   set PS1

case "$RUNNING_SHELL" in
*bash)
    case "$id" in
    *id\=0*)
        PS1="(\[$BOLD_M_ON\]root@$HOST:\$PWD ) # \[$BOLD_M_OFF\]"
    ;;
    # NOT root...
    *)
        PS1="(\[$BOLD_M_ON\]\t\[$BOLD_M_OFF\] \u@$HOST:\w ) "
    ;; 
    esac

# done with bash.
;;
*ksh)
    # ksh specific setup:
    export ENV=/etc/profile
    . /usr/local/etc/kshrc
    test -r $HOME/.kshrc && ENV=$HOME/.kshrc
    case "$id" in
    *id\=0*)
echo ksh and id is 0
        case "$TERM" in
            xterm|xterm*|aixterm|dtterm|hpterm|vt320)
                PS1="$esc]2;*** root@$HOSTNAME:\$PWD ***$bel$esc]1;**root**@$HOST$bel$cr$BOLD_M_ON**root**@$HOST:\$PWD # $BOLD_M_OFF"
            ;;
            *)
                PS1="$BOLD_M_ON**root**@$HOST:\$PWD # $BOLD_M_OFF"
            ;;
        esac
    ;;
    # NICHT root...
    *)
        case "$TERM" in
        xterm|xterm*|aixterm|dtterm|hpterm|vt320)
            PS1="$esc]2;$LOGNAME@$HOSTNAME:\$PWD$bel$esc]1;$HOST$bel$cr$BOLD_M_ON$HOST$BOLD_M_OFF:\$PWD$ps"
        ;;
        *)
            PS1="$BOLD_M_ON$LOGNAME@$HOST$BOLD_M_OFF:\$PWD$ps"
        ;;
        esac    # case $TERM
    esac        # case $UID
;;

*zsh) # zsh specific setup with all these nice features is done further up.
      # here is only the prompt setup.
    case "$id" in
        *id\=0*)
	    prompt="(%B%T root@%m:%b%/ ) # "
        ;;
	*)
	    prompt="(%U%T%u %n@%m:%B%{${cyan}%}%~%{${col_rmall}%}%b ) "
	;;
    esac
;;
esac            # case $SHELL

PS2='> '
ignoreeof=0
export PS1 PS2 ignoreeof


#-------------------------------------------------------------------------
# SectE2: xterm title hacks
#-------------------------------------------------------------------------
if [ ! -z "$SSH_AUTH_SOCK" ]; then
    AGENT="SSH-AGENT "
fi
case "$TERM" in
xterm|xterm*|aixterm|dtterm|hpterm|vt320)
    case "$id" in
	*id\=0*)
	    case "$SHELL" in
	    *bash) # set PROMPT_COMMAND to do the job.
		PROMPT_COMMAND='echo -n "$esc]2;${AGENT}**** $DISPNUM root@$HNAME:$PWD   ****$bel$esc]1;root@$HNAME$bel"'
	    ;;
	    *ksh) # ksh does not have a mechanism for this luxury. Forget about it.
	    ;;
	    *zsh) # precmd
		function precmd() { echo -n "$esc]2;${AGENT}**** $DISPNUM root@$HNAME:$PWD   ****$bel$esc]1;root@$HNAME$bel" ; }
	    ;;
	    esac
	;;
	*)
            case "$SHELL" in
            *bash) # set PROMPT_COMMAND to do the job.
		PROMPT_COMMAND='echo -n "$esc]2;${AGENT}$DISPNUM $LOGNAME@$HNAME:$PWD$bel$esc]1;$HNAME$bel"'
            ;;
            *ksh) # ksh does not have a mechanism for this luxury. Forget about it.
            ;;
            *zsh) # precmd
		function precmd() { echo -n "$esc]2;${AGENT}$DISPNUM $LOGNAME@$HNAME:$PWD$bel$esc]1;$HNAME$bel" ; }
            ;;
            esac
	;;
    esac # "$id"
;;
*)
    case "$SHELL" in
    *bash) 
	unset PROMPT_COMMAND
    ;;
    *ksh) # ksh does not have a mechanism for this luxury. Forget about it.
    ;;
    *zsh)
	#unfunction precmd
    ;;
    esac
;;
esac # "$TERM"

	
	

#-------------------------------------------------------------------------
# SectF: local settings.
#-------------------------------------------------------------------------
if [ "$DEBUG_MODE" = "yes" ];then
echo SectF
fi

MAIL=/var/spool/mail/$USER
export MAIL

#DSMDIR=/usr/adsm
#export DSMDIR
#PATH=${PATH}:$DSMDIR


# yeah.
# alias id='echo "uid=0(root) gid=(root)"'

PRINTER='lp'
export PRINTER
#http_proxy="http://proxy.suse.de:3128/"
#ftp_proxy="http://proxy.suse.de:3128/"
#http_proxy="http://proxy.suse.de:8080/"
#gopher_proxy="http://proxy.suse.de:3128/"
#wais_proxy="http://proxy.suse.de:3128/"
#no_proxy="suse.de"
#export http_proxy ftp_proxy gopher_proxy wais_proxy no_proxy 

if [ `id -u` -lt 1000 ]; then
  SYSTEMUSER=true
fi
if [ ! -f $HOME/.signature -a "$SYSTEMUSER" != true ]; then
  DOMAIN=$(hostname -f | cut -f2- -d.)
    (ypcat passwd; cat /etc/passwd) 2> /dev/null | awk -F: \
    "\$1==\"$LOGNAME\" { print \$5 \" <$LOGNAME@$DOMAIN>\"}" \
    > $HOME/.signature
fi


# If we have a terminal:
if [ -t 0 -a -t 1 ]; then
# Bugfix/Workaround for Solaris, HPUX. Never log in with termspeed=0.

if [ "$DEBUG_MODE" = "yes" ];then
echo stty
fi
#/bin/stty 9600
# Not necessary:
# stty erase '^h' cs8 -istrip
#frm -q -S
#case "$HOST" in
#    rzlin1*)
#if [ "$DEBUG_MODE" = "yes" ];then
#echo quota
#fi
#    /usr/bin/quota
#    ;;
#esac
fi

function setupbuild() {
. /work/src/bin/.profile
unset TMPDIR
if [ -d /ram0/lost+found ]; then
 RDP=/ram0
else
 RDP=/build
fi
echo "
 export BUILD_DIST=6.1-i386 ; export BUILD_ROOT=$RDP/build-6.1
 export BUILD_DIST=6.2-i386 ; export BUILD_ROOT=$RDP/build-6.2
 export BUILD_DIST=6.3-i386 ; export BUILD_ROOT=$RDP/build-6.3
 export BUILD_DIST=6.4-i386 ; export BUILD_ROOT=$RDP/build-6.4
 export BUILD_DIST=7.0-i386 ; export BUILD_ROOT=$RDP/build-7.0
 export BUILD_DIST=7.1-i386 ; export BUILD_ROOT=$RDP/build-7.1
 export BUILD_DIST=7.2-i386 ; export BUILD_ROOT=$RDP/build-7.2
 export BUILD_DIST=7.3-i386 ; export BUILD_ROOT=$RDP/build-7.3
 export BUILD_DIST=8.0-i386 ; export BUILD_ROOT=$RDP/build-8.0
 export BUILD_DIST=8.1-i386 ; export BUILD_ROOT=$RDP/build-8.1
 export BUILD_DIST=8.2-i386 ; export BUILD_ROOT=$RDP/build-8.2
 export BUILD_DIST=i386 ; export BUILD_ROOT=$RDP/build-i386
"
}

