# This file is part of BOINC.
# https://boinc.berkeley.edu
# Copyright (C) 2025 University of California
#
# BOINC is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# BOINC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with BOINC.  If not, see <http://www.gnu.org/licenses/>.

name: Snap
on:
  push:
    branches: [ master ]
    tags: [ 'client_release/**' ]
  pull_request:
    branches: [ master ]
  schedule:
    - cron: '0 0 * * *'

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

env:
  AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_KEY }}
  AWS_DEFAULT_REGION: us-west-2

jobs:
  docker_build:
    name: snap-${{ matrix.type }}-build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
        - type: x64
          architecture: linux/amd64
          snap_file: boinc_amd64.snap

    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683

    - name: Install dependencies
      run: |
        sudo apt-get -qq update
        sudo apt-get install -y p7zip-full

    - name: Running from origin repo
      if: ${{ success() && env.AWS_ACCESS_KEY_ID != 0 && env.AWS_SECRET_ACCESS_KEY != 0 }}
      run: |
        echo "VCPKG_BINARY_SOURCES=clear;x-aws,s3://vcpkg.cache.boinc/,readwrite" >> $GITHUB_ENV

    - name: Running from fork
      if: ${{ success() && (env.AWS_ACCESS_KEY_ID == 0 || env.AWS_SECRET_ACCESS_KEY == 0) }}
      run: |
        echo "VCPKG_BINARY_SOURCES=clear;x-aws-config,no-sign-request;x-aws,s3://vcpkg.cache.boinc/,read" >> $GITHUB_ENV

    - name: Fix version in the snapcraft.yaml
      run: |
        PKG_VERSION=$(cat version.h | grep BOINC_VERSION_STRING | sed -e 's|#define BOINC_VERSION_STRING||' | jq -r .)
        if [[ "x${PKG_VERSION}" == "x" ]]; then
          printf "Could not obtain release package version from version.h"
          exit 1
        fi
        PKG_VERSION_FULL="${PKG_VERSION}-${{ github.run_number }}"
        sed -i -E "s/(version: \"$PKG_VERSION\")/version: \"$PKG_VERSION_FULL\"/" ${{ github.workspace }}/snap/snapcraft.yaml

    - name: Configure docker qemu
      if: success()
      uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392

    - name: Build boinc snap
      if: success()
      run: |
        docker run \
          --rm \
          --tty \
          --privileged \
          --volume $PWD:/root \
          --workdir /root \
          --platform "${{ matrix.architecture }}" \
          --env VCPKG_BINARY_SOURCES=$VCPKG_BINARY_SOURCES \
          --env AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
          --env AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
          --env AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION \
          diddledani/snapcraft:core22 \
          "snap run snapcraft --verbosity verbose pack --destructive-mode --output ${{ matrix.snap_file }}"

          sudo chown $USER ${{ matrix.snap_file }}

    - name: Install and test snap boinc inside docker
      if: success()
      run: |
        docker run \
          --rm \
          --tty \
          --privileged \
          --volume $PWD:/root \
          --workdir /root \
          --platform "${{ matrix.architecture }}" \
          diddledani/snapcraft:core22 \
          "snap install --dangerous ${{ matrix.snap_file }} && boinc --version && boinc.client --version"

    - name: Install and test snap boinc x64
      if: success() && matrix.type == 'x64'
      run: |
        sudo snap install --dangerous ${{ matrix.snap_file }}
        boinc --version
        boinc.client --version

    - name: Prepare logs on failure
      if: ${{ failure() }}
      run: |
        sudo chown -R $USER parts/boinc/build/3rdParty/linux/vcpkg/buildtrees/
        python ./deploy/prepare_deployment.py logs

    - name: Upload logs on failure
      if: ${{ failure() }}
      uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
      with:
        name: snap_logs_${{ matrix.type }}_${{ github.event.pull_request.head.sha }}
        path: deploy/logs.7z

    - name: Publish nightly snap build to the edge
      if: success() && matrix.type == 'x64' && github.ref == 'refs/heads/master' && github.event_name == 'schedule'
      uses: canonical/action-publish@214b86e5ca036ead1668c79afb81e550e6c54d40
      env:
        SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAP_LOGIN }}
      with:
        snap: ${{ matrix.snap_file }}
        release: edge

    - name: Publish alpha snap build to the beta
      if: success() && matrix.type == 'x64' && startsWith(github.ref, 'refs/tags/')
      uses: canonical/action-publish@214b86e5ca036ead1668c79afb81e550e6c54d40
      env:
        SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAP_LOGIN }}
      with:
        snap: ${{ matrix.snap_file }}
        release: beta

    - name: Upload artifacts
      uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
      if: success()
      with:
        name: linux_snap_${{ matrix.type }}_${{ github.event.pull_request.head.sha }}
        path: boinc_*.snap
