# This is a basic workflow to help you get started with Actions

name: mac-pwsafe

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build_mac_pwsafe:
    name: Build pwsafe for macOS
    # The type of runner that the job will run on
    runs-on: macos-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2

    - name: Set build parameters
      # If you need to rebuild wxWidgets for some reason, say you changed the build params, just increment FORCE_REBUILD_WX
      run: |
        echo "WX_VERS=3.0.5" >> $GITHUB_ENV
        echo "MACOS_VER_MIN=10.9" >> $GITHUB_ENV
        echo "WX_SRC_DIR=$HOME/wxWidgets" >> $GITHUB_ENV
        echo "WX_BUILD_DIR=$HOME/wxbuild" >> $GITHUB_ENV
        echo "XCODE_VER=11.7" >> $GITHUB_ENV
        echo "FORCE_REBUILD_WX=2" >> $GITHUB_ENV

    - name: Force compatible Xcode version
      run: sudo xcode-select -s /Applications/Xcode_${XCODE_VER}.app/Contents/Developer

    - name: Print Xcode version in use
      run: xcode-select -p

    - name: Cache wxWidgets build
      id: cache-wxWidgets-build
      uses: actions/cache@v2
      with:
        path: ${{ env.WX_BUILD_DIR }}
        key: wx-${{ env.WX_VERS }}-macOS-${{ env.MACOS_VER_MIN }}-Xcode-${{ env.XCODE_VER }}-build-${{ env.FORCE_REBUILD_WX }}

    - name: Checkout wxWidgets
      if: steps.cache-wxWidgets-build.outputs.cache-hit != 'true'
      run: git clone https://github.com/wxWidgets/wxWidgets.git $WX_SRC_DIR

    - name: Build wxWidgets
      if: steps.cache-wxWidgets-build.outputs.cache-hit != 'true'
      working-directory: ${{ env.WX_SRC_DIR }}
      run: |
        git pull --ff-only origin
        git -C $WX_SRC_DIR reset --hard v${{ env.WX_VERS }}
        git -C $WX_SRC_DIR submodule update --init
        rm -rf $WX_BUILD_DIR
        mkdir -p $WX_BUILD_DIR
        cd $WX_SRC_DIR
        # use built-in versions of jpeg, tiff & png libs to avoid linking with those in /usr/local/opt
        ./configure --prefix=$WX_BUILD_DIR --disable-shared --enable-unicode --with-macosx-version-min=$MACOS_VER_MIN --with-libpng=builtin --with-libjpeg=builtin --with-libtiff=builtin --without-liblzma
        make
        make install
        du -sh $WX_BUILD_DIR

    - name: Generate xcconfigs
      working-directory: ${{ github.workspace }}
      run: Xcode/generate-configs -r $WX_BUILD_DIR/bin/wx-config > Xcode/pwsafe-release.xcconfig
    
    - name: Build pwsafe
      working-directory: ${{ github.workspace }}
      run: xcodebuild -project Xcode/pwsafe-xcode6.xcodeproj -scheme pwsafe

    - name: Get Xcode build output location
      run: echo "BUILD_OUTPUT_DIR=$(xcodebuild -showBuildSettings -project Xcode/pwsafe-xcode6.xcodeproj | fgrep CONFIGURATION_BUILD_DIR | cut -d= -f2 | sed 's/^ *\(.*\)/\1/')" >> $GITHUB_ENV

    - name: Move app to another folder
      run: mkdir "$BUILD_OUTPUT_DIR"/app && mv "$BUILD_OUTPUT_DIR"/pwsafe.app "$BUILD_OUTPUT_DIR"/app/

    - name: Install dependencies for creating dmg
      run: brew install create-dmg

    - name: Create dmg
      run: create-dmg --volname "Password Safe" --volicon "src/ui/wxWidgets/graphics/pwsafe.icns" --eula LICENSE ${{ github.workspace }}/PasswordSafe.dmg "$BUILD_OUTPUT_DIR/app"

    - name: Upload artifacts
      uses: 'actions/upload-artifact@v2'
      with:
        name: PasswordSafe.${{ github.sha }}.dmg
        path: ${{ github.workspace }}/PasswordSafe.dmg
