version: "3"

tasks:
  release:
    cmds:
      - uv run semantic-release -v version --no-vcs-release
      - git push
      - git push --tags

  docs:widoco:
    desc: "Generate WIDOCO HTML docs for a spec version (default: latest). Pass version as: task docs:widoco -- vX.X"
    vars:
      VERSION:
        sh: |
          if [ -n "{{.CLI_ARGS}}" ]; then
            echo "{{.CLI_ARGS}}"
          else
            ls -d specs/*/ 2>/dev/null \
              | sed 's:/*$::' \
              | grep -v current \
              | sort -V \
              | tail -n 1 \
              | xargs basename
          fi
    cmds:
      - |
        set -e
        version="{{.VERSION}}"
        input="specs/$version/celine.ttl"
        output="releases/$version"

        if [ ! -f "$input" ]; then
          echo "Ontology file not found: $input"
          exit 1
        fi

        echo "Generating WIDOCO docs for $version..."
        mkdir -p "$output"
        chmod 777 "$output"

        docker run --rm \
          -v "$(pwd)/specs/$version:/usr/local/widoco/in:ro" \
          -v "$(pwd)/$output:/usr/local/widoco/out" \
          ghcr.io/dgarijo/widoco:v1.4.23 \
          -ontFile /usr/local/widoco/in/celine.ttl \
          -outFolder /usr/local/widoco/out \
          -rewriteAll \
          -lang en \
          -includeAnnotationProperties

        echo "Docs generated at $output"

  release:ontology:
    desc: Point specs/current to the latest versioned spec directory
    cmds:
      - |
        set -e
        echo "Detecting latest spec..."

        latest_dir=$(ls -d specs/*/ 2>/dev/null \
          | sed 's:/*$::' \
          | grep -v current \
          | sort -V \
          | tail -n 1)

        if [ -z "$latest_dir" ]; then
          echo "No spec directories found under specs/"
          exit 1
        fi

        latest=$(basename "$latest_dir")

        ln -sfn "$latest" specs/current
        echo "Updated symlink: specs/current → $latest"

        git add .
        git commit -m "release $latest"

        git tag -f $latest
        git push -f
