#!/bin/sh

# This file is covered by the GNU General Public License,
# which should be included with libshell as the file LICENSE.
# All copyright information are listed in the COPYING.

# Copyright (C) 2012 Andrew V. Stepanov <stanv@altlinux.org>

#
# RUN SSH CLIENT
#
# http://linux.die.net/man/1/rdesktop
#

# Use libshell
. shell-config
. shell-quote
. shell-var
export verbose=1

if [ -z "${__included_rd_ssh-}" ]; then
__included_rd_ssh=1

run_ssh() {
    
    local profile_file="$1" && shift
    local client="/usr/bin/ssh"

    if ! [ -x "$client" ]; then
        verbose "Can't find $client"
        return 1
    fi

    TERMCMD="/usr/bin/xterm"
    if ! [ -x "$TERMCMD" ]; then
        fatal "Can't find $TERMINAL."
    fi
    
    # Create sub-shell to save virgin environment
    (
    local OPTIONS=
    local msg=
    local ret=

    # Include profile
    . "$profile_file"
   
    # Check server supplied
    if [ -z "$SERVER" ]; then
        verbose "Bad profile: no SERVER specified"
        return 1
    fi
    
    # Run ssh using X terminal
    if shell_var_is_yes "$TERMINAL" && [ -z "$RD_SSH_TERMINAL" ]; then
        verbose "Run using terminal $TERMCMD."
        # Export flag variable to signify that terminal already executed
        export RD_SSH_TERMINAL="yes"
        exec "$TERMCMD" -e "$0" "$profile_file"
    fi
    
    # Reset positional parameters
    set --

    # Allow X forward
    set -- "$@" "-o" "ForwardX11 yes"

    # Sillent add new hosts with their public keys list
    set -- "$@" "-o" "StrictHostKeyChecking no"

    # Do not maintain know hosts list at all.
    set -- "$@" "-o" " UserKnownHostsFile /dev/null"
    
    # Check user name supplied
    if [ -n "$USER_NAME" ]; then
        verbose "Set user name to $USER_NAME"
        set -- "$@" "-o" "User $USER_NAME"
    fi
    
    # Specifies the port number to connect on the remote host.  The default is 22.
    if [ -n "$PORT" ]; then
        verbose "Set port to $PORT"
        set -- "$@" "-o" "Port $PORT"
    fi
    
    # Set remote server name
    verbose "Set server to $SERVER"
    set -- "$@" "$SERVER"
    
    # Gets password from stdin
    if [ -n "$COMMAND" ]; then
        verbose "Set command line to"
        set -- "$@" "$COMMAND"
    fi

    verbose "cmdline: $@"

    # Run client
    if [ -n "$PASSWORD" ]; then
        verbose "Use supplied password."
        SSHPASS="$PASSWORD" sshpass -e "$client" "$@"
        ret=$?
    else
        # Export SSH_ASKPASS
        if [ -f "/etc/profile.d/ssh-askpass.sh" ]; then
            . "/etc/profile.d/ssh-askpass.sh"
        fi
        verbose "Interactively ask password."
        "$client" "$@"
        ret=$?
    fi

    return $ret
    )

    return $?
}

fi #__included_rd_ssh

PROFILE_SSH=
PROFILES_PATH="/etc/alterator-rd"

#
# ENTER POINT
#

if [ "${#*}" -ne 1 ]; then
    echo "Use: $PROG <PROFILE>"
    exit 1
fi

for i in "$1" "$PROFILES_PATH/$1"; do
    if [ -r "$i" -a -f "$i" ]; then
        PROFILE_SSH="$i"
        verbose "Found profile: $PROFILE_SSH"
        break
    fi
done

if [ -z "$PROFILE_SSH" ]; then
    fatal "Can't find profile: $1"
fi

run_ssh "$PROFILE_SSH"
ret=$?

verbose "Client exited with $ret"
