#!/bin/sh
# Voeg snel een website toe: ./add-website.sh  of  ./add-website.sh "Naam" "url" "groep"
set -e
APP="$(cd "$(dirname "$0")" && pwd)"
FILE="$APP/websites-add.txt"

usage() {
  echo "Gebruik:"
  echo "  $0"
  echo "  $0 \"Naam\" \"http://url\" [groep]"
  echo ""
  echo "Groepen: Infrastructure | \"Media & TV\" | \"Smart Home\" | Productivity"
  echo "         \"Tools & Utils\" | \"AI Assistants\" | \"Dev & Docs\" | \"Web Design\""
  exit 1
}

pick_group() {
  echo "Kies groep:"
  echo "  1) Infrastructure   5) Tools & Utils"
  echo "  2) Media & TV         6) AI Assistants"
  echo "  3) Smart Home         7) Dev & Docs"
  echo "  4) Productivity       8) Web Design"
  printf "> "
  read -r n
  case "$n" in
    1) echo "Infrastructure" ;;
    2) echo "Media & TV" ;;
    3) echo "Smart Home" ;;
    4) echo "Productivity" ;;
    5) echo "Tools & Utils" ;;
    6) echo "AI Assistants" ;;
    7) echo "Dev & Docs" ;;
    8) echo "Web Design" ;;
    *) echo "Productivity" ;;
  esac
}

if [ $# -eq 0 ]; then
  printf "Naam: "
  read -r NAME
  printf "URL: "
  read -r URL
  GROUP=$(pick_group)
elif [ $# -ge 2 ]; then
  NAME="$1"
  URL="$2"
  GROUP="${3:-Productivity}"
else
  usage
fi

[ -n "$NAME" ] && [ -n "$URL" ] || usage

# Pipe in naam/url escapen
NAME_ESC=$(printf '%s' "$NAME" | sed 's/|/-/g')
echo "${GROUP}|${NAME_ESC}|${URL}" >> "$FILE"
echo "Toegevoegd: ${GROUP} | ${NAME} | ${URL}"
echo ""
"$APP/apply.sh"
