apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: dataset-download
  labels:
    dagweave.io/catalog-item: dataset-download
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "AI"
spec:
  entrypoint: fetch
  templates:
    - name: fetch
      inputs:
        parameters:
          # Absolute URL of the dataset archive to download.
          - name: url
          # Directory to download into and unpack the archive under.
          - name: dest-path
            default: /tmp/dataset
          # Name/key of a Kubernetes Secret holding an optional bearer token. If the
          # Secret or key is absent no Authorization header is sent.
          - name: token-secret-name
            default: dataset-token
          - name: token-secret-key
            default: token
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # alpine:3.24 — provides sh, wget, tar and unzip (busybox).
        image: alpine@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
        command: [sh, -c]
        # Values are passed as env vars (below), never interpolated into the shell.
        # Downloads the archive with wget (optionally authenticated) then unpacks it
        # by extension. Unlike download-file this item also extracts the archive so a
        # downstream step sees the unpacked dataset tree.
        args:
          - |
            set -eu
            mkdir -p "${DEST_PATH}"
            if [ -n "${TOKEN:-}" ]; then
              wget --header="Authorization: Bearer ${TOKEN}" \
                -O "${DEST_PATH}/archive" "${URL}"
            else
              wget -O "${DEST_PATH}/archive" "${URL}"
            fi
            case "${URL}" in
              *.tar.gz|*.tgz) tar -xzf "${DEST_PATH}/archive" -C "${DEST_PATH}" ;;
              *.tar)          tar -xf  "${DEST_PATH}/archive" -C "${DEST_PATH}" ;;
              *.zip)          unzip -o "${DEST_PATH}/archive" -d "${DEST_PATH}" ;;
              *) echo "no known archive extension; left as ${DEST_PATH}/archive" ;;
            esac
            echo "dataset ready under ${DEST_PATH}"
            ls -la "${DEST_PATH}"
        env:
          - name: URL
            value: "{{inputs.parameters.url}}"
          - name: DEST_PATH
            value: "{{inputs.parameters.dest-path}}"
          - name: TOKEN
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.token-secret-name}}"
                key: "{{inputs.parameters.token-secret-key}}"
                optional: true
