#!/usr/bin/env bash
# kien/stack — free "try it" edition.
# ArgoCD + one GitOps Application that deploys Traefik. Nothing else.
#   curl -fsSL https://shop.kien.cc/try.sh | bash
#   ./try/install.sh            install on the current kubectl context
#   curl -fsSL https://shop.kien.cc/try.sh | bash -s uninstall  remove everything
# Prereqs: kubectl + helm, any Kubernetes cluster (kind, minikube, k3d, cloud...).
set -euo pipefail
ARGOCD_VERSION=${ARGOCD_VERSION:-10.11.6}   # argo-cd chart
TRAEFIK_VERSION=${TRAEFIK_VERSION:-42.2.0}  # traefik chart
log() { printf '\033[1;35m[kien/stack try]\033[0m %s\n' "$*"; }
die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; }

for b in kubectl helm; do command -v "$b" >/dev/null || die "$b is required"; done
kubectl version --request-timeout=10s >/dev/null 2>&1 || die "no reachable cluster in the current kubectl context"

if [ "${1:-}" = uninstall ]; then
  kubectl -n argocd delete application traefik --ignore-not-found --wait=true
  helm uninstall argocd -n argocd 2>/dev/null || true
  kubectl delete ns argocd traefik --ignore-not-found
  log "removed"; exit 0
fi

log "context: $(kubectl config current-context)"
log "installing ArgoCD (chart $ARGOCD_VERSION)"
helm upgrade --install argocd argo-cd --repo https://argoproj.github.io/argo-helm \
  --version "$ARGOCD_VERSION" -n argocd --create-namespace \
  --set dex.enabled=false --set notifications.enabled=false \
  --set 'configs.params.server\.insecure=true' --wait --timeout 10m >/dev/null

log "creating the 'traefik' Application (ArgoCD deploys it from the official chart)"
kubectl apply -f - >/dev/null <<YAML
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: traefik
  namespace: argocd
  labels: {app.kubernetes.io/part-of: kien-stack-try}
spec:
  project: default
  source:
    repoURL: https://traefik.github.io/charts
    chart: traefik
    targetRevision: "$TRAEFIK_VERSION"
    helm:
      valuesObject:
        service: {type: ${TRAEFIK_SERVICE_TYPE:-LoadBalancer}}
        ingressClass: {enabled: true, isDefaultClass: true}
  destination: {server: https://kubernetes.default.svc, namespace: traefik}
  syncPolicy:
    automated: {prune: true, selfHeal: true}
    syncOptions: [CreateNamespace=true]
YAML

log "waiting for Synced/Healthy"
for i in $(seq 1 60); do
  s=$(kubectl -n argocd get application traefik -o jsonpath='{.status.sync.status}/{.status.health.status}' 2>/dev/null || true)
  [ "$s" = "Synced/Healthy" ] && break
  # LoadBalancer stays Progressing without a cloud LB (kind/minikube): accept once pods are ready
  if [ "$s" = "Synced/Progressing" ] && kubectl -n traefik rollout status deploy/traefik --timeout=5s >/dev/null 2>&1; then break; fi
  sleep 5
done
log "traefik: ${s:-unknown} (Progressing = no cloud load balancer on this cluster, Traefik still runs on its NodePort)"

pw=$(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' | base64 -d)
cat <<TXT

  ArgoCD is managing Traefik from Git/Helm. Open the UI:
    kubectl -n argocd port-forward svc/argocd-server 8080:80
    http://localhost:8080   user: admin   password: $pw

  Try GitOps: change something and watch ArgoCD self-heal it
    kubectl -n traefik scale deploy/traefik --replicas=0   # ArgoCD puts it back

  Want certificates, secrets, Postgres, monitoring, rollouts or a private AI?
  -> the full platform: https://shop.kien.cc

  Remove: curl -fsSL https://shop.kien.cc/try.sh | bash -s uninstall
TXT
