#!/bin/sh

po_domain="alterator-rd"
alterator_api_version=1

. alterator-sh-functions
. shell-config
. shell-quote

# Common settings
readonly LOG="/tmp/alterator-rd.log"
readonly RD_CONFIG_DIR="/etc/alterator-rd/"
readonly RD_USERS_CFG_DIR="$RD_CONFIG_DIR/users/"
readonly RD_GROUP="users"

# Module specific settings
readonly RD_PROTO="rdesktop"
readonly RDESKTOP_OPTS_SINGLE="server user_name password domain rdesktop_shell client_name startup_path vch_name vch_exec vch_params"
readonly RDESKTOP_OPTS_LISTS="geometry color_depth quality sound local_printer"
readonly RDESKTOP_OPTS_BOOL="attach_to_console publish_local_printers force_bitmaps disable_client_encryption disable_mouse_motion_events use_private_colourmap not_override_wm_key_bindings enable_numlock_sync enable_bitmaps_caching fullscreen"

rdesktop_list_quality() {
    write_enum_item "bad" "Bad"
    write_enum_item "good" "Good"
    write_enum_item "best" "Best"
}

rdesktop_list_sound() {
    write_enum_item "off" "Off"
    write_enum_item "local" "Local"
    write_enum_item "remote" "Remote"
}

rdesktop_list_local_printer() {
    write_enum_item "default"  "`_ "Use system's default printer"`"
    printers=($(lpstat -a | sed -n -e 's/[[:space:]]accepting.*$//p'))
    default_printer="$(lpstat -d | sed -n -e 's/^.\+:[[:space:]]//p')"
    for i in "${printers[@]}"; do
        if [ -z "$i" ]; then
            continue
        fi
        if [ $default_printer = $i ]; then
            suffix="`_ " (default printer)"`"
        else
            suffix=
        fi
        write_enum_item "$i" "${i}${suffix}"
    done
}

rdesktop_list_color_depth() {
    write_enum_item "8" "256 colors"
    write_enum_item "15" "High color 15 bit"
    write_enum_item "16" "High color 16 bit"
    write_enum_item "24" "True color 24 bit"
}

rdesktop_list_geometry() {
    write_enum_item "remote" "Use client geometry"
    write_enum_item "640x480" "640x480"
    write_enum_item "800x600" "800x600"
    write_enum_item "1024x768" "1024x768"
    write_enum_item "1152x864" "1280x720"
    write_enum_item "1280x800" "1280x800"
    write_enum_item "1280x1024" "1280x1024"
    write_enum_item "1360x768" "1360x768"
    write_enum_item "1366x768" "1366x768"
    write_enum_item "1440x900" "1440x900"
    write_enum_item "1600x1200" "1600x1200"
    write_enum_item "1680x1050" "1680x1050"
    write_enum_item "1920x1080" "1920x1080"
}

# Add new share for certain profile
rdesktop_share_add()
{
    # Profile name
    local profile_name="$1"; shift

    # New share
    local share="$1"; shift

    # Target profile file name
    local readonly profile_file="$RD_CONFIG_DIR/$profile_name"

    # Check profile name already exists
    if ! [ -w "$profile_file" ]; then
        write_error "Can't write profile. ($profile_name)"
        return 1
    fi

    # Create subshell & include config file
    (
    . "$profile_file"
    SHARES=("${SHARES[@]}" "$share")
    shell_config_set "$profile_file" 'SHARES' "($(for i in "${SHARES[@]}"; do echo -n "\"$(quote_shell "$i")\" "; done))"
    )
}

# Delete selected share for certain profile
rdesktop_share_del() {
    # Profile name
    local profile_name="$1"; shift

    # New share
    local share="$1"; shift

    # Target profile file name
    local readonly profile_file="$RD_CONFIG_DIR/$profile_name"

    # Check profile name already exists
    if ! [ -w "$profile_file" ]; then
        write_error "Can't write profile. ($profile_name)"
        return 1
    fi
    
    # Create subshell & include config file
    (
    . "$profile_file"
    SHARES[$share]=
    shell_config_set "$profile_file" 'SHARES' "($(for i in "${SHARES[@]}"; do test -z "$i" && continue; echo -n "\"$(quote_shell "$i")\" "; done))"
    )
}

# List known shares for certain profile
rdesktop_list_shares()
{
    # Profile name
    local profile_name="$1"; shift

    # Target profile file name
    local readonly profile_file="$RD_CONFIG_DIR/$profile_name"

    # Check profile name already exists
    if ! [ -e "$profile_file" ]; then
        write_error "Can't read profile. ($profile_name)"
        return 1
    fi

    # Create subshell & include config file
    (
    . "$profile_file"
    j=0
    for i in "${SHARES[@]}"; do
        write_enum_item "$((j++))" "$i"
    done
    )
}


