name: build

on:
  push:
    branches: [ master ]
    tags: [ "*" ]
    paths-ignore: [ "README.md", "CHANGELOG.md", "release-notes.md" ]

  pull_request:
    branches: [ master ]

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.operating-system }}

    strategy:
      matrix:
        operating-system: [windows-latest,ubuntu-latest,macos-latest]
#        lazarus-versions: [stable, 2.2.2, 2.2.0, 2.0.12]
        lazarus-versions: [stable]

    steps:
    - name: Checkout Repository
      uses: actions/checkout@v3
      with:
        submodules: false

    - name: Install Lazarus
      uses: gcarreno/setup-lazarus@v3
      with:
        with-cache: false
        lazarus-version: ${{ matrix.lazarus-versions }}

    - name: Build the Pascal daemon
      run: lazbuild -B "src/pascalcoin_daemon.lpi"

    - name: Build reference Pascal Miner
      run: lazbuild -B "src/pascalcoin_miner.lpi"

    - name: Build the Main Wallet Application (Windows/Linux)
      if: ${{ matrix.operating-system != 'macos-latest' }}
      run: lazbuild -B "src/pascalcoin_wallet_classic.lpi"

    - name: Build the Main Wallet Application (macOS)
      if: ${{ matrix.operating-system == 'macos-latest' }}
      run: lazbuild -B --ws=cocoa "src/pascalcoin_wallet_classic.lpi"

    - name: Upload binary (Windows)
      if: ${{ (matrix.operating-system == 'windows-latest')  &&  (matrix.lazarus-versions == 'stable') }}
      uses: actions/upload-artifact@v2
      with:
        name: PascalCoinWindows
        path: |
          src/pascalcoin_daemon.exe
          src/PascalCoinMiner.exe
          src/PascalCoinWalletLazarus.exe

    - name: Upload binary (Ubuntu)
      if: ${{ (matrix.operating-system == 'ubuntu-latest')  &&  (matrix.lazarus-versions == 'stable') }}
      uses: actions/upload-artifact@v2
      with:
        name: PascalCoinLinux
        path: |
          src/pascalcoin_daemon
          src/PascalCoinMiner
          src/PascalCoinWalletLazarus

    - name: Upload binary (macOS)
      if: ${{ (matrix.operating-system == 'macos-latest')  &&  (matrix.lazarus-versions == 'stable') }}
      uses: actions/upload-artifact@v3
      with:
        name: PascalCoinMacOS
        path: |
          src/pascalcoin_daemon
          src/PascalCoinMiner
          src/PascalCoinWalletLazarus

  package-release:
    if: contains(github.ref, '/tags/')

    name: Package and create GitHub Release
    runs-on: ubuntu-latest
    needs: build

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set output
      id: vars
      run: echo "tag=${GITHUB_REF#refs/*/}" >> ${GITHUB_OUTPUT}

    - name: Download the Release binary
      uses: actions/download-artifact@v3

    - name: List files
      run: |
        ls -alF PascalCoinWindows
        ls -alF PascalCoinLinux
        ls -alF PascalCoinMacOS

    - name: Create release files
      run: |
        cd PascalCoinWindows
        cp ../windows-dll/*.dll .
        zip -r PascalCoin-${{ steps.vars.outputs.tag }}-x86_64-win64.zip *.exe *.dll
        cd ../PascalCoinLinux
        cp ../linux-libs/*.so.1.1 .
        chmod +x PascalCoin* pascalcoin*
        tar -zcvf PascalCoin-${{ steps.vars.outputs.tag }}-x86_64-linux.tar.gz PascalCoin* pascalcoin* *.so.1.1
        cd ../PascalCoinMacOS
        chmod +x PascalCoin* pascalcoin*
        tar -zcvf PascalCoin-${{ steps.vars.outputs.tag }}-x86_64-darwin.tar.gz PascalCoin* pascalcoin*

    - name: Create GitHub release
      uses: softprops/action-gh-release@v1
      with:
        name: PascalCoin ${{ steps.vars.outputs.tag }}
        body_path: release-notes.md
        files: |
          PascalCoinWindows/*.zip
          PascalCoinLinux/*.tar.gz
          PascalCoinMacOS/*.tar.gz
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

