#!/usr/bin/env bash
set -euo pipefail
CATALOG="${CATALOG:-/tmp/ligbox-catalog-20.csv}"

create_op() {
  local slug="$1" seats="$2" domains="$3" websites="$4" disk_gb="$5"
  if opencli plan-list 2>/dev/null | grep -q "| ${slug} "; then
    echo "exists $slug"
    return 0
  fi
  local emails=$(( seats * 2 )); [[ $emails -lt 10 ]] && emails=10
  [[ $disk_gb -lt 2 ]] && disk_gb=2
  opencli plan-create name="${slug}" description="Ligbox ${slug}" emails="${emails}" ftp=2 \
    domains="${domains}" websites="${websites}" disk="${disk_gb}" inodes=500000 databases=2 \
    cpu=100 ram=1024 bandwidth=102400 feature_set=default max_email_quota=512G max_hourly_email=500
  echo "created $slug"
}

tail -n +2 "$CATALOG" | while IFS='|' read -r slug title category price seats mail_gb files_gb op_domains op_websites op_disk_gb foss_type; do
  [[ "$foss_type" == "custom" ]] && continue
  [[ -z "$slug" ]] && continue
  create_op "$slug" "$seats" "$op_domains" "$op_websites" "$op_disk_gb" || echo "FAIL $slug"
done
echo "--- OP plans ligbox ---"
opencli plan-list 2>/dev/null | grep -E 'ligbox|----'
