#######################################################################
### .kshrc for pdksh|mksh|ksh93                                     ###
#######################################################################
### by Peter R Tynan                                                ###
#######################################################################
### Thank you to mirabilos and lewellyn                             ###
### from #ksh on freenode for help with this                        ###
#######################################################################

#######################################################################
### Set editors
#######################################################################
EDITOR='/bin/ed'
VISUAL='/usr/pkg/bin/nvi'
export EDITOR VISUAL


#######################################################################
### Set local                                                       ###
#######################################################################
# Set TimeZone
TZ='Europe/London'

# Set character set
unset LC_ALL
LC_ALL="en_GB"
LANG="en_GB"
LC_CTYPE="en_GB.ISO8859-15"
LC_TIME="en_GB.ISO8859-15"
LC_NUMERIC="en_GB.ISO8859-15"
LC_MONETARY="en_GB.ISO8859-15"
LC_MESSAGES="en_GB.ISO8859-15"

# Get perl to shut up about the LANG variable with utf-8
PERL_BADLANG='0'

# Export local environmental variables 
export TZ LC_ALL LANG LC_CTYPE LC_COLLATE LC_TIME LC_NUMERIC LC_MONETARY \
LC_MESSAGES PERL_BADLANG


#######################################################################
### Set the default permissions                                     ###
#######################################################################
umask 0077


#######################################################################
### skip this setup for non-interactive shells                      ###
#######################################################################
if [[ -n $KSH_VERSION ]]; then
	# pdksh (or some derivate thereof) might not know [[ -o â€¦ ]]
	[[ $- = *i* && -t 0 ]] || return
else
	[[ -o interactive && -t 0 ]] || return
fi


#######################################################################
### Confirm the terminal type                                       ###
#######################################################################
echo && echo && eval $(tset -m ansi:ansi -m $TERM:?${TERM:-ansi} -r -s -Q)

# If job control is enabled, set the suspend character to ^Z (control-z):
case $- in
(*m*)	stty susp '^Z'
	;;
esac


#######################################################################
### Make sure $USER and $HOST are set                               ###
#######################################################################
[[ -n $USER ]] || USER=$(id -un)
[[ -n $HOST ]] || HOST=$(uname -n)


#######################################################################
### Colour or Mono prompt                                           ###
#######################################################################
## Parts of the shell testing script stolen from Thorsten Glaser     ##
## and subject to                                                    ##
#######################################################################
# Copyright (c) 2009,  2010 Thorsten Glaser                           #
# 	<tg@mirbsd.org>                                               #
#                                                                     #
# Provided that  these terms and disclaimer  and all                  #
# copyright notices are retained or reproduced in an                  #
# accompanying  document, permission  is granted  to                  #
# deal in  this work without  restriction, including                  #
# unlimited   rights  to  use,   publicly   perform,                  #
# distribute,  sell, modify,  merge,  give away,  or                  #
# sublicence.                                                         #
#                                                                     #
# This work is provided "AS IS" and WITHOUT WARRANTY                  #
# of  any  kind,  to  the  utmost  extent  permitted                  #
# by  applicable law,  neither express  nor implied;                  #
# without malicious  intent or  gross negligence. In                  #
# no event may a  licensor, author or contributor be                  #
# held  liable for  indirect, direct,  other damage,                  #
# loss, or  other issues arising  in any way  out of                  #
# dealing  in  the  work,  even if  advised  of  the                  #
# possibility  of  such  damage or  existence  of  a                  #
# defect, except proven that  it results out of said                  #
# person's immediate  fault when  using the  work as                  #
# intended.                                                           #
#                                                                     #
# see    https://www.mirbsd.org/cvs.cgi/contrib/code                  #
# /Snippets/getshver?rev=HEAD for details                             #
#######################################################################

echo "Do you want a colour prompt (yes or no)"
read c_answer
case $c_answer in
([Yy]|[Yy][Ee][Ss])
	c_answer="y"
	;;
