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

if test "$#" -eq 0 -a -t 1
then echo 1>&2 "$$: Running 'env'"
  : 'This obviously must not be exported'
  INTERACTIVE=yes
fi

: '
  Copyright (C) 1991 PeterG. This program is free software: you
  can redistribute it and/or modify it under the terms of the
  GNU General Public License as published by the Free Software
  Foundation, either version 2 of the License, or (at your
  option) any later version. This program is distributed in the
  hope that it will be useful, but WITHOUT ANY WARRANTY; without
  even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  PARTICULAR PURPOSE.
'

: '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*)
  case "$LOCAL;$SHARE" in
  ?*';'*)
    test -e "$LOCAL/lib/sh/dirs"	&& . "$LOCAL/lib/sh/dirs"
    test -e "$LOCAL/lib/sh/nc"		&& . "$LOCAL/lib/sh/nc";;
  *';'?*)
    test -e "$SHARE/lib/sh/dirs"	&& . "$SHARE/lib/sh/dirs"
    test -e "$SHARE/lib/sh/nc"		&& . "$SHARE/lib/sh/nc";;
  esac;;

[kz]sh*)
  case "$LOCAL;$SHARE" in
  ?*';'*)
    test -e "$LOCAL/lib/sh/nc"		&& . "$LOCAL/lib/sh/nc";;
  *';'?*)
    test -e "$SHARE/lib/sh/nc"		&& . "$SHARE/lib/sh/nc";;
  esac;;

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'; };;

*)
  typeset () { : 'nop unfortunately'; };;
esac

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

: 'These are too useful'
 
lv() { 'ls' -C		${LS_I+-I "$LS_I"} ${1+"$@"}; }
lx() { 'ls' -x		${LS_I+-I "$LS_I"} ${1+"$@"}; }
lc() { 'ls' --color     ${LS_I+-I "$LS_I"} ${1+"$@"}; }
lf() { 'ls' --file-type ${LS_I+-I "$LS_I"} ${1+"$@"}; }

dt() { 'df' -T -BG	$DF_OPTS ${1+"$@"}; }
dl() { 'df' -T -BG -l	$DF_OPTS ${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+"$@"}; }
ec() { emacsclient -n -c  ${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() \
{
  if ! test -e /usr/bin/id
  then
    case "$USER" in
    'root')	echo '#';;
    *)		echo '%';;
    esac
  else
    case "`/usr/bin/id -u`" in
    0)		echo '#';;
    *)
      case "$1" in
      *'# '*)	echo '#';;
      *'$ '*)	echo '$';;
      ?*)	expr "$1" : '.*\([^ ]\) *';;
      *)	echo '$';;
      esac;;
    esac
  fi
}

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

: 'Useful for SSH'

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

: 'Regrettably k-i may allow main-in-the-middle'
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"; }

: '
  Set SCREEN tab title:	\033kMESSAGE\033\134
  Set window title:	\033]0;MESSAGE\007
  Set tab title:	\033]30;MESSAGE\007
  http://meta.ath0.com/2006/05/24/unix-shell-games-with-kde
  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 
  printf "\033k$*\033\\"
}

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

xtsize() \
{
   printf "\033[8;${X-80};${Y-34}t"
}

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

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"`
  printf "\033[28;${RGB}t"
}

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

  export PS PS0 PS1 PS2 PS3 PS4

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

  case "$0" in [^-]*)
    PS="$PS "; 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 -e '/bin/dircolors' -o -e '/usr/bin/dircolors'
    then
      case "$LS_COLORS" in '')
      	export LS_COLORS
	if test -e /etc/DIR_COLORS
	then eval `dircolors /etc/DIR_COLORS`
	else eval `dircolors`
	fi;;
      esac
    fi
  fi;;

esac

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