name: Mac

on:
  release:
    types: [created]

jobs:
  build-on-mac-10_13:
    runs-on: [self-hosted, macOS, X64, "10.13"]

    strategy:
      matrix:
        include:
          - name: "synergy"
            remote_folder: "v1-core-standard"
            enterprise: ""
            business: ""
          - name: "synergy-enterprise"
            remote_folder: "v1-core-enterprise"
            enterprise: "1"
            business: ""
          - name: "synergy-business"
            remote_folder: "v1-core-business"
            enterprise: ""
            business: "1"
    env:
      GIT_COMMIT: ${{ github.sha }}
      SYNERGY_ENTERPRISE: ${{ matrix.enterprise }}
      SYNERGY_BUSINESS: ${{ matrix.business }}
      Qt5_DIR: /usr/local/opt/qt/5.15.2/clang_64
      OpenSSL_DIR: /usr/local/ssl
      CODESIGN_ID: "Developer ID Application: Symless Ltd (4HX897Y6GJ)"

    steps:
      - uses: actions/checkout@v2

      - name: Setup Environment
        run: |
          python3 -m pip install dmgbuild

      - name: Build SYNERGY
        env:
          CMAKE_PREFIX_PATH: "${{ env.Qt5_DIR }};${{ env.OpenSSL_DIR }}"
        run: |
          export PATH="$Qt5_DIR/bin:$PATH"
          python3 CI/build_version.py
          mkdir build
          cd build
          cmake \
            -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13 \
            -DCMAKE_OSX_ARCHITECTURES=x86_64 \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_CONFIGURATION_TYPES=Release ..
          . ./version
          make -j
          make install/strip

      - name: Version Info
        id: version
        run: |
          . ./build/version
          SYNERGY_VERSION="${SYNERGY_VERSION_MAJOR}.${SYNERGY_VERSION_MINOR}.${SYNERGY_VERSION_PATCH}"
          SYNERGY_REVISION=$(git rev-parse --short=8 HEAD)
          SYNERGY_DMG_VERSION="${SYNERGY_VERSION}-${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}"
          echo "::set-output name=SYNERGY_VERSION_MAJOR::$SYNERGY_VERSION_MAJOR"
          echo "::set-output name=SYNERGY_VERSION_MINOR::$SYNERGY_VERSION_MINOR"
          echo "::set-output name=SYNERGY_VERSION_PATCH::$SYNERGY_VERSION_PATCH"
          echo "::set-output name=SYNERGY_VERSION_STAGE::$SYNERGY_VERSION_STAGE"
          echo "::set-output name=SYNERGY_VERSION_BUILD::$SYNERGY_VERSION_BUILD"
          echo "::set-output name=SYNERGY_VERSION::$SYNERGY_VERSION"
          echo "::set-output name=SYNERGY_REVISION::$SYNERGY_REVISION"
          echo "::set-output name=SYNERGY_DMG_VERSION::$SYNERGY_DMG_VERSION"
          SYNERGY_PACKAGE_NAME=${{ matrix.name }}
          SYNERGY_REMOTE_FOLDER="${{ matrix.remote_folder }}/${SYNERGY_VERSION}/${SYNERGY_VERSION_STAGE}/b${SYNERGY_VERSION_BUILD}-${SYNERGY_REVISION}"
          SYNERGY_DMG_FILENAME="${SYNERGY_PACKAGE_NAME}_${SYNERGY_DMG_VERSION}_macos-10.13_x86-64.dmg"
          echo "SYNERGY_REMOTE_FOLDER: $SYNERGY_REMOTE_FOLDER"
          echo "::set-output name=SYNERGY_REMOTE_FOLDER::$SYNERGY_REMOTE_FOLDER"
          echo "::set-output name=SYNERGY_PACKAGE_NAME::$SYNERGY_PACKAGE_NAME"
          echo "::set-output name=SYNERGY_DMG_FILENAME::$SYNERGY_DMG_FILENAME"

      - name: Sign applicaiton
        run: |
          export PATH="$Qt5_DIR/bin:$PATH"
          macdeployqt ${{ github.workspace }}/build/bundle/Synergy.app -codesign="$CODESIGN_ID"
          codesign -f --options runtime --deep -s "$CODESIGN_ID" ${{ github.workspace }}/build/bundle/Synergy.app
          ln -s /Applications ${{ github.workspace }}/build/bundle/Applications

      - name: Create Installer
        env:
          SYNERGY_DMG_FILENAME: ${{ steps.version.outputs.SYNERGY_DMG_FILENAME }}
        run: |
          dmgbuild \
            -s CI/MacOS/installator_settings.py \
            -D app=${{ github.workspace }}/build/bundle/Synergy.app \
            -D background=${{ github.workspace }}/build/bundle/Synergy.app/Contents/Resources/.installer_background.tiff \
            "Synergy" \
            $SYNERGY_DMG_FILENAME
          mkdir pkg
          mv $SYNERGY_DMG_FILENAME pkg/
          cd pkg
          md5 -r $SYNERGY_DMG_FILENAME >> $SYNERGY_DMG_FILENAME.checksum.txt
          shasum $SYNERGY_DMG_FILENAME >> $SYNERGY_DMG_FILENAME.checksum.txt
          shasum -a 256 $SYNERGY_DMG_FILENAME >> $SYNERGY_DMG_FILENAME.checksum.txt

      - name: Submit for Notarization
        env:
          ASC_USERNAME: ${{ secrets.ASC_USERNAME }}
          NOTORY_APP_PASSWORD: ${{ secrets.NOTORY_APP_PASSWORD }}
          SYNERGY_VERSION: ${{ steps.version.outputs.SYNERGY_VERSION }}
          SYNERGY_REVISION: ${{ steps.version.outputs.SYNERGY_REVISION }}
          SYNERGY_DMG_FILENAME: ${{ steps.version.outputs.SYNERGY_DMG_FILENAME }}
        run: |
          cd pkg
          ../CI/MacOS/notorize.sh

      - name: Send package to Binary Storage
        uses: garygrossgarten/github-action-scp@v0.7.3
        with:
          local: "${{ github.workspace }}/pkg/"
          remote: "${{ secrets.BINARIES_SSH_DIR }}/${{ steps.version.outputs.SYNERGY_REMOTE_FOLDER }}/"
          host: ${{ secrets.BINARIES_SSH_HOST }}
          username: ${{ secrets.BINARIES_SSH_USER }}
          privateKey: ${{ secrets.BINARIES_SSH_KEY }}
