name: Nightly

on:
  push:
    paths-ignore:
     - 'README.md'
#  schedule:
#    - cron:  '0 0 * * *'
  workflow_dispatch:
    inputs:
      version:
        description: dummy
        default: dummy

defaults:
  run:
    shell: bash

jobs:
  macos-compile:
    runs-on: macos-12
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4

    - name: Display the OS Version1
      run: sw_vers -productVersion

    - name: Display the OS Version2
      run: system_profiler SPSoftwareDataType

    - name: install pkgs
      run:  |
            brew install ldid

    - name: xcode info
      run:  |
            ls -al
            pwd
            id -a
            pod install
            xcode-select --print-path
            xcodebuild -list -workspace Antidote.xcworkspace
            xcodebuild -list -project Antidote.xcodeproj

    - name: xcode list destinations
      run:  |
            env NSUnbufferedIO=YES xcodebuild -workspace ./Antidote.xcworkspace -scheme Antidote -destination "name=NoSuchName" -destination-timeout 1 || echo 'NO ERR'
            xcrun xctrace list devices || echo 'NOERR'

    - name: xcode compile
      run:  |
            echo "iPhone 11 Simulator"
            set -x
            id=`env NSUnbufferedIO=YES xcodebuild -workspace ./Antidote.xcworkspace -scheme Antidote -destination "name=NoSuchName" -destination-timeout 1 2>&1 | grep 'platform:iOS Simulator,' | grep 'name:iPhone 11 }' | tail -1 | sed -e 's#.* id:##'|sed -e 's#, OS:.*$##'` || echo 'NOERR'
            echo $id
            env NSUnbufferedIO=YES xcodebuild -workspace ./Antidote.xcworkspace -scheme Antidote -destination "platform=iOS Simulator,id=$id"

    - name: xcode compile for IOS
      run:  |
            echo "IOS"
            env NSUnbufferedIO=YES xcodebuild -workspace ./Antidote.xcworkspace -configuration Release archive -archivePath build/Antidote.xcarchive -scheme Antidote CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED="NO" CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO"
            ls -al ./build/Antidote.xcarchive
            ldid -S $(pwd)/build/Antidote.xcarchive/Products/Applications/Antidote.app/Antidote
            echo codesign -fs "keyname" $(pwd)/build/Antidote.xcarchive/Products/Applications/Antidote.app/Antidote || echo "dummy"
            ldid -d $(pwd)/build/Antidote.xcarchive/Products/Applications/Antidote.app/Antidote
            ./tools/package-ida.sh $(pwd)/build/Antidote.xcarchive $(pwd)/Antidote_unsigned.ipa

    - name: check compile output
      run:  |
            ls -al /Users/runner/Library/Developer/Xcode/DerivedData/Antidote-bhjjxfvewsypipfpuadvlmuxuzew/Build/Products/Debug-iphonesimulator/Antidote.app/
            file /Users/runner/Library/Developer/Xcode/DerivedData/Antidote-bhjjxfvewsypipfpuadvlmuxuzew/Build/Products/Debug-iphonesimulator/Antidote.app/Antidote

    - name: upload IPA file
      uses: actions/upload-artifact@v4
      with:
        name: ipa
        path: |
          /Users/runner/work/Antidote/Antidote/Antidote_unsigned.ipa

    - name: Rename artifact for release upload
      if: contains(github.ref, 'refs/tags/v')
      run: |
        echo "RELEASE_VERSION1=${GITHUB_REF#refs/*/}"
        echo "RELEASE_VERSION2=${{github.ref_name}}"
        cp -v /Users/runner/work/Antidote/Antidote/Antidote_unsigned.ipa Antidote_unsigned_${GITHUB_REF#refs/*/}.ipa
    - name: Upload to versioned release
      if: contains(github.ref, 'refs/tags/v')
      uses: ncipollo/release-action@v1
      with:
        allowUpdates: true
        draft: true
        token: ${{ secrets.GITHUB_TOKEN }}
        artifacts: "Antidote_unsigned*.ipa"
