if [ "$SSH_SESSION_ID" != "" ]; then
    mkdir -p /tmp/ssh_sessions
    echo $$ > /tmp/ssh_sessions/"$SSH_SESSION_ID"
fi

if [ "$REMOTE_PATH" != "" ]; then
    if [ -d "$REMOTE_PATH" ]; then
        cd "$REMOTE_PATH"
    fi
elif [ "$PREVIOUS_SESSION_ID" != "" ]; then
    cd "$(readlink -e /proc/$(cat /tmp/ssh_sessions/$PREVIOUS_SESSION_ID)/cwd)"
fi

function run_waypipe() {
    SSH_SESSION_ID=$RANDOM
    REMOTE_PATH=$(echo $(pwd) | sed "s|/home/[a-z]*|\$HOME|g")
    SSH_SESSION_ID=$SSH_SESSION_ID waypipe ssh -t $1 env REMOTE_PATH=\"$REMOTE_PATH\" SSH_SESSION_ID=$SSH_SESSION_ID "zsh --login"
}
