#!/bin/sh

export PATH=$PATH:/usr/bin

if ! uci -q get icecast.general >/dev/null; then
    # Add the section without replacing old UCI settings needed by migration.
    printf "config general 'general'\n" | uci -m import icecast || exit 1
fi

if ! uci -q get icecast.admin >/dev/null; then
    printf "config section 'admin'\n" | uci -m import icecast || exit 1
fi

# Older general sections may exist without the new options.
for option in general.enabled general.configuration_file general.first_icecast_update general.certificate admin.password; do
    if ! uci -q get "icecast.$option" >/dev/null; then
        value=$(uci -c /barix/config/defaults -q get "icecast.$option") || exit 1
        uci set "icecast.$option=$value" || exit 1
    fi
done

# The beta password option is obsolete; released firmware uses admin.password.
if uci -q get icecast.general.admin_password >/dev/null; then
    uci -q delete icecast.general.admin_password || exit 1
fi

uci commit icecast || exit 1
