#!/bin/sh
# Re-apply active Muoto icon theme (cover-sync). No-op when activeIconPack is default.
set -eu

. /usr/share/harbour-muoto/service/muoto-dbus-wait.sh

muoto_log "update-icons: start uid=$(id -u)"

if muoto_os_update_running; then
    muoto_log "update-icons: skip (OS update in progress)"
    exit 0
fi

pack=$(muoto_dconf_as_user "dconf read /apps/harbour-muoto/activeIconPack" 2>/dev/null || true)
pack=${pack#\'}
pack=${pack%\'}

overlay=$(muoto_dconf_as_user "dconf read /apps/harbour-muoto/iconOverlay" 2>/dev/null || true)
case "$overlay" in
    true) o=true ;;
    *) o=false ;;
esac

muoto_log "update-icons: pack='$pack' overlay=$o"

if [ -z "$pack" ] || [ "$pack" = "default" ]; then
    muoto_log "update-icons: no-op (no active theme)"
    exit 0
fi

# activeIconPack holds the full rpm name; strip the prefix before prepending it
# so this matches IconPaths::packDir, which accepts either form.
packdir="/usr/share/harbour-themepack-${pack#harbour-themepack-}"
if [ ! -d "$packdir" ]; then
    muoto_log "update-icons: no-op (missing $packdir)"
    exit 0
fi

_apply_once() {
    _attempt="${1:-1}"
    if ! muoto_ensure_launcher_icond; then
        muoto_log "update-icons: attempt $_attempt failed (launcher-icond not on session bus)"
        return 1
    fi
    _seq_before=$(muoto_op_status_sequence)
    muoto_log "update-icons: attempt $_attempt ApplyIcons pack=$pack overlay=$o"
    muoto_wait_op_begin ApplyIcons 180
    if ! muoto_dbus_session_send \
        "$MUOTO_LAUNCHER_SERVICE" \
        "$MUOTO_LAUNCHER_PATH" \
        "$MUOTO_LAUNCHER_THEMES.ApplyIcons" \
        "string:$pack" "boolean:true" "boolean:$o"; then
        muoto_wait_op_cancel
        muoto_log "update-icons: attempt $_attempt dbus-send ApplyIcons rejected"
        return 1
    fi
    if ! muoto_wait_op_end ApplyIcons; then
        muoto_log "update-icons: attempt $_attempt wait lock failed"
        return 1
    fi

    # The lock released, but that alone never meant our request had run.
    _seq_after=$(muoto_op_status_sequence)
    if [ "$_seq_after" = "$_seq_before" ]; then
        muoto_log "update-icons: attempt $_attempt produced no result (daemon gone?)"
        return 1
    fi

    _outcome=$(muoto_op_status_outcome)
    case "$_outcome" in
        ok)
            muoto_log "update-icons: attempt $_attempt applied"
            return 0
            ;;
        partial)
            # Some entries could not be written. The pack is still applied, and
            # failing here would keep the repair oneshot retrying forever.
            muoto_log "update-icons: attempt $_attempt applied with skipped entries"
            return 0
            ;;
        *)
            muoto_log "update-icons: attempt $_attempt failed (outcome=$_outcome)"
            return 1
            ;;
    esac
}

if _apply_once 1; then
    muoto_log "update-icons: success"
    exit 0
fi

muoto_log "update-icons: retry in 3s"
sleep 3
if _apply_once 2; then
    muoto_log "update-icons: success (retry)"
    exit 0
fi

muoto_log "update-icons: failed"
exit 1
