#!/bin/bash

show_help() {
    cat << EOF
Usage: groovix-set [OPTIONS] LABEL=value SITE=value TYPE=value [TYPE=value ...]

Set configuration variables for Groovix system.

Arguments:
    LABEL=value     Set the label (single value)
    SITE=value      Set the site (single value)
    TYPE=value      Set type values (multiple allowed, space-separated in output)

Options:
    --help          Show this help message

Examples:
    groovix-set LABEL=example1 SITE=example2 TYPE=example3 TYPE=example4
    groovix-set SITE=production LABEL=server1 TYPE=web TYPE=api TYPE=database

Output:
    The script outputs three lines:
    - Line 1: LABEL value
    - Line 2: SITE value
    - Line 3: All TYPE values separated by spaces

EOF
}

# Check for help or no arguments
if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
    show_help
    exit 0
fi

# Initialize variables
LABEL=""
SITE=""
TYPES=""

# Process command line arguments
for arg in "$@"; do
    # Check if argument contains '='
    if [[ "$arg" == *"="* ]]; then
        # Split on first '=' to get key and value
        key="${arg%%=*}"
        value="${arg#*=}"
        
        case "$key" in
            "LABEL")
                LABEL="$value"
                ;;
            "SITE")
                SITE="$value"
                ;;
            "TYPE")
                # Add TYPE values to TYPES variable (space-separated)
                if [[ -z "$TYPES" ]]; then
                    TYPES="$value"
                else
                    TYPES="$TYPES $value"
                fi
                ;;
            *)
                echo "Warning: Unknown parameter '$key'" >&2
                ;;
        esac
    else
        echo "Warning: Invalid argument format '$arg' (expected KEY=VALUE)" >&2
    fi
done

# Output the variables
bashconfset GX_LABEL="$LABEL" /etc/groovix/groovix.conf
bashconfset GX_SITE="$SITE" /etc/groovix/groovix.conf
bashconfset GX_TYPES="$TYPES" /etc/groovix/groovix.conf

