# SPDX-FileCopyrightText: Stone Tickle <lattis@mochiro.moe>
# SPDX-License-Identifier: GPL-3.0-only

name: macos

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
    types: [opened, synchronize, reopened]

permissions:
  contents: read

jobs:
  macos:
    runs-on: macos-latest
    environment: master
    steps:
    - uses: actions/checkout@v3
    - name: deps
      run: |
        set -x
        mkdir -p build
        arch
        curl -L -o build/pkgconf-1.9.3.tar.gz https://github.com/pkgconf/pkgconf/archive/refs/tags/pkgconf-1.9.3.tar.gz
        tar xvf build/pkgconf-1.9.3.tar.gz
        mv pkgconf-pkgconf-1.9.3 subprojects/pkgconf
    - name: build
      run: |
        ./bootstrap.sh build
        PATH="build:$PATH" build/muon -v setup build
        build/muon -C build samu
    - name: test
      run: |
        build/muon -C build test -v -ddots
    - name: build-x86_64
      run: |
        arch -x86_64 sh -c '
          ./bootstrap.sh build-x86_64 &&\
          PATH="build-x86_64:$PATH" build-x86_64/muon -v setup build-x86_64 &&\
          build-x86_64/muon -C build-x86_64 samu\
        '
    - name: build-universal-binary
      run: |
        ver="$(cat build/version.txt)"
        lipo -create -output muon build-x86_64/muon build/muon
        zip "muon-${ver}-universal-macos.zip" muon
    - name: upload-universal-binary
      if: github.ref == 'refs/heads/master'
      env:
        GH_DEPLOY: 1
      run: |
        ver="$(cat build/version.txt)"
        echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/deploy
        chmod 600 ~/.ssh/deploy
        cat >> ~/.ssh/config <<EOF
        Host mochiro.moe
            IdentityFile ~/.ssh/deploy
            IdentitiesOnly yes
        EOF
        tools/ci/deploy.sh "/releases/${ver}" \
          "muon-${ver}-universal-macos.zip"
