name: Centreon collect packaging

on:
  workflow_call:
    inputs:
      major_version:
        required: true
        type: string
      minor_version:
        required: true
        type: string
      img_version:
        required: true
        type: string
      release:
        required: true
        type: string
      commit_hash:
        required: true
        type: string
      stability:
        required: true
        type: string
      legacy_engine:
        required: true
        type: string
      packages_in_artifact:
        required: true
        type: boolean
      image:
        required: true
        type: string
      distrib:
        required: true
        type: string
      package_extension:
        required: true
        type: string
      runner:
        required: true
        type: string
      arch:
        required: true
        type: string


jobs:
  package:
    runs-on: ${{ fromJson(format('["self-hosted", "{0}"]', inputs.runner)) }}

    env:
      SCCACHE_PATH: "/usr/bin/sccache"
      SCCACHE_BUCKET: "centreon-github-sccache"
      SCCACHE_REGION: "eu-west-1"
      AWS_ACCESS_KEY_ID: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.COLLECT_S3_SECRET_KEY }}

    container:
      image: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ inputs.image }}:${{ inputs.img_version }}
      credentials:
        username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
        password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}

    name: package ${{ inputs.distrib }} ${{ inputs.arch }}

    steps:
      - name: Checkout sources
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Install sccache
        run: |
          if [ "${{ inputs.package_extension }}" = "deb" ]; then
            apt-get update
            apt-get install -y wget
          elif [ "${{ inputs.package_extension }}" = "rpm" ]; then
            dnf install -y wget
          fi
          if [ "${{ inputs.arch }}" = "amd64" ]; then
            wget https://github.com/mozilla/sccache/releases/download/v0.8.1/sccache-v0.8.1-x86_64-unknown-linux-musl.tar.gz
            tar xzf sccache-v0.8.1-x86_64-unknown-linux-musl.tar.gz
            mv sccache-v0.8.1-x86_64-unknown-linux-musl/sccache /usr/bin/
          elif [ "${{ inputs.arch }}" = "arm64" ]; then
            wget https://github.com/mozilla/sccache/releases/download/v0.8.1/sccache-v0.8.1-aarch64-unknown-linux-musl.tar.gz
            tar xzf sccache-v0.8.1-aarch64-unknown-linux-musl.tar.gz
            mv sccache-v0.8.1-aarch64-unknown-linux-musl/sccache /usr/bin/
          fi
          ${SCCACHE_PATH} --start-server

      - name: Generate selinux binaries
        if: ${{ inputs.package_extension == 'rpm' }}
        run: |
          cd selinux
          for MODULE in "centreon-engine" "centreon-broker" "centreon-monitoring-agent"; do
            cd $MODULE
            sed -i "s/@VERSION@/${{ inputs.major_version }}.${{ inputs.minor_version }}/g" $MODULE.te
            make -f /usr/share/selinux/devel/Makefile
            cd -
          done
        shell: bash

      - name: Remove selinux packaging files on debian
        if: ${{ inputs.package_extension == 'deb' }}
        run: rm -f packaging/*-selinux.yaml
        shell: bash

      - name: Compile sources
        run: |
          CMAKE="cmake3"
          if [ "${{ inputs.package_extension }}" = "deb" ]; then
            CMAKE="cmake"
          fi

          if [ "${{ inputs.arch }}" = "arm64" ]; then
            export VCPKG_FORCE_SYSTEM_BINARIES=1
            export TRIPLET=arm64-linux-release
          else
            export TRIPLET=x64-linux-release
          fi

          mv /root/.cache /github/home/
          export VCPKG_ROOT="/vcpkg"
          export PATH="$VCPKG_ROOT:$PATH"
          $CMAKE \
                  -B build \
                  -DVCPKG_OVERLAY_TRIPLETS=/custom-triplets \
                  -DVCPKG_TARGET_TRIPLET=$TRIPLET \
                  -DVCPKG_OVERLAY_PORTS=/overlays \
                  -GNinja \
                  -DDEBUG_ROBOT=OFF \
                  -DWITH_TESTING=OFF \
                  -DWITH_BENCH=ON \
                  -DWITH_MODULE_SIMU=OFF \
                  -DCMAKE_INSTALL_PREFIX=/usr \
                  -DWITH_STARTUP_SCRIPT=systemd \
                  -DWITH_ENGINE_LOGROTATE_SCRIPT=ON \
                  -DWITH_USER_BROKER=centreon-broker \
                  -DWITH_GROUP_BROKER=centreon-broker \
                  -DWITH_USER_ENGINE=centreon-engine \
                  -DWITH_GROUP_ENGINE=centreon-engine \
                  -DWITH_VAR_DIR=/var/log/centreon-engine \
                  -DWITH_DAEMONS=ON \
                  -DWITH_CREATE_FILES=OFF \
                  -DWITH_CONFIG_FILES=ON \
                  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
                  -DCMAKE_C_COMPILER_LAUNCHER=${SCCACHE_PATH} \
                  -DCMAKE_CXX_COMPILER_LAUNCHER=${SCCACHE_PATH} \
                  -DLEGACY_ENGINE=${{ inputs.legacy_engine }} \
                  -S .

          ninja -Cbuild
        shell: bash

      - name: Cache statistics
        run: ${SCCACHE_PATH} --show-stats
        shell: bash

      - name: Stop sccache server
        run: ${SCCACHE_PATH} --stop-server
        shell: bash

      - name: Generate debug files
        run: |
          for file in $(find build/{broker,engine,clib,connectors} -name '*.so' -type f); do
            echo "Making a debug file of $file"
            objcopy --only-keep-debug $file $file.debug
            objcopy --strip-debug $file
            objcopy --add-gnu-debuglink $file.debug $file
          done
          exe=("build/broker/cbd"
               "build/broker/watchdog/cbwd"
               "build/engine/centengine"
               "build/engine/centenginestats"
               "build/engine/modules/bench/centengine_bench_passive"
               "build/connectors/perl/centreon_connector_perl"
               "build/connectors/ssh/centreon_connector_ssh"
               "build/ccc/ccc"
               "build/agent/centagent")
          for file in "${exe[@]}"; do
            echo "Making a debug file of $file"
            objcopy --only-keep-debug $file $file.debug
            objcopy --strip-debug $file
            objcopy --add-gnu-debuglink $file.debug $file
          done
        shell: bash

      - uses: ./.github/actions/package
        with:
          nfpm_file_pattern: "packaging/*.yaml"
          distrib: ${{ inputs.distrib }}
          package_extension: ${{ inputs.package_extension }}
          major_version: ${{ inputs.major_version }}
          minor_version: ${{ inputs.minor_version }}
          release: ${{ inputs.release }}
          arch: ${{ inputs.arch }}
          commit_hash: ${{ inputs.commit_hash }}
          cache_key: ${{ github.run_id }}-${{ github.sha }}-${{ inputs.package_extension}}-centreon-collect-${{ inputs.distrib }}-${{ inputs.arch }}-${{ github.head_ref || github.ref_name }}
          rpm_gpg_key: ${{ secrets.RPM_GPG_SIGNING_KEY }}
          rpm_gpg_signing_key_id: ${{ secrets.RPM_GPG_SIGNING_KEY_ID }}
          rpm_gpg_signing_passphrase: ${{ secrets.RPM_GPG_SIGNING_PASSPHRASE }}
          stability: ${{ inputs.stability }}

      - name: Cleaning not needed packages
        shell: bash
        run: rm -rf *-debuginfo*.${{ inputs.package_extension }}

      # set condition to true if artifacts are needed
      - if: inputs.packages_in_artifact == true
        name: Upload package artifacts
        uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
        with:
          name: packages-${{ inputs.distrib }}-${{ inputs.arch }}
          path: ./*.${{ inputs.package_extension}}
          retention-days: 1
