#!/bin/sh

po_domain="alterator-rd"
alterator_api_version=1

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


# 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"
readonly RD_USER_UNDEF_PROFILE="undef"

# Print profile type
read_profile_type() {
    local profile_name="$1" && shift
    local profile_file="$RD_CONFIG_DIR/$profile_name"
    local value=

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

    eval value="$(shell_config_get "$profile_file" "PROTOCOL")"

    if [ -n "$value" ]; then
        write_string_param "profile_name" "$profile_name"
        write_string_param "profile_type" "$value"
    fi
}

# Print profile options for certain user
read_profile_options() {
    local user_name="$1" && shift
    local profile_name="$1" && shift
    
    # User config
    local readonly config="$RD_USERS_CFG_DIR/$user_name"
    if ! [ -f "$config" -a -r "$config" ]; then
        # No any options
        return
    fi

    # Autostart option
    eval autostart=$(shell_config_get "$config" RD_AUTOSTART)
    for i in "${autostart[@]}"; do
        if [ "$i" = "$profile_name" ]; then
            write_bool_param "check_profile_autostart" 1
        fi
    done
}

# Enable RD profile for user
enable_profile() {
    local user_name="$1" && shift
    local profile_name="$1" && shift
    local profiles=()

    local readonly config="$RD_USERS_CFG_DIR/$user_name"
    
    if [ -f "$config" -a -w "$config" ]; then
        eval profiles=$(shell_config_get "$config" RD_PROFILES)
    fi

    if ! [ -f "$config" ]; then
	    mkdir -p "$RD_USERS_CFG_DIR"
	    touch "$config"
    fi

    profiles=("${profiles[@]}" "$profile_name")
    
    shell_config_set "$config" 'RD_PROFILES' "($(for i in "${profiles[@]}"; do test -z "$i" && continue; echo -n "\"$(quote_shell "$i")\" "; done))"
}

# Disable RD profile for user
disable_profile() {
    local user_name="$1" && shift
    local profile_name="$1" && shift
    local profiles=()
    local new_profiles=()

    local readonly config="$RD_USERS_CFG_DIR/$user_name"
    
    if [ -f "$config" -a -w "$config" ]; then
        eval profiles=$(shell_config_get "$config" RD_PROFILES)
    fi
    
    if ! [ -f "$config" ]; then
	    mkdir -p "$RD_USERS_CFG_DIR"
	    touch "$config"
    fi

    for i in "${profiles[@]}"; do
        if [ "$i" != "$profile_name" ]; then
            new_profiles=("${new_profiles[@]}" "$i")
        fi
    done
    
    shell_config_set "$config" 'RD_PROFILES' "($(for i in "${new_profiles[@]}"; do test -z "$i" && continue; echo -n "\"$(quote_shell "$i")\" "; done))"
}

# List disabled profiles for certain user
list_profiles_disabled()
{
    local user_name="$1" 
    local name=
    local protocol=
    local config=
    local all_disabled="yes"
    local profiles=()
    
    # User config
    local readonly config="$RD_USERS_CFG_DIR/$user_name"
    if [ -f "$config" -a -r "$config" ]; then
        eval profiles=$(shell_config_get "$config" RD_PROFILES)
        export profiles
    fi

    find "$RD_CONFIG_DIR" -maxdepth 1 -type f |
    while read file; do
        local enabled="no"

        name="$(basename "$file")"

        eval protocol="$(shell_config_get "$file" PROTOCOL)"
        if ! [ -n "$name" -a -n "$protocol" ]; then
            continue
        fi

        for i in "${profiles[@]}"; do
            if [ "$i" = "$name" ]; then
                enabled="yes"
            fi
        done

        if ! shell_var_is_yes "$enabled"; then
            write_enum_item "$name" "$name ($protocol)"
        fi
    done
}

# List enabled profiles for user
list_profiles_enabled()
{
    local user_name="$1" && shift
    local profile_file=
    local protocol=
    local config=
    local profiles=

    # User config
    local readonly config="$RD_USERS_CFG_DIR/$user_name"
    if ! [ -f "$config" -a -r "$config" ]; then
        return
    fi

    eval profiles=$(shell_config_get "$config" RD_PROFILES)
    
    for i in "${profiles[@]}"; do
        profile_file="$RD_CONFIG_DIR/$i"

        if ! [ -f "$profile_file" -a -r "$profile_file" ]; then
            continue
        fi

        eval protocol="$(shell_config_get "$profile_file" PROTOCOL)"

        if [ -z "$protocol" ]; then
            continue
        fi

        write_enum_item "$i" "$i ($protocol)"
    done
}

# List users
list_users()
{
    for user in $(getent group $RD_GROUP | cut -d : -f 4 | tr ',' ' '); do
            write_enum_item "$user" "$user"
    done
}

# Check profile name for correctness
profile_name_is_good() {
    return 0
}

