#!/usr/bin/env bash

local_jitsi_config_dir() {
    local root_dir="$1"
    local local_dir="$root_dir/local"

    if [[ -n "${POLITICTALK_LOCAL_JITSI_CONFIG_DIR:-}" ]]; then
        printf '%s\n' "$POLITICTALK_LOCAL_JITSI_CONFIG_DIR"
        return
    fi

    if grep -qi microsoft /proc/version 2>/dev/null && [[ "$root_dir" == /mnt/* ]]; then
        printf '%s\n' "$HOME/politictalk-jitsi-cfg"
        return
    fi

    printf '%s\n' "$local_dir/.jitsi-meet-cfg"
}

local_jitsi_set_env() {
    local key="$1"
    local value="$2"
    local file="$3"

    if grep -qE "^#?${key}=" "$file"; then
        perl -0pi -e "s|^#?${key}=.*|${key}=${value}|m" "$file"
    else
        printf '%s=%s\n' "$key" "$value" >> "$file"
    fi
}
