function upload_to_personal_space() {
    scp $1 people:public_html/
}

function kill_my_docker_containers() {
    docker ps | grep $USER | awk '{print $1;}' | xargs docker kill
}

function list_processes() {
    # List all processes matching the given process name string
    ps aux | grep $(whoami) | grep -i "$1" | grep -v grep
}

function kill_all() {
    # Kill all processes matching the given process name string
    if [ ! "$1" ]; then
        echo "kill_all requires a process name to kill all instances of"
        return 1
    fi
    kill_flag=15
    if [ "$15" ]; then
        kill_flag="$15"
    fi

    count=$(list_processes "$1" | wc -l)

    echo "Killing $count running processes matching $1"
    kill -"$kill_flag" $(list_processes "$1" | awk '{print $2}')
    
    count=$(list_processes "$1" | wc -l)
    if [ $count -gt 0 ]; then
        echo "$count processes could not be killed"
    fi
}

function ranger-cd() {
    # Make shell cd to ranger directory
    tempfile=$(mktemp)
    ranger --choosedir="$tempfile" "${@:-$(pwd)}" < $TTY
    test -f "$tempfile" &&
    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
        cd $(cat $tempfile)
    fi
    zle reset-prompt
    printf '\n'
    rm -f -- "$tempfile"
}

function terminal-clock() {
    tty-clock -tcb < $TTY
    zle reset-prompt
    printf '\n'
}

function check_dotfiles_version {
    # Check if the version of the dotfiles directory is the latest version
    # Return 0 if repo is present and the latest version
    # Return 1 if the repo is not present
    # Return 2 if the repo is behind the remote
    # Return 3 if the repo is ahead of the remote
    # Return 4 if the repo has diverged from the remote
    if [ ! -d $HOME/dotfiles ]; then
        return 2
    fi

    pushd $HOME/dotfiles

    git fetch origin
    UPSTREAM=${1:-'@{u}'}
    LOCAL=$(git rev-parse @)
    REMOTE=$(git rev-parse "$UPSTREAM")
    BASE=$(git merge-base @ "$UPSTREAM")

    popd

    if [[ $LOCAL = $REMOTE ]]; then
        return 0
    elif [[ $LOCAL = $BASE ]]; then
        return 2
    elif [[ $REMOTE = $BASE ]]; then
        return 3
    else
        return 4
    fi
}
