#!/bin/sh
# meanwhile kurulumu: eklentiyi kurar (ya da günceller) ve otomatik güncellemeyi açar.
#   curl -fsSL https://meanwhile.oktaydak.com/kur.sh | sh
# Aynı komut güncelleme için de çalışır.
set -e

if ! command -v claude >/dev/null 2>&1; then
  echo "Install Claude Code first: https://claude.com/claude-code" >&2
  exit 1
fi

echo "› adding the meanwhile marketplace…"
if claude plugin marketplace list 2>/dev/null | grep -q 'meanwhile'; then
  claude plugin marketplace update meanwhile >/dev/null
else
  claude plugin marketplace add radioheavy/meanwhile >/dev/null
fi

echo "› installing the plugin…"
if claude plugin list 2>/dev/null | grep -q 'meanwhile@meanwhile'; then
  claude plugin update meanwhile@meanwhile >/dev/null
else
  claude plugin install meanwhile@meanwhile >/dev/null
fi

# Otomatik güncelleme: /plugin ekranındaki "Enable auto-update" ile aynı ayar.
DOSYA="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/plugins/known_marketplaces.json"
if [ -f "$DOSYA" ] && command -v perl >/dev/null 2>&1 && perl -MJSON::PP -e1 2>/dev/null; then
  perl -MJSON::PP -e '
    my $f = shift; local $/; open my $i, "<", $f or exit 1; my $d = decode_json(<$i>); close $i;
    exit 1 unless $d->{meanwhile};
    $d->{meanwhile}{autoUpdate} = JSON::PP::true;
    open my $o, ">", "$f.yeni" or exit 1; print $o JSON::PP->new->utf8->pretty->canonical->encode($d); close $o;
    rename "$f.yeni", $f or exit 1;
  ' "$DOSYA" && echo "› auto-update is on" || OTO=yok
else
  OTO=yok
fi
if [ "${OTO:-}" = yok ]; then
  echo "! Couldn't turn on auto-update. In Claude, go to /plugin → Marketplaces → meanwhile → Enable auto-update." >&2
fi

echo
echo "Ready. Restart Claude Code (or run /reload-plugins) and type /meanwhile."
