# -*-sh-*-

# these functions _must_ be portable !!!


under_bash(){
 [ ! -z $BASH_VERSION ]
 # [ "$SHELL" = "/bin/bash" -o  "$SHELL" = "/bin/sh" -o "$SHELL" = "/bin/rbash" ]
}

under_zsh(){
  [ ! -z $ZSH_VERSION ]
 #[ "$SHELL" = "/bin/zsh" ]
}


under_xterm(){
#echo "under xterm $TERM"
#
[ "$TERM" = "xterm" -o  "$TERM" = "rxvt"  -o  "$TERM" = "rxvt-unicode" -o "$TERM" = "xterm-256color" ]
# [[ $TERM == xterm ]]
}

relax(){
    echo -n ""
}

# todo: remove
my_source() {
   #echo "source $1...."
   [[ -e $1 ]] && source $1
   # source $1
   #echo "\t...end (source $1)"
}

# source if exists:
possibly_source()
{
    [ -r $1 ] && source $1
}




shell_interactive(){
if under_bash; then
   true;
   # set -o history
   # or interactive_comments

else
    if under_zsh
    then
        if [[ -o interactive ]]; then
            true;
        else
            false
        fi
    else
    # other shells are not interactive!
      false
    fi
fi
}

#
fpath+=/usr/share/zsh/site-functions/Misc

autoload -Uz run-help-podman
autoload -Uz run-help-docker


#from kill-hook
fpath+=/usr/share/zsh/site-functions
autoload -Uz kill_region_hook


# From maruska-preference:

function hist()
{
    grep -a -r $1 ~/.zsh_history
}

function cd ()
{
    if  [[ $# -gt 0 ]] && [[ ! "$1" = "-" ]] && [[  -f "$1" ]]; then
        builtin cd $1:h
    else
#     echo "not a file"
        builtin cd $*
    fi
}

# todo: from a specific directory. And provide common recipes.
alias zl="zload-file /usr/share/maruska/recipes/"

function prehistory()
{
    HISTFILE=$1
    fc -R
}

function rehistory()
{
    HISTFILE=$1
    #/home/michal/.zsh_history/git-xorg
    fc -W
}




# Clint helped
alias ,="load_buffer"
alias ,,=", ${activity_dir}zsh/snippet"
# fixme: only if changed  between now, and last invokation/command?
[ -x  ${activity_dir}/zsh/subst ] && .  ${activity_dir}/zsh/subst


PERIOD=60
function periodic ()
{
#    echo "running periodic"
    if [ -r ~/.aliases ]
    then
        source ~/.aliases
    fi
    rehash
}

append_to_path()
{
    # append_to_array path $1
    if [[ ${path[(i)$1]} = $(( ${#path} + 1 )) ]];
    then
        path=($path $1)
    fi
}
