name: "deb-delivery"
description: "Package deb packages"
inputs:
  module_name:
    description: "The package module name"
    required: true
  distrib:
    description: "The distribution used for packaging"
    required: true
  version:
    description: "Centreon packaged major version"
    required: true
  cache_key:
    description: "The cached package key"
    required: true
  stability:
    description: "The package stability (stable, testing, unstable)"
    required: true
  artifactory_token:
    description: "Artifactory token"
    required: true
  release_type:
    description: "Type of release (hotfix, release)"
    required: true
  is_cloud:
    description: "Release context (cloud or not cloud)"
    required: true

runs:
  using: "composite"
  steps:
    - name: Use cache DEB files
      uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
      with:
        path: ./*.deb
        key: ${{ inputs.cache_key }}
        fail-on-cache-miss: true

    - uses: jfrog/setup-jfrog-cli@ff5cb544114ffc152db9cea1cd3d5978d5074946 # v4.5.11
      env:
        JF_URL: https://packages.centreon.com
        JF_ACCESS_TOKEN: ${{ inputs.artifactory_token }}

    - name: Parse distrib name
      id: parse-distrib
      uses: ./.github/actions/parse-distrib
      with:
        distrib: ${{ inputs.distrib }}

    - name: Publish DEBs
      run: |
        FILES="*.deb"

        # DEBUG
        echo "[DEBUG] - Version: ${{ inputs.version }}"
        echo "[DEBUG] - Distrib: ${{ inputs.distrib }}"
        echo "[DEBUG] - module_name: ${{ inputs.module_name }}"
        echo "[DEBUG] - is_cloud: ${{ inputs.is_cloud }}"
        echo "[DEBUG] - release_type: ${{ inputs.release_type }}"
        echo "[DEBUG] - stability: ${{ inputs.stability }}"

        # Make sure all required inputs are NOT empty
        if [[ -z "${{ inputs.module_name }}" || -z "${{ inputs.distrib }}" || -z ${{ inputs.stability }} || -z ${{ inputs.version }} || -z "${{ inputs.is_cloud }}" ]]; then
          echo "::error::some mandatory inputs are empty, please check the logs."
          exit 1
        fi

        if [[ "${{ inputs.stability }}" != "testing" && "${{ inputs.stability }}" != "unstable" ]]; then
          echo "::error::stability must be one of: testing, unstable (got '${{ inputs.stability }}')"
          exit 1
        fi

        ROOT_REPO_PATH_PREFIX=""
        if [[ "${{ steps.parse-distrib.outputs.distrib_family }}" == "debian" ]]; then
          ROOT_REPO_PATH_PREFIX="apt-"
        elif [[ "${{ steps.parse-distrib.outputs.distrib_family }}" == "ubuntu" ]]; then
          ROOT_REPO_PATH_PREFIX="ubuntu-"
        else
          echo "::error::Unsupported distribution family: ${{ steps.parse-distrib.outputs.distrib_family }}"
          exit 1
        fi

        ROOT_REPO_PATH_SUFFIX=""
        if [[ "${{ inputs.is_cloud }}" == "true" ]]; then
          ROOT_REPO_PATH_SUFFIX="-internal"
        fi

        ROOT_REPO_PATH="${ROOT_REPO_PATH_PREFIX}standard${ROOT_REPO_PATH_SUFFIX}"

        # Cleanup (equivalent to --sync-deletes done with RPMs)
        # This is a workaround the fact that jfrog cli does not allow upload
        # of multiple artifacts at once to an APT repository

        # For each local artifact
        for file in $FILES; do
          # build a search pattern using ARTIFACT_BASENAME+OS_DISTRIB+ARCH
          VERSION=${{ inputs.major_version }}
          BASENAME=$(echo "$file" | cut -d '_' -f 1)
          OS_DISTRIB=""
          ARCH=$(echo $file | cut -d '_' -f3 | cut -d '.' -f1)

          if [[ "${{ steps.parse-distrib.outputs.distrib_family }}" == "ubuntu" ]]; then
            OS_DISTRIB=$(echo "$file" | sed -nE 's/.*(ubuntu\.[0-9]{2}\.[0-9]{2}).*/\1/p')
          elif [[ "${{ steps.parse-distrib.outputs.distrib_family }}" == "debian" ]]; then
            OS_DISTRIB=$(echo "$file" | sed -nE 's/.*(\+deb[0-9]+).*/\1/p')
          else
            echo "Distrib ${{ inputs.distrib }} is not supported as OS_DISTRIB."
          fi

          # debug
          echo "[DEBUG] - Version: $VERSION"
          echo "[DEBUG] - Basename: $BASENAME"
          echo "[DEBUG] - OS_distrib: $OS_DISTRIB"
          echo "[DEBUG] - Arch: $ARCH"

          if [[ "${{ inputs.release_type }}" == "hotfix" || "${{ inputs.release_type }}" == "release" ]]; then
            POOL_SUBPATH="${{ inputs.version }}/${{ inputs.stability }}/${{ inputs.release_type }}/${{ inputs.module_name }}"
            DEB_PROP="${{ inputs.distrib }}-${{ inputs.version }}-${{ inputs.stability }}-${{ inputs.release_type }}/main/$ARCH"
          else
            POOL_SUBPATH="${{ inputs.version }}/${{ inputs.stability }}/${{ inputs.module_name }}"
            DEB_PROP="${{ inputs.distrib }}-${{ inputs.version }}-${{ inputs.stability }}/main/$ARCH"
          fi

          SEARCH_PATH="$ROOT_REPO_PATH/pool/${POOL_SUBPATH}/${BASENAME}_*${OS_DISTRIB}*${ARCH}*.deb"

          # search and delete all remote artifacts matching this pattern
          echo "Searching and deleting $SEARCH_PATH"
          jf rt delete "$SEARCH_PATH"

          # upload local artifact
          echo "Uploading $file"
          jf rt upload "$file" "$ROOT_REPO_PATH/pool/${POOL_SUBPATH}/" --flat --deb "${DEB_PROP}" --detailed-summary
        done

        curl -H "Authorization: Bearer ${{ inputs.artifactory_token }}" -X POST "https://centreon.jfrog.io/artifactory/api/deb/reindex/$ROOT_REPO_PATH?async=0"
      shell: bash
