#!/bin/tcsh -f
# JLdL 10Dec11.
#
# Copyright (C) 2005-2011 by Jorge L. deLyra <delyra@fma.if.usp.br>.
# This program may be copied and/or distributed freely. See the
# _ terms and conditions in /usr/share/doc/<package>/copyright.
#
# This program effects modified remote ssh logins between two
# _ hosts in a cluster, within which all hosts share the same
# _ user and authorization environments; it is supposed to be
# _ used by means of symbolic links pointing to this file; it
# _ is not supposed to be executed directly, and therefore it
# _ should be installed outside the usual executable paths.
#
# Get the name of the target host from the name with which this
# _ program was called, which is the name of the symbolic link.
set thehost = `basename $0`
#
# If the environment variable RCLSH is set and the local
# _ current working directory is not the home directory,
# _ then try to go to the current working directory in
# _ the remote host, instead of the home directory.
if ( "$?RCLSH" && "$PWD" != "$HOME" ) then
    #
    # Get the type of shell from the environment.
    set theshell = `basename $SHELL`
    #
    # If the shell is tcsh, include some error handling.
    if ( $theshell == tcsh ) then
	#
	# Call ssh in the appropriate way, passing to it
	# _ all submitted command-line arguments.
	/usr/bin/ssh $* -t $thehost \
	    "if ( -d $PWD ) cd $PWD ; $SHELL"
    #
    # If the shell is bash, include some error handling.
    else if ( $theshell == bash ) then
	#
	# Call ssh in the appropriate way, passing to it
	# _ all submitted command-line arguments.
	/usr/bin/ssh $* -t $thehost \
	    "if [ -d $PWD ] ; then cd $PWD ; fi ; $SHELL"
    #
    # Otherwise, just go ahead and hope for the best.
    else
	#
	# Call ssh in the appropriate way, passing to it
	# _ all submitted command-line arguments.
	/usr/bin/ssh $* -t $thehost "cd $PWD ; $SHELL"
    endif
#
# Otherwise, just log into the remote host in the usual way,
# _ passing to ssh all submitted command-line arguments.
else
    /usr/bin/ssh $* $thehost
endif
