#!/bin/bash

get_uci_config() {
    local key="$1"
    local value
    value=$(uci get "$key" 2>/dev/null)
    if [ $? -ne 0 ] || [ -z "$value" ]; then
        echo "Error getting UCI config for '$key'" >&2
        return 1
    fi
    echo "$value"
}

try_get_uci_config() {
    local key="$1"
    local value
    value=$(uci get "$key" 2>/dev/null)
    if [ $? -ne 0 ] || [ -z "$value" ]; then
        return 1
    fi
    echo "$value"
}

system_version=$(dpkg -s rosp-core | grep "^Version:" | awk '{print $2}' | awk -F'.' '{print $1"."$2}')

rosp2_distribution="rosp2"
bugfix_distribution="rosp2-bugfix-updates/v${system_version}"

DEBIAN_REPO_CONFIG="/etc/apt/sources.list"
ROSP_REPO_CONFIG="/etc/apt/sources.list.d/rosp.list"
APP_REPO_CONFIG="/etc/apt/sources.list.d/rosp_app.list"

DEBIAN_REPO_SERVER=$(get_uci_config "software_update.debian_repo_server")
ROSP_REPO_SERVER=$(get_uci_config "software_update.rosp_repo_server")
APP_REPO_SERVER=$(get_uci_config "software_update.app_repo_server")

DEBIAN_REPO_GLOBAL_SERVER="http://deb.debian.org/debian"
DEBIAN_REPO_GLOBAL_SECURITY_SERVER="http://security.debian.org/debian-security"
DEBIAN_REPO_CHINA_SERVER="http://mirrors.tuna.tsinghua.edu.cn/debian"
DEBIAN_REPO_CHINA_SECURITY_SERVER="http://mirrors.tuna.tsinghua.edu.cn/debian-security"

ROSP_REPO_GLOBAL_SERVER="https://debian.robustel.net/rosp"
ROSP_REPO_CHINA_SERVER="https://debian.robustel.cn/rosp"

APP_REPO_GLOBAL_SERVER="https://debian.robustel.net/rosp"
APP_REPO_CHINA_SERVER="https://debian.robustel.cn/rosp"

ROSP_REPO_BUGFIX_CONFIG=""
APP_REPO_BUGFIX_CONFIG=""

if [ $ROSP_REPO_SERVER == "global" ]; then
    echo "deb $ROSP_REPO_GLOBAL_SERVER $rosp2_distribution main" > "$ROSP_REPO_CONFIG"
    echo "deb $ROSP_REPO_GLOBAL_SERVER $bugfix_distribution main" >> "$ROSP_REPO_CONFIG"
    ROSP_REPO_BUGFIX_CONFIG="deb $ROSP_REPO_GLOBAL_SERVER $bugfix_distribution main"
elif [ $ROSP_REPO_SERVER == "china" ]; then
    echo "deb $ROSP_REPO_CHINA_SERVER $rosp2_distribution main" > "$ROSP_REPO_CONFIG"
    echo "deb $ROSP_REPO_CHINA_SERVER $bugfix_distribution main" >> "$ROSP_REPO_CONFIG"
    ROSP_REPO_BUGFIX_CONFIG="deb $ROSP_REPO_CHINA_SERVER $bugfix_distribution main"
elif [ $ROSP_REPO_SERVER == "custom" ]; then
    echo "# User Custom ROS Pro repo server" > "$ROSP_REPO_CONFIG"
    x=1
    while true; do
        url=$(try_get_uci_config "software_update.rosp_custom.repo_config[$x]")
        if [ -z "$url" ]; then
            break
        fi
        echo "$url" >> "$ROSP_REPO_CONFIG"

        if [[ "$url" == *"bugfix"* ]]; then
            ROSP_REPO_BUGFIX_CONFIG="$url"
        fi
        x=$((x+1))
    done
fi

if [ $DEBIAN_REPO_SERVER == "global" ]; then
    echo "deb $DEBIAN_REPO_GLOBAL_SERVER bullseye main contrib non-free" > "$DEBIAN_REPO_CONFIG"
    echo "deb $DEBIAN_REPO_GLOBAL_SERVER bullseye-updates main contrib non-free" >> "$DEBIAN_REPO_CONFIG"
    echo "deb $DEBIAN_REPO_GLOBAL_SECURITY_SERVER bullseye-security main contrib non-free" >> "$DEBIAN_REPO_CONFIG"

elif [ $DEBIAN_REPO_SERVER == "china" ]; then
    echo "deb $DEBIAN_REPO_CHINA_SERVER bullseye main contrib non-free" > "$DEBIAN_REPO_CONFIG"
    echo "deb $DEBIAN_REPO_CHINA_SERVER bullseye-updates main contrib non-free" >> "$DEBIAN_REPO_CONFIG"
    echo "deb $DEBIAN_REPO_CHINA_SECURITY_SERVER bullseye-security main contrib non-free" >> "$DEBIAN_REPO_CONFIG"
elif [ $DEBIAN_REPO_SERVER == "custom" ]; then
    echo "# User Custom Debian repo server" > "$DEBIAN_REPO_CONFIG"
    x=1
    while true; do
        url=$(try_get_uci_config "software_update.debian_custom.repo_config[$x]")
        if [ -z "$url" ]; then
            break
        fi
        echo "$url" >> "$DEBIAN_REPO_CONFIG"
        x=$((x+1))
    done

    url=$(try_get_uci_config "software_update.debian_security_config")
    if [ -n "$url" ]; then
        echo "$url" >> "$DEBIAN_REPO_CONFIG"
    fi
fi

if [ $APP_REPO_SERVER == "global" ]; then
    echo "deb $APP_REPO_GLOBAL_SERVER $rosp2_distribution app" > "$APP_REPO_CONFIG"
    echo "deb $APP_REPO_GLOBAL_SERVER $bugfix_distribution app" >> "$APP_REPO_CONFIG"
    APP_REPO_BUGFIX_CONFIG="deb $APP_REPO_GLOBAL_SERVER $bugfix_distribution app"
elif [ $APP_REPO_SERVER == "china" ]; then
    echo "deb $APP_REPO_CHINA_SERVER $rosp2_distribution app" > "$APP_REPO_CONFIG"
    echo "deb $APP_REPO_CHINA_SERVER $bugfix_distribution app" >> "$APP_REPO_CONFIG"
    APP_REPO_BUGFIX_CONFIG="deb $APP_REPO_CHINA_SERVER $bugfix_distribution app"
elif [ $APP_REPO_SERVER == "custom" ]; then
    echo "# User Custom App center repo server" > "$APP_REPO_CONFIG"
    x=1
    while true; do
        url=$(try_get_uci_config "software_update.app_custom.repo_config[$x]")
        if [ -z "$url" ]; then
            break
        fi
        echo "$url" >> "$APP_REPO_CONFIG"

        if [[ "$url" == *"bugfix"* ]]; then
            APP_REPO_BUGFIX_CONFIG="$url"
        fi
        x=$((x+1))
    done
fi

renv set rosp_bugfix_repo_config "$ROSP_REPO_BUGFIX_CONFIG"
renv set app_bugfix_repo_config "$APP_REPO_BUGFIX_CONFIG"