# Create new profile
new_profile_create() {
    local name="$1"; shift
    local protocol="$1"; shift

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

    # Check profile name is good
    if ! profile_name_is_good "$name"; then
        write_error "Bad name. ($name)"
        return 1
    fi

    # Check profile name already exists
    if [ -f "$profile" ]; then
        write_error "Already exist. ($name)"
        return 1
    fi

    # Create directory with profiles
    mkdir -p "$RD_CONFIG_DIR"

    echo "# Do not edit, all manual editings will be lost." > "$profile"
    # Create new profile
    shell_config_set "$profile" "PROTOCOL" "\"$(quote_shell "$protocol")\""
}

# Delete existing profile
delete_profile() {
    readonly local name="$1"; shift
    
    # Target profile file name
    local readonly profile="$RD_CONFIG_DIR/$name"

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

    # Remove profile
    if ! rm -f "$profile"; then
        write_error "Can't remove profile. ($name)"
        return 1
    fi
}

# Main loop
on_message() {
    case "$in_action" in
        users)
            case "$in__objects" in
                list_users)
                    list_users
                    ;;

			    list_profiles_disabled)
                    if [ -z "$in_user" ]; then
                        write_error "Unknown user name."
                        return 1
                    fi

                    list_profiles_disabled "$in_user"
                    ;;

			    list_profiles_enabled)
                    if [ -z "$in_user" ]; then
                        write_error "Unknown user name."
                        return 1
                    fi
                    
                    list_profiles_enabled "$in_user"
                    ;;

                read_profile_options)
                    if [ -z "$in_user" ]; then
                        write_error "Unknown user name."
                        return 1
                    fi
                    
                    if [ -z "$in_profile" ]; then
                        write_error "Unknown profine name."
                        return 1
                    fi

                    read_profile_options "$in_user" "$in_profile"
                    ;;

                write_profile_options)
                    if [ -z "$in_user" ]; then
                        write_error "Unknown user name."
                        return 1
                    fi
                    
                    if [ -z "$in_profile" ]; then
                        write_error "Unknown profine name."
                        return 1
                    fi

                    # User config
                    local config="$RD_USERS_CFG_DIR/$in_user"
   
                    # Create config
                    if ! [ -f "$config" ]; then
                        mkdir -p "$RD_USERS_CFG_DIR"
                        touch "$config"
                    fi

                    # Autostart option
                    local autostart=()
                    local autostart_new=()
                    eval autostart=$(shell_config_get "$config" RD_AUTOSTART)
                    if [ -n "$in_autostart" ]; then
                        # Remove all profile mention
                        for i in "${autostart[@]}"; do
                            if [ "$i" != "$in_profile" ]; then
                                autostart_new=("${autostart_new[@]}" "$i")
                            fi
                        done

                        # Turn on autostart if specified
                        if [ "$in_autostart" = "#t" ]; then
                            autostart_new=("${autostart_new[@]}" "$in_profile")
                        fi
    
                        shell_config_set "$config" 'RD_AUTOSTART' "($(for i in "${autostart_new[@]}"; do test -z "$i" && continue; echo -n "\"$(quote_shell "$i")\" "; done))"
                    fi
    

                    ;;

                enable_profile)
                    if [ -z "$in_user" ]; then
                        write_error "Unknown user name."
                        return 1
                    fi
                    
                    if [ -z "$in_profile" ]; then
                        write_error "Unknown profine name."
                        return 1
                    fi

                    enable_profile "$in_user" "$in_profile"
                    ;;

                disable_profile)
                    if [ -z "$in_user" ]; then
                        write_error "Unknown user name."
                        return 1
                    fi
                    
                    if [ -z "$in_profile" ]; then
                        write_error "Unknown profine name."
                        return 1
                    fi

                    disable_profile "$in_user" "$in_profile"
                    ;;

            esac
            ;;
        new)
            case "$in__objects" in
                /)
                    if [ -n "$in_name" -a -n "$in_protocol" ]; then
                        msg="$(new_profile_create "$in_name" "$in_protocol")"
                        ret=$?
                        if [ $ret -ne 0 ]; then
                            [ -n "$msg" ] || msg="new_profile_create failed"
                            write_error "$msg"
                        fi
                    fi
                    ;;
            esac
            ;;
        read)
            case "$in__objects" in
                /)
                    ;;
                type)
                    if [ -n "$in_profile_name" ]; then
                        read_profile_type "$in_profile_name"
                    fi
                    ;;
            esac
            ;;
        delete)
            if [ -n "$in_profile_name" ]; then
                        msg="$(delete_profile "$in_profile_name")"
                        ret=$?
                        if [ $ret -ne 0 ]; then
                            [ -n "$msg" ] || msg="delete_profile failed"
                            write_error "$msg"
                        fi
            fi
            ;;
    esac
}

message_loop