# List avail profiles
list_profiles()
{
    local name=
    local protocol=
    find "$RD_CONFIG_DIR" -maxdepth 1 -type f |
    while read file; do
        name="$(basename "$file")"
        eval protocol="$(shell_config_get "$file" PROTOCOL)"
        if [ -n "$name" -a -n "$protocol" -a "$protocol" = "$RD_PROTO" ]; then
            write_enum_item "$name" "$name"
        fi
    done
}

# Main loop
on_message() {
    case "$in_action" in
        #######################################
        # RDESKTOP (same as freerdp, but older)
        #######################################
        rdesktop)
            local rdesktop_profile_name="${in_rdesktop_profile_name}"
            case "$in__objects" in
                list_quality)
                    rdesktop_list_quality
                    ;;
                list_sound)
                    rdesktop_list_sound
                    ;;
                list_color_depth)
                    rdesktop_list_color_depth
                    ;;
                list_local_printer)
                    rdesktop_list_local_printer
                    ;;
                list_geometry)
                    rdesktop_list_geometry
                    ;;
                list_shares)
                    if [ -n "$rdesktop_profile_name" ]; then
                        rdesktop_list_shares "$rdesktop_profile_name"
                    fi
                    ;;
                share_add)
                    if [ -n "$rdesktop_profile_name" -a -n "$in_rdesktop_share" ]; then
                        rdesktop_share_add "$rdesktop_profile_name" "$in_rdesktop_share"
                    fi
                    ;;
                share_del)
                    if [ -n "$rdesktop_profile_name" -a -n "$in_rdesktop_share" ]; then
                        rdesktop_share_del "$rdesktop_profile_name" "$in_rdesktop_share"
                    fi
                    ;;
                read)
                    # Check that profile name supplied
                    if [ -z "$in_rdesktop_profile_name" ]; then
                        write_error "Can't read profile settings."
                        return 1
                    fi
                    local profile_file="$RD_CONFIG_DIR/$in_rdesktop_profile_name"

                    # Check profile name already exists
                    if ! [ -f "$profile_file" ]; then
                        write_error "Profile doesn't exists. ($in_rdesktop_profile_name)"
                        return 1
                    fi

                    write_string_param "profile_type" "rdesktop"
                    write_string_param "rdesktop_profile_name" "$in_rdesktop_profile_name"

                    # Read options
                    for option in $RDESKTOP_OPTS_SINGLE $RDESKTOP_OPTS_LISTS; do
                        option_upper="$(echo "$option" | tr '[:lower:]' '[:upper:]')"
                        eval value="$(shell_config_get "$profile_file" "$option_upper")"
                        if [ -n "$value" ]; then
                            write_string_param "rdesktop_$option" "$value"
                        fi
                    done
                   
                    # Read bool options
                    for option in $RDESKTOP_OPTS_BOOL; do
                        local optionU="$(echo "$option" | tr '[:lower:]' '[:upper:]')"
                        eval value="$(shell_config_get "$profile_file" "$optionU")"
                        if [ "$value" = "yes" ]; then
                            write_bool_param "rdesktop_$option" 1
                        elif [ "$value" = "no" ]; then
                            write_bool_param "rdesktop_$option" 0
                        fi
                    done
                    ;;
                write)
                    # Check that profile name supplied
                    if [ -z "$in_avail_profiles" ]; then
                        write_error "Can't update profile settings."
                        return 1
                    fi
                    local profile_file="$RD_CONFIG_DIR/$in_avail_profiles"

                    # Check profile name already exists
                    if ! [ -f "$profile_file" ]; then
                        write_error "Profile doesn't exists. ($in_avail_profiles)"
                        return 1
                    fi
                    
                    shell_config_set "$profile_file" "PROTOCOL" "\"$RD_PROTO\""

                    # Fill parameters
                    for i in $RDESKTOP_OPTS_SINGLE $RDESKTOP_OPTS_LISTS; do 
                        eval "value=\$in_rdesktop_$i"
                        option="$(echo "$i" | tr '[:lower:]' '[:upper:]')"
                        if [ -n "$value" ]; then
                            shell_config_set "$profile_file" "$option" "\"$(quote_shell "$value")\""
                        else
                            shell_config_set "$profile_file" "$option" ''
                        fi
                    done
                    
                    # Fill bool parameters
                    for i in $RDESKTOP_OPTS_BOOL; do 
                        eval "value=\$in_rdesktop_$i"
                        option="$(echo "$i" | tr '[:lower:]' '[:upper:]')"
                        if [ "$value" = "#t" ]; then
                            shell_config_set "$profile_file" "$option" "\"$(quote_shell "yes")\""
                        else
                            shell_config_set "$profile_file" "$option" "\"$(quote_shell "no")\""
                        fi
                    done
                    ;;
            esac
            ;;
        list)
            case "${in__objects##*/}" in
                avail_profiles) list_profiles;;
                *) :;;
            esac
            ;;
    esac
}

message_loop
