name: Snapshot Build
on:
  push:
    branches:
      master
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build_windows:
    name: Build (Windows)
    if: github.repository == 'cc65/cc65'
    runs-on: windows-latest

    steps:
      - shell: bash
        run: git config --global core.autocrlf input

      - name: Checkout Source
        uses: actions/checkout@v4

      - name: Add msbuild to PATH
        uses: microsoft/setup-msbuild@v2

      - name: Build app (debug)
        run: msbuild src\cc65.sln -t:rebuild -property:Configuration=Debug

      - name: Build app (release)
        run: msbuild src\cc65.sln -t:rebuild -property:Configuration=Release

  build_linux:
    name: Build, Test, and Snapshot (Linux)
    if: github.repository == 'cc65/cc65'
    runs-on: ubuntu-latest

    steps:
      - name: Install Dependencies
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install linuxdoc-tools-info gcc-mingw-w64-x86-64 gcc-mingw-w64-i686

      - shell: bash
        run: git config --global core.autocrlf input
      - name: Checkout Source
        uses: actions/checkout@v4

      - name: Do some simple style checks
        shell: bash
        run: make -j2 checkstyle
      - name: Check bsearch tables
        shell: bash
        run: make -j2 sorted
      - name: Build the tools.
        shell: bash
        run: |
            make -j2 bin USER_CFLAGS=-Werror  QUIET=1
            make -j2 util QUIET=1
      - name: Build the platform libraries.
        shell: bash
        run: make -j2 lib QUIET=1
      - name: Run the regression tests.
        shell: bash
        run: make -j2 test QUIET=1
      - name: Test that the samples can be built.
        shell: bash
        run: make -j2 samples QUIET=1
      - name: Remove the output from the samples tests.
        shell: bash
        run: make -C samples clean QUIET=1
      - name: Remove programs in util directory
        shell: bash
        run: make -C util clean QUIET=1
      - name: Build the document files.
        shell: bash
        run: make -j2 doc QUIET=1
      - name: Build and package 64-bit Windows versions of the tools.
        run: |
          make -C src clean QUIET=1
          make -j2 bin QUIET=1 USER_CFLAGS=-Werror CROSS_COMPILE=x86_64-w64-mingw32-
          make zip
          mv cc65.zip cc65-snapshot-win64.zip
      - name: Build and package 32-bit Windows versions of the tools.
        run: |
          make -C src clean QUIET=1
          make -j2 bin USER_CFLAGS=-Werror QUIET=1 CROSS_COMPILE=i686-w64-mingw32-
          make zip
          mv cc65.zip cc65-snapshot-win32.zip

      - name: Upload a 32-bit Snapshot Zip
        uses: actions/upload-artifact@v4
        with:
          name: cc65-snapshot-win32
          path: cc65-snapshot-win32.zip
      - name: Upload a 64-bit Snapshot Zip
        uses: actions/upload-artifact@v4
        with:
          name: cc65-snapshot-win64
          path: cc65-snapshot-win64.zip

      - name: Get the online documents repo.
        uses: actions/checkout@v4
        with:
          repository: cc65/doc
          # this token will expire, if it does, generate a new one as decribed in https://github.com/cc65/cc65/issues/2065
          # - apparently only a "classic" token works here
          # - the token must exist in the cc65/cc65 repo
          token: ${{ secrets.DOC_PAT }} # use secret token instead of default
          path: doc.git
      - name: Update the online documents.
        run: |
          cd doc.git
          rm *.*
          cp ../html/*.* .
          git config user.name "cc65-github"
          git config user.email "cc65.nomail@github.com"
          git config push.default simple
          git add -A
          # prevent failure when there is nothing to commit
          git diff-index --quiet HEAD || git commit -m "Updated from https://github.com/cc65/cc65/commit/${GITHUB_SHA}"
          git push
      - name: Package offline documents.
        run: 7z a cc65-snapshot-docs.zip ./html/*.*
      - name: Upload a Documents Snapshot Zip
        uses: actions/upload-artifact@v4
        with:
          name: cc65-snapshot-docs
          path: cc65-snapshot-docs.zip

    # enter secrets under "repository secrets"
      - name: Upload 32-bit Windows snapshot to sourceforge
        uses: nogsantos/scp-deploy@master
        with:
          src: cc65-snapshot-win32.zip
          host: ${{ secrets.SSH_HOST }}
          remote: ${{ secrets.SSH_DIR }}
          port: ${{ secrets.SSH_PORT }}
          user: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
      - name: Upload 64-bit Windows snapshot to sourceforge
        uses: nogsantos/scp-deploy@master
        with:
          src: cc65-snapshot-win64.zip
          host: ${{ secrets.SSH_HOST }}
          remote: ${{ secrets.SSH_DIR }}
          port: ${{ secrets.SSH_PORT }}
          user: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
      - name: Upload documents snapshot to sourceforge
        uses: nogsantos/scp-deploy@master
        with:
          src: cc65-snapshot-docs.zip
          host: ${{ secrets.SSH_HOST }}
          remote: ${{ secrets.SSH_DIR }}
          port: ${{ secrets.SSH_PORT }}
          user: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}

      # TODO: Publish snapshot zip at https://github.com/cc65/cc65.github.io
