name:  Nightly

on:
  workflow_dispatch:
  schedule:
    - cron: 0 20 * * *

jobs:
  build_windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v1
      - name: Install cURL
        run: choco install curl
      - name: Download and unpack LLVM bins
        shell: cmd
        run: |
          cd bin
          curl -sL https://github.com/odin-lang/Odin/releases/download/llvm-windows/llvm-binaries.zip --output llvm-binaries.zip
          7z x llvm-binaries.zip > nul
          rm -f llvm-binaries.zip
      - name: build Odin
        shell: cmd
        run: |
          call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat
          ./build.bat 1 1
      - name: Odin run
        shell: cmd
        run: |
          call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat
          odin run examples/demo/demo.odin
      - name: Copy artifacts
        run: |
          rm bin/llvm/windows/LLVM-C.lib
          mkdir dist
          cp odin.exe dist
          cp LLVM-C.dll dist
          cp -r shared dist
          cp -r core dist
          cp -r bin dist
          cp -r examples dist
      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: windows_artifacts
          path: dist
  build_ubuntu:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: (Linux) Download LLVM
        run: sudo apt-get install llvm
      - name: build odin
        run: make nightly
      - name: Odin run
        run: ./odin run examples/demo/demo.odin
      - name: Copy artifacts
        run: |
          mkdir dist
          cp odin dist
          cp -r shared dist
          cp -r core dist
          cp -r examples dist
      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: ubuntu_artifacts
          path: dist
  build_macos:
    runs-on: macOS-latest
    steps:
      - uses: actions/checkout@v1
      - name: Download LLVM and setup PATH
        run: |
          brew install llvm
          echo "/usr/local/opt/llvm/bin" >> $GITHUB_PATH
          TMP_PATH=$(xcrun --show-sdk-path)/user/include
          echo "CPATH=$TMP_PATH" >> $GITHUB_ENV
      - name: build odin
        run: make nightly
      - name: Odin run
        run: ./odin run examples/demo/demo.odin
      - name: Copy artifacts
        run: |
          mkdir dist
          cp odin dist
          cp -r shared dist
          cp -r core dist
          cp -r examples dist
      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: macos_artifacts
          path: dist
  upload_b2:
    runs-on: [ubuntu-latest]
    needs: [build_windows, build_macos, build_ubuntu]
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      
      - name: Install B2 CLI
        shell: bash
        run: |
          python -m pip install --upgrade pip
          pip install --upgrade b2
      
      - name: Display Python version
        run: python -c "import sys; print(sys.version)"
      
      - name: Download Windows artifacts
        uses: actions/download-artifact@v1
        with:
          name: windows_artifacts
      
      - name: Download Ubuntu artifacts
        uses: actions/download-artifact@v1
        with:
          name: ubuntu_artifacts
      
      - name: Download macOS artifacts
        uses: actions/download-artifact@v1
        with:
          name: macos_artifacts

      - name: Create archives and upload
        shell: bash
        env:
          APPID: ${{ secrets.B2_APPID }}
          APPKEY: ${{ secrets.B2_APPKEY }}
          BUCKET: ${{ secrets.B2_BUCKET }}
          DAYS_TO_KEEP: ${{ secrets.B2_DAYS_TO_KEEP }}
        run: |
          b2 authorize-account "$APPID" "$APPKEY"

          chmod +x ./ci/upload_create_nightly.sh
          ./ci/upload_create_nightly.sh "$BUCKET" windows-amd64 windows_artifacts/
          ./ci/upload_create_nightly.sh "$BUCKET" ubuntu-amd64 ubuntu_artifacts/
          ./ci/upload_create_nightly.sh "$BUCKET" macos-amd64 macos_artifacts/

          python3 ci/delete_old_binaries.py "$BUCKET" "$DAYS_TO_KEEP"

          python3 ci/create_nightly_json.py "$BUCKET" > nightly.json
          b2 upload-file "$BUCKET" nightly.json nightly.json

          b2 clear-account
