#!/bin/sh
#
# Example script to start up tunnel with autossh.
#
# This script will tunnel 2200 from the remote host
# to 22 on the local host. On remote host do:
#     ssh -p 2200 localhost
#
# Recommends: notify-bin

set -e

action="$1"
remotehost="$2"
remoteport="${3:-2200}"

info() {
	if [ -n "$DISPLAY" ] && [ -e /usr/bin/notify-send ]; then
		notify-send "$1" "$2"
	else
		echo >&2 "$1"
		echo >&2 "$2"
	fi
}

case "$action" in
  start)
	info "Starting remote help..." "Establishing a connection tunnel from $remotehost into this machine..."
	;;
  stop)
	killall -KILL autossh
	info "Remote help is off!" "Connection tunnels into this machine is now inactive."
	exit $?
	;;
  *)
	echo 2>& "Wrong action passed to localautossh!"
	exit 1
	;;
esac

if [ -z "$SSH_ASKPASS" ]; then
	export SSH_ASKPASS="ssh-askpass"
fi

if [ "X$SSH_AUTH_SOCK" = "X" ]; then
	eval `ssh-agent -s`
	ssh-add $HOME/.ssh/id_rsa
fi

#AUTOSSH_POLL=600
#AUTOSSH_PORT=20000
#AUTOSSH_GATETIME=30
#AUTOSSH_LOGFILE=$HOST.log
#AUTOSSH_DEBUG=yes 
#AUTOSSH_PATH=/usr/local/bin/ssh
export AUTOSSH_POLL AUTOSSH_LOGFILE AUTOSSH_DEBUG AUTOSSH_PATH AUTOSSH_GATETIME AUTOSSH_PORT

autossh -2 -f -o BatchMode=yes -N -M 20000 -R $remoteport:localhost:22 "$remotehost"
info "Remote help is on!" "Connection tunnel from $remotehost into this machine is now active."