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

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

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

run_rdp() {
    
    local profile_file="$1" && shift
    local client="/usr/bin/xfreerdp"

    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=

    # Reset positional parameters
    set --

    # Include profile
    . "$profile_file"

    if [ -z "$SERVER" ]; then
        verbose "Bad profile: SERVER"
        return 1
    fi
   
    # Sets the color depth for the connection.
    if [ -n "$COLOR_DEPTH" ]; then
        verbose "Set color depth to $COLOR_DEPTH"
        set -- "$@" "-a" "$COLOR_DEPTH"
    fi

    # Username for authentication on the server.
    if [ -n "$USER_NAME" ]; then
        verbose "Set user name to $USER_NAME"
        set -- "$@" "-u" "$USER_NAME"
    fi
   
    # The password to authenticate with. 
    if [ -n "$PASSWORD" ]; then
        verbose "Set password to ******"
        set -- "$@" "-p" "$PASSWORD"
    fi
   
    # Domain for authentication. 
    if [ -n "$DOMAIN" ]; then
        verbose "Set domain to $DOMAIN"
        set -- "$@" "-d" "$DOMAIN"
    fi
   
    # Startup shell for the session - starts a specific application instead of Explorer. 
    if [ -n "$RDP_SHELL" ]; then
        verbose "Set shell to $RDP_SHELL"
        set -- "$@" "-s" "$RDP_SHELL"
    fi
   
    # The initial working directory for the session. Often used in combination with -s to set up a fixed login environment. 
    if [ -n "$STARTUP_PATH" ]; then
        verbose "Set directory to $STARTUP_PATH"
        set -- "$@" "-c" "$STARTUP_PATH"
    fi
   
    # Desktop geometry.
    if [ -n "$GEOMETRY" ]; then
        if [ "$GEOMETRY" != "remote" ]; then
            verbose "Set geometry $STARTUP_PATH"
            set -- "$@" "-g" "$GEOMETRY"
        fi
    fi
   
    # Client hostname reported to the server. Normally xfreerdp automatically obtains the hostname of the client.
    if [ -n "$CLIENT_NAME" ]; then
        verbose "Set hostname to $CLIENT_NAME"
        set -- "$@" "-n" "$CLIENT_NAME"
    fi

    # Changes default bandwidth performance behaviour for RDP5.
    if [ -n "$QUALITY" ]; then
        verbose "Set performance flags to $QUALITY"
        [ "$QUALITY" = "bad" ] && flag="m"
        [ "$QUALITY" = "good" ] && flag="b"
        [ "$QUALITY" = "best" ] && flag="l"
        set -- "$@" "-x" "$flag"
    fi

    # Disable TLS encryption.
    if shell_var_is_yes "$DISABLE_TLS_ENCRYPTION"; then
        verbose "Disable TLS encryption"
        set -- "$@" "--no-tls"
    fi
    
    # Fullscreen
    if shell_var_is_yes "$FULLSCREEN"; then
        verbose "Enable fullscreen"
        set -- "$@" "-f"
    fi
   
    # Synchronize client and server clipboard data. Plain text, Unicode text, HTML content and images are supported.
    if shell_var_is_yes "$DISABLE_CLIPBOARD_SYNCHRONIZATION"; then
        verbose "Disable clipboard synchronization"
    else
        verbose "Allow clipboard synchronization"
        set -- "$@" "--plugin" "cliprdr"
    fi

    # Sound
    if [ "$SOUND" = "remote" ]; then
        verbose "Remote sound"
        # Play audio on the server side.
        set -- "$@" "-o"
    elif [ "$SOUND" = "local" ]; then
        # Redirects sound generated on the server to the client.
        verbose "Local sound"
        set -- "$@" "--plugin" "rdpsnd"
    elif [ "$SOUND" = "off" ]; then
        verbose "Disable sound"
    fi

    # Dynamic virtual channels, audio input redirection
    if shell_var_is_yes "$AUDIO_INPUT_REDIRECTION"; then
        verbose "Enable audio input redirection"
        # Some plugins support retrieving initial parameters.
        # Add --data argument after the <pluginname> in order to pass such parameters, and end it with --.
        set -- "$@" "--plugin" "drdynvc" "--data" "audin" "--"
    fi


    # RDPDR plugin
    if [ ${#SHARES[*]} -gt 0 ] || shell_var_is_yes "$PUBLISH_LOCAL_PRINTERS"; then
        # Begin plugin
        set -- "$@" "--plugin" "rdpdr"

        # Add shares
        for share in "${SHARES[@]}"; do
            local share_name="$(echo "$share" | cut -d ':' -f 1)"
            local path="$(echo "$share" | cut -d ':' -f 2)"
            if [ -n "$share_name" -a -n "$path" ];then
                set -- "$@" "--data" "disk:$share_name:$path"
            fi
        done
    
        # Add local printers
        # printer[:<printername>[:<driver>]] Redirect printers to the server.
        # If both <printername> and <driver> are omitted, the printer sub-plugin will automatically
        # redirect all CUPS printers using the default PostScript driver "MS Publisher Imagesetter". 
        if shell_var_is_yes "$PUBLISH_LOCAL_PRINTERS"; then
            verbose "Export to server local CUPS printers"
            set -- "$@" "--data" "printer"
        else
            verbose "Don't export CUPS printers"
        fi

        # End plugin
        set -- "$@" "--"
    fi

    # Attach to the admin console of the server. This is the default if no server license is available.
    if shell_var_is_yes "$ATTACH_TO_CONSOLE"; then
        verbose "Attach to the admin console of the server."
        set -- "$@" "-0"
    else
        verbose "Attach to the admin console of the server."
    fi
       
    # Set remote server name
    set -- "$@" "$SERVER"
    
    verbose "cmdline: $@"

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

    return $ret
    )

    return $?
}

fi #__included_rd_xfreerdp

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

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

run_rdp "$PROFILE_RDP"
ret=$?

verbose "Client exited with $ret"
