# vim:ft=sh:fdm=marker
# Configuration for the `minimal-shell' function of grml's zsh setup.
# That function spawns a `mksh' shell with this file as its configuration.

# skip this setup for non-interactive shells
[[ -o interactive && -t 0 ]] || return

export PATH="/bin:/sbin/:/usr/bin/:/usr/sbin:/usr/local/bin:/usr/local/sbin:${HOME}/bin:/opt/bin"
export EDITOR='ed'

if [[ -x $(which vim) ]] ; then
    export VISUAL='vim'
else
    export VISUAL='vi'
fi
if [[ -x $(which less) ]] ; then
    export PAGER='less'
else
    export PAGER='more'
fi

# some options.
set -o bgnice        \
    -o braceexpand   \
    -o emacs         \
    -o markdirs      \
    -o monitor       \
    -o nohup         \
    -o trackall

# aliases for various command shortcuts
alias ls='command ls --color=auto'  # we're on grml, so we do have GNU ls.
alias ll='command ls -l --color=auto'
alias la='command ls -la --color=auto'
alias pu='ps -fu $USER'

## Keybindings
bind '^L=clear-screen'
if [[ -x $(which infocmp) ]] && [[ -x $(which sed) ]] ; then
    # Reading some special keys from terminfo database. If `infocmp'
    # cannot be found, we rely on mksh to do the right thing.
    infocmp -x1L | sed '/^\t[^k]/d;/^[^\t]/d;s/\t//;s/,$//' | \
        while read -r line ; do
            key="${line%=*}"
            [[ "${key}" != k* ]] && continue
            seq="$(print -r -- "${line#*=}" | sed -e 's,\\\([1-7][0-7]*\),\\0\1,g')"
            seq="$(print "${seq}")"
            case "$key" in
                key_left)      bind "${seq}"='backward-char'        ;;
                key_right)     bind "${seq}"='forward-char'         ;;
                key_up)        bind "${seq}"='up-history'           ;;
                key_down)      bind "${seq}"='down-history'         ;;
                key_ppage)     bind "${seq}"='search-history-up'    ;;
                key_npage)     bind "${seq}"='search-history-down'  ;;
                key_home)      bind "${seq}"='beginning-of-line'    ;;
                key_end)       bind "${seq}"='end-of-line'          ;;
                key_backspace) bind "${seq}"='delete-char-backward' ;;
                key_dc)        bind "${seq}"='delete-char-forward'  ;;
            esac
        done
fi

function _lrv {
    local rv="$?"
    if [[ "${rv}" != 0 ]]; then
        print -- "[${rv}]-"
    fi
    return 0
}
# put the current directory and history number in the prompt.
# use a wrapper for 'cd' to get '~' instead of /home/foo/.
function _cd {
    "cd" "$@"
    [[ $PWD = $HOME* && $HOME != / ]] && _pwd=\~"${PWD#$HOME}" && return
    _pwd="$PWD"
}
alias cd=_cd
_cd .
PS1='$(_lrv)(!)-$_pwd'
if (( USER_ID )); then
    PS1="$PS1"'\$ '
else
    PS1="$PS1"'# '
fi

HISTSIZE=500
HISTEDIT="$EDITOR"
# This is the default in mksh, but we make sure in case people tempered
# with it in their environment.
unset HISTFILE
true