([Nn]|[Nn][Oo])
	c_answer="n"
	;;
(*)
	c_answer="n"
	;;
esac

# mksh, mirbsdksh (pre-mksh), oksh (3.7+), pdksh
case $KSH_VERSION in
(*MIRBSD\ KSH*) _kver="mksh";;
(*PD\ KSH*MirOS*) _kver="pdksh";;
(*PD\ KSH*) _kver="pdksh";;
esac

# ksh93
x=.sh.version
(eval x=\$\{$x\}) 2>&- || x=fail
if test fail != "$x"; then
	eval _kver="ksh93"
fi

# ksh88
if [[ $_kver == "" ]]; then if (
	typeset _p

	_p='f+([ o])bar'
	eval [[ foobar = $_p '&&' \"foo bar\" = $_p ]]
) 2>&-; then
	_kver="ksh88"
fi;fi

# and now to set the PS1
if [[ $c_answer = "y" ]];then
	case $_kver in
	(pdksh|ksh88)
		PS1='ÿÿ[33mÿ'$(uname -n)\
' (ÿ[0mÿ${PWD##/*/}ÿ[33mÿ) [ÿ[0mÿ!ÿ[33mÿ]ÿ[0mÿ % '
		;;
	(mksh|ksh93)
		PS1='[33m'$(uname -n)\
':[0m${PWD/#$HOME/\~}[33m [[0m![33m][0m % '
		;;
	(*)
		PS1='% '
		;;
	esac
else
	case $_kver in
	(pdksh|ksh88)
		PS1=$(uname -n)' (${PWD##/*/}) [!] % '
		;;
	(mksh|ksh93)
		PS1=$(uname -n)':${PWD/#$HOME/\~} [!] % '
		;;
	(*)
		PS1='% '
		;;
	esac
fi

## set other prompts
PS2='==> '
PS3='===? '
PS4='[$LINENO]+ '


#######################################################################
### Set other environmental variables                               ###
#######################################################################
# This sets the environment variable "MAIL" for the location of your
# email file.  Typically email is spooled in /usr/mail with the user's
# login (enviroment variable LOGNAME) for the file's name.
MAIL=/mail/${LOGNAME:?}
MAILCHECK=86400

# Set History file
case $_kver in
(pdksh|mksh)
	HISTFILE=~/.history_pdksh
	;;
(*)
	HISTFILE=~/.history_ksh
	;;
esac
HISTSIZE=1024
HISTEDIT=$EDITOR

# Set Spellcheck 
SPELL="/usr/pkg/bin/ispell"

# Set default dictionary for ispell
DICTIONARY="britishxlg"

# This includes the programs in my ~/bin directory in the
# shell's search path:
PATH=$PATH:$HOME/bin

# Set Pager
if [[ $c_answer = "y" ]];then
	PAGER='/usr/pkg/bin/most +u'
else
	PAGER='/usr/pkg/bin/most -C +u'
fi

# Set default www browser
BROWSER=/usr/pkg/bin/elinks

# Set homepage for www.browsers
WWW_HOME=https://duckduckgo.com/lite

# Set colours for ls command
LS_COLORS="ow=01;93:di=00;93:ln=00;33"

# GZIP
GZIP=-9

# GREP
GREP_OPTIONS='-d skip'

if [[ $c_answer = "y" ]];then
	GREP_COLOR='01;33'	# colour (bright yellow)
else
	GREP_COLOR=7		# mono (inverse video)
fi

# avoid certain file types in completion
FIGNORE='@(*.o|~*)'

# Set the PARINIT environment variable for par
PARINIT="rTbgqR B=.,?_A_a Q=_s>|"


#######################################################################
### Export the environmental variables                              ###
#######################################################################
export PS1 PS2 PS3 PS4 MAIL MAILCHECK HISTFILE HISTSIZE \
HISTEDIT SPELL DICTIONARY PATH PAGER BROWSER WWW_HOME LS_COLORS \
GZIP GREP_OPTIONS GREP_COLOR FIGNORE PARINIT


#######################################################################
### Set up Alias                                                    ###
#######################################################################
alias showa='alias -p | $PAGER'
alias \!\!='fc -e -'
alias bye='tput clear && exit'
alias logout='tput clear && exit'
alias cls='tput clear && date && webquota'
alias readme='cat -n README | $PAGER'
alias who='who -u | sort | pg -e'

alias alpine='alpine -z'
alias pine=alpine
alias ed='ed -p:'
alias pilot='pilot -am -n300'
alias pgr='$PAGER'

alias pu='ps -lu $USER'
alias cp='cp -i'
alias mv='mv -i'
alias del='rm -i'
alias md=mkdir
alias rd=rmdir
alias df='df -h'
alias list='file -psz * | sort | pg -e'
alias psme='echo && ps aux | /usr/bin/grep --color=auto $USER && echo '
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias up='cd ../'

case $c_answer in
(n)
	## mono Alias
	alias mc='mc -abcx'
	alias l='gls --color=never --group-directories-first -F -l -s -a -h'
	alias ll='gls --color=never --group-directories-first -h -l -F'
	alias la='gls --color=never --group-directories-first -F -G -s -a -h'
	alias lf='gls -Ftr --color=never|grep /|grep -v RCS'
	alias ls='gls --color=never --group-directories-first -F'
	alias dir=\
'gdir --color=never --group-directories-first --ignore-backups -F'
	alias vdir=\
'gvdir --color=never --group-directories-first --ignore-backups -F -h'
	alias lstree=\
'gls --color=never --group-directories-first -R -F -G | less -R'
	;;
(y)
	## colour Alias
	alias mc='mc -acx'
	alias l='gls --color=auto --group-directories-first -F -l -s -a -h'
	alias ll='gls --color=auto --group-directories-first -h -l -F'
	alias la='gls --color=auto --group-directories-first -F -G -s -a -h'
	alias lf='gls -Ftr --color=auto|grep /|grep -v RCS'
	alias ls='gls --color=auto --group-directories-first -F'
	alias dir=\
'gdir --color=auto --group-directories-first --ignore-backups -F'
	alias vdir=\
'gvdir --color=auto --group-directories-first --ignore-backups -F -h'
	alias lstree=\
'gls --color=always --group-directories-first -R -F -G | less -R'
	;;
esac


#######################################################################
### Set other shell options                                         ###
#######################################################################
set -o gmacs		# Set emacs mode
set -o ignoreeof	# do not let control-d logout, 
			# iffy D key on my keyboard
set -o bgnice		# nice background processes
set -b			# immediate background job reporting


#######################################################################
### Display the host and shell in X-Terminal titles                 ###
#######################################################################
print -n "\033]2;$(uname -n) - $_kver\007"


#######################################################################
### Experimental                                                    ###
#######################################################################
# case $_kver in
# (ksh93)
#	# The built-in editors will use multiple lines on the screen for 
#	# lines that are longer than the width of the screen. This may 
#	# not work for all terminals.
#	set -o multiline
# esac


#######################################################################
### Fun Stuff                                                       ###
#######################################################################
echo
phoon -l 7
echo
echo
ruptime
echo
quota
webquota
echo
uinfo $USER; id $USER
echo
echo -n "Logged on at "
date
echo -n "You are now connected to "$HOST
SSH_IP=$(echo "${SSH_CLIENT%% *}") 
if [ $SSH_IP ] ; then
	echo " via a encyrpted connection from "$SSH_IP # SSH connection
else
	echo " via a unencyrpted connection,"	# NOT an SSH connection
fi
echo "your shell is "$_kver
echo
/usr/pkg/bin/dues -p
# /usr/pkg/bin/vote -c
/usr/pkg/bin/notes -c
# echo
# fortune -aes | cowsay -n
echo
echo
