#!/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 XDMCP CLIENT
#
# http://linux.die.net/man/1/rdesktop
#

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

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

run_xdmcp() {
    
    local profile_file="$1" && shift
    local client="/usr/bin/Xnest"

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

    # Create sub-shell to save virgin environment
    (
    local OPTIONS=
    local msg=
    local ret=

    # Include profile
    . "$profile_file"
  
    # Check connection method
    if [ -z "$CONNECTION_METHOD" ]; then
        verbose "Bad profile: no CONNECTION_METHOD specified"
        return 1
    fi

    # Reset positional parameters
    set --

    case "$CONNECTION_METHOD" in 
        direct)
            # Check server supplied
            if [ -z "$SERVER" ]; then
                verbose "Bad profile: no SERVER specified"
                return 1
            fi

            # Set sessionhost
            verbose "Set sessionhost to $SERVER"
            set -- "$@" "-query" "$SERVER"
            ;;
        broadcast)
            # Connection method == broadcast
            verbose "Set connection method to broadcast"
            set -- "$@" "-broadcast"
            ;;
        browse)
            # Check server supplied
            if [ -z "$SERVER" ]; then
                verbose "Bad profile: no SERVER specified"
                return 1
            fi

            # Set xdmcpserver
            verbose "Set xdmcpserver to $SERVER"
            set -- "$@" "-indirect" "$SERVER"
            ;;
        *)
        verbose "Bad profile: unknown CONNECTION_METHOD: $CONNECTION_METHOD"
        return 1
    esac

    # Check user name supplied
    if [ -n "$COLOR_DEPTH" ]; then
        if [ "$COLOR_DEPTH" = "default" ]; then
            verbose "Use default color depth"
        else
            verbose "Set color depth to $COLOR_DEPTH"
            set -- "$@" "-depth" "$COLOR_DEPTH"
        fi
    fi
    
    # Specifies the port number to connect on the remote host.  The default is 22.
    if [ -n "$GEOMETRY" ]; then
        if [ "$GEOMETRY" = "remote" ]; then
            verbose "Geometry is 3/4 of current screen resolution"
        else
            verbose "Set geometry to $GEOMETRY"
            set -- "$@" "-geometry" "$GEOMETRY"
        fi
    fi
    
    # Lookup for next free display number
    local display="$(who -u | sed -n -e 's/^.*(:\([[:digit:]]\+\)\.[[:digit:]]\+)$/\1/p' | sort -n | head -n 1)"
    verbose "Current max display is $display"
    display=$((display+1))
            
    set -- "$@" ":$display"

    verbose "cmdline: $@"

    # Run client
    "$client" "$@"
    ret=$?

    return $ret
    )

    return $?
}

fi #__included_rd_xdmcp

PROFILE_XDMCP=
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_XDMCP="$i"
        verbose "Found profile: $PROFILE_XDMCP"
        break
    fi
done

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

run_xdmcp "$PROFILE_XDMCP"
ret=$?

verbose "Client exited with $ret"
