#!/bin/sh
: 'shell startup 120214 -*- ksh -*- vim:set ft=sh noet ts=8 sw=2:'

: 'This is duplicated in "profile"'

export SHELL_VERSION
SHELL_VERSION='sh'

case "$KSH_VERSION" in
			*PD*)	SHELL_VERSION="pdksh $KSH_VERSION";;
			?*)	SHELL_VERSION="ksh $KSH_VERSION";;
esac
case "$BASH_VERSION" in	?*)	SHELL_VERSION="bash $BASH_VERSION";;	esac
case "$ZSH_VERSION" in	?*)	SHELL_VERSION="zsh $ZSH_VERSION";;	esac
case "$SHELL_VERSION;$OSTYPE" in
  		'sh;'*BSD)	SHELL_VERSION="bsdsh";;
esac

case "$SHELL_VERSION" in

pdksh*)
  test -f "$LOCAL/lib/sh/dirs"	&& . "$LOCAL/lib/sh/dirs"
  test -f "$LOCAL/lib/sh/nc"	&& . "$LOCAL/lib/sh/nc";;

[kz]sh*)
  test -f "$LOCAL/lib/sh/nc"	&& . "$LOCAL/lib/sh/nc";;

bash*)
  unalias -a
  set -o monitor
  shopt -s checkwinsize
  : 'shopt -u cmdhist hostcomplete'
  typeset -fx nc
  typeset -fx pushd popd cwd chdir dirs;;

bsdsh*)
  set -E
  typeset () { : 'nop unfortunately'; }

esac

: 'In the following the real command name must be quoted'

: 'These are too useful'

lv() { 'ls' -C ${1+"$@"}; }
lx() { 'ls' -x ${1+"$@"}; }
lc() { 'ls' --color     ${1+"$@"}; }
lf() { 'ls' --file-type ${1+"$@"}; }

dt() { 'df' -T -BG    ${1+"$@"}; }
dl() { 'df' -T -BG -l ${1+"$@"}; }

: '"Safe" file commands; so important they are defined here as a default'

re() { 'rm' -i ${1+"$@"}; }
mo() { 'mv' -i ${1+"$@"}; }
cy() { 'cp' -i ${1+"$@"}; }
lk() { 'ln' -i ${1+"$@"}; }

: '"gnuclient -q" is just too long and common'

gq() { gnuclient   -q ${1+"$@"}; }
en() { emacsclient -n ${1+"$@"}; }

: 'Important for symbolic links to directories'

pdot()  { /bin/pwd; }
cdot()  { PWD="`/bin/pwd`" && cd "$PWD" && echo "$PWD"; }

: 'This is very useful too for detaching/forking processes'

fork() \
{ 
  (
    case "$1" in '-h') shift;; ?*) cd;; esac
    case "$1" in ?*) ${1+"$@"} &;; esac
  )
}

: 'Set sensible prompts'

promptchar() \
{
  case "$1" in
  *'# '*) echo '#';;
  *'$ '*) echo '$';;
  ?*) expr "$1" : '.*\([^ ]\) *';;
  '') if test -x /usr/bin/id
      then case "`/usr/bin/id -u`" in 0) echo '#';; *) echo '$';; esac
      else echo ':'
      fi;;
  esac
}

prompts() \
{
  case "$1" in 
  ?*) PS0="${2-$HOST}${1} ";;
  '') PS0="${2-$HOST}`promptchar \"$PS1\"` ";;
  esac
  PS1="$PS0"; PS2="${2-$HOST}> ";
}

: 'Useful for SSH'

sshexec() { exec ssh-agent "$SHELL";  }

sshp()	  { ssh -o 'PreferredAuthentications=keyboard-interactive,password' ${1+"$@"}; }
rsyncp()  { rsync -e 'ssh -o PreferredAuthentications=keyboard-interactive,password' ${1+"$@"}; }

: 'Nice'

ku()	  { kfmclient openURL ${1+"$@"}; }

xkbmap()  { setxkbmap ${1+"$@"} -print | xkbcomp -w 0 - "$DISPLAY"; }

# http://meta.ath0.com/2006/05/24/unix-shell-games-with-kde
# Set SCREEN tab title:	\033kMESSAGE\033\134
# Set window title:	\033]0;MESSAGE\007
# Set tab title:	\033]30;MESSAGE\007
# Set tab title color:	\033[28;RGBt
# where RGB is in *decimal*.

stitle() \
{
  case "$#" in '0')	set "$PS1";;					esac
  case "$*" in \!|\@)	echo 2>&1 'No arguments or default!'; exit 1;;	esac 
  echo $ECHON "k$*\\$ECHOC"
}

xtitle() \
{
  case "$#" in '0')	set "$HOST"\!"$USER";;				esac
  case "$*" in \!|\@)	echo 2>&1 'No arguments or default!'; exit 1;;	esac 
  echo $ECHON "]0;$*$ECHOC"
}

ktitle() \
{
  case "$#" in '0')	set "$PS1";;					esac
  case "$*" in \!|\@)	echo 2>&1 'No arguments or default!'; exit 1;;	esac 
  echo $ECHON "]30;$*$ECHOC"
}

kcolor() \
{
  case "$#" in '0')	set 255 32 0;;					esac
  case "$*" in \!|\@)	echo 2>&1 'No arguments or default!'; exit 1;;	esac 
  RGB=`expr "$1" \* 65536 + "$2" \* 256 + "$3"`
  echo $ECHON "[28;${RGB}t$ECHOC"
}

case "$-" in *i*)
  INTERACTIVE=yes

  export PS0 PS1 PS2 PS3 PS4

  : 'PS0 is a work around BASH unsetting PS1 if "-c" is used, as in "xterm"'
  case "$PS0" in '') PS0="$PS1";; ?*) PS1="$PS0";; esac

  case "$0" in [^-]*)
    PS0="$PS0 "; PS1="$PS1 "; PS2="$PS2 "; PS3="$PS3 "; PS4="$PS4 ";;
  esac

  case "$SHELL_VERSION" in

  zsh*)
    set -o monitor
    setopt HIST_IGNORE_ALL_DUPS HIST_IGNORE_SPACE
    case "$TERM" in
    emacs)	set +Z;;
    *)		set -Z; bindkey -e;;
    esac;;

  ?*sh*)
    set -o monitor
    set -o noclobber

    case "$TERM" in
    emacs)	set -o vi +o vi;;
    *)		set -o emacs;;
    esac

    case "$SHELL_VERSION" in
    pdksh*)
      bind '^[[=prefix-2'
      bind '^XA=up-history'
      bind '^XB=down-history'
      bind '^XC=forward-char'
      bind '^XD=backward-char'
      bind '^I=complete';;
    esac;;

  esac

  if test -t 0
  then
    case "$USER;$HOST" in
    ?*';'?*)
      case "$TERM;$DISPLAY" in
      'xterm'*';'*)	xtitle;;
      'rxvt'*';'*)	xtitle;;
      'konsole'*';'*)	xtitle; ktitle;;
      ';'?*)		xtitle;;
      esac
    esac

    if test -f '/bin/dircolors' -o -f '/usr/bin/dircolors'
    then
      case "$LS_COLORS" in '')
      	export LS_COLORS
	if test -r /etc/DIR_COLORS
	then eval `dircolors /etc/DIR_COLORS`
	else eval `dircolors`
	fi;;
      esac

      unset ls; typeset -fx lc; lc() { /bin/ls --color ${1+"$@"}; }
    fi
  fi;;

esac

test "$HOME" -a -r "$HOME/.userenv"		&& . "$HOME"/.userenv
