#!/bin/sh

po_domain="alterator-rd"
alterator_api_version=1

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

# Include modules

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

# Module specific
readonly SSH_OPTS_SINGLE="server port user_name password command"
readonly SSH_OPTS_LISTS=""
readonly SSH_OPTS_BOOL="terminal"

# 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
        #########
        # ssh
        #########
        ssh)
            case "$in__objects" in
                read)
                    # Check that profile name supplied
                    if [ -z "$in_ssh_profile_name" ]; then
                        write_error "Can't read profile settings."
                        return 1
                    fi
                    local profile_file="$RD_CONFIG_DIR/$in_ssh_profile_name"

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

                    write_string_param "profile_type" "$RD_PROTO"
                    write_string_param "ssh_profile_name" "$in_ssh_profile_name"

                    # Read options
                    for option in $SSH_OPTS_SINGLE $SSH_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 "ssh_$option" "$value"
                        fi
                    done
                   
                    # Read bool options
                    for option in $SSH_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 "ssh_$option" 1
                        elif [ "$value" = "no" ]; then
                            write_bool_param "ssh_$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 $SSH_OPTS_SINGLE $SSH_OPTS_LISTS;do 
                        eval "value=\$in_ssh_$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 $SSH_OPTS_BOOL;do 
                        eval "value=\$in_ssh_$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
