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

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

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

run_tigervnc() {
    
    local profile_file="$1" && shift
    local client="/usr/bin/vncviewer"

    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"
   
    # Check server supplied
    if [ -z "$SERVER" ]; then
        verbose "Bad profile: no SERVER specified"
        return 1
    fi

    # Allowing to share the desktop with someone already using it.
    if shell_var_is_yes "$SHARED"; then
        verbose "Set shared mode."
        set -- "$@" "-Shared"
    fi
    
    # Specifies that no keyboard or mouse events should be sent to the server.
    if shell_var_is_yes "$VIEWONLY"; then
        verbose "Set view only mode."
        set -- "$@" "-ViewOnly"
    fi
    
    # Start in full-screen mode.
    if shell_var_is_yes "$FULLSCREEN"; then
        verbose "Set fullscreen mode."
        set -- "$@" "-FullScreen"
    fi
    
    # Send clipboard changes to the server.
    if shell_var_is_yes "$SENDCLIPBOARD"; then
        verbose "Send clipboard changes to the server."
        set -- "$@" "-SendClipboard"
    fi
    
    # Accept clipboard changes from the server.
    if shell_var_is_yes "$ACCEPTCLIPBOARD"; then
        verbose "Accept clipboard changes from the server."
        set -- "$@" "-AcceptClipboard"
    fi
    
    # Render the mouse cursor locally. 
    if shell_var_is_yes "$USELOCALCURSOR"; then
        verbose "Render the mouse cursor locally."
        set -- "$@" "-UseLocalCursor"
    fi
    
    # Send the primary selection and cut buffer to the server as well as the clipboard selection
    if shell_var_is_yes "$SENDPRIMARY"; then
        verbose "Send the primary selection and cut buffer to the server as well as the clipboard selection."
        set -- "$@" "-SendPrimary"
    fi

    # Reconfigure desktop size on the server on connect (if possible)
    if [ -n "$DESKTOPSIZE" -a "$DESKTOPSIZE" != "remote" ]; then
        verbose "Reconfigure desktop size on the server on connect to $DESKTOPSIZE"
        set -- "$@" "--DesktopSize=$DESKTOPSIZE"
    fi
    
    # Color 
    if [ "$COLOR_LEVEL" = "all" ]; then
        verbose "Use full color"
        set -- "$@" "-FullColour"
    else
        verbose "Do not Use full color."
        set -- "$@" "-FullColour=0"

        if [ "$COLOR_LEVEL" = "2" ]; then
            verbose "Medium (256 colors)"
            set -- "$@" "-LowColorLevel=2"
        elif [ "$COLOR_LEVEL" = "1" ]; then
            verbose "Low (64 colors)"
            set -- "$@" "-LowColorLevel=1"
        elif [ "$COLOR_LEVEL" = "0" ]; then
            verbose "Very Low (8 colors)"
            set -- "$@" "-LowColorLevel=0"
        elif [ -n "$COLOR_LEVEL" ]; then
            verbose "Bad parameter COLOR_LEVEL == $COLOR_LEVEL"
        fi
    fi

    # Compression level
    if [ -n "$COMPRESSLEVEL" ]; then
        verbose "Set compression level to $COMPRESSLEVEL"
        set -- "$@" "-CompressLevel=$COMPRESSLEVEL"
    fi
    
    # JPEG quality level
    if [ -n "$QUALITYLEVEL" ]; then
        verbose "JPEG quality level to $QUALITYLEVEL"
        set -- "$@" "-QualityLevel=$QUALITYLEVEL"
    fi

    # Gets password from stdin
    if [ -n "$PASSWORD" ]; then
        set -- "$@" "-passwdInput"
    fi
      
    # Set remote server name
    local server="${SERVER}${DISPLAYNUM:+:$DISPLAYNUM}"
    verbose "Set server to $server"
    set -- "$@" "$server"

    verbose "cmdline: $@"

    # Run client
    if [ -n "$PASSWORD" ]; then
        verbose "Use password."
        echo "$PASSWORD" | "$client" "$@"
        ret=$?
    else
        verbose "Do not use password."
        "$client" "$@"
        ret=$?
    fi

    return $ret
    )

    return $?
}

fi #__included_rd_tigervnc

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

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

run_tigervnc "$PROFILE_TIGERVNC"
ret=$?

verbose "Client exited with $ret"
