PATH=/bin:/usr/bin:$PATH ; export PATH

##
## This script assumes 'id' and 'host' variables are already setup.
## It uses them to generate $prefix, $prefix, $sshcmd, $sshicmd.
##

spooldir=/tmp # spooldir must be secure!

## File name prefix
prefix="$spooldir/$id"
rprefix="$prefix"

## Directory where the scripts are located
bindir=`$dirname "$0"`
cygpath=`$which cygpath 2> /dev/null`
[ -x "$cygpath" ]  && bindir=`cygpath "$bindir"`

## Path to .magictree/
mtdir=`dirname "$bindir"`

## Path to the directory where we keep MT-specific SSH key
dotsshdir="$mtdir/.ssh"

## SSH commands. NB: $sshcmd will be eval'ed!
unset SSH_AUTH_SOCK
sshcmd='ssh -T -o IdentitiesOnly=yes -i "$dotsshdir/id_rsa" -o "UserKnownHostsFile=$dotsshdir/known_hosts" $host -o BatchMode=yes'
sshicmd="ssh -t -o IdentitiesOnly=yes -i '$dotsshdir/id_rsa' -o 'UserKnownHostsFile=$dotsshdir/known_hosts' $host -o BatchMode=yes"

## Bugfix for #241. We will not use this directory for anything.
mkdir -p "$HOME/.ssh"

# plink does not work well
#sshcmd="plink -agent -T -batch $host"
# Session multiplexing, does not work on Cygwin
#sshcmd="ssh -T -o ControlPath=$HOME/.ssh/$host..master $host"
# Use existing authsocket
#authsock="$HOME/.ssh/authsock"
#sshcmd="env SSH_AUTH_SOCK=$authsock ssh -T $host"
#sshicmd="env SSH_AUTH_SOCK=$authsock ssh -t $host"

if uname | grep -q ^CYGWIN ; then
	term () {
		nohup rxvt -e $SHELL -c "$1" > /dev/null 2>&1 &
	}

	## generate /etc/passwd file for cygwin
	[ -f /etc/passwd ] || mkpasswd -l > /etc/passwd
elif uname | grep -q ^Darwin ; then
	term () {
		osascript -e "tell application \"Terminal\" to do script \"exec $1\"" ; osascript -e "tell application \"Terminal\" to activate"
	}
else
	term () {
		nohup xterm -e "$1" > /dev/null 2>&1 &
	}
fi
