name: Nightly

on:
  workflow_dispatch:
  schedule:
    - cron: '53 7 * * *'

permissions:
  contents: read

jobs:
  linux:

    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Install dependencies
      run: |
          ghc --version
          cabal --version
          cabal update
          cabal build --dependencies-only -fembed_data_files -fnightly pandoc-cli
    - name: Build and install artifact
      run: |
          export ARTIFACTS=nightly-linux/pandoc-nightly-linux-$(date +%Y-%m-%d)
          mkdir -p ${ARTIFACTS}
          cabal install --installdir="${ARTIFACTS}" --install-method=copy -fembed_data_files -fnightly pandoc-cli
          strip "${ARTIFACTS}/pandoc"
          cp COPYRIGHT ${ARTIFACTS}/
          echo "Built from ${GITHUB_SHA}" > ${ARTIFACTS}/README.nightly.txt
    - uses: actions/upload-artifact@v3
      with:
        name: nightly-linux
        path: nightly-linux

  windows:

    runs-on: windows-2019

    steps:
    - uses: actions/checkout@v3
    - name: Install dependencies
      run: |
          cabal update
          cabal build --dependencies-only -fembed_data_files -fnightly pandoc-cli
    - name: Build artifacts
      shell: cmd
      run: |
          for /f %%a in ('powershell -Command "Get-Date -format yyyy-MM-dd"') do set THEDATE=%%a
          set ARTIFACTS=%CD%\nightly-windows\pandoc-nightly-windows-%THEDATE%
          mkdir %ARTIFACTS%
          cabal install --install-method=copy --installdir=%ARTIFACTS% -fnightly -fembed_data_files pandoc-cli
          strip %ARTIFACTS%/pandoc
          copy COPYRIGHT %ARTIFACTS%
          ren %ARTIFACTS%\COPYRIGHT COPYRIGHT.txt
          echo Built from %GITHUB_SHA% > %ARTIFACTS%\README.nightly.txt
    - uses: actions/upload-artifact@v3
      with:
        name: nightly-windows
        path: nightly-windows

  macos:

    runs-on: macos-11

    steps:
    - uses: actions/checkout@v3
    - name: Install dependencies
      run: |
          cabal update
          cabal build --dependencies-only -fnightly -fembed_data_files pandoc-cli
    - name: Build artifacts
      run: |
          export ARTIFACTS=nightly-macos/pandoc-nightly-macos-$(date +%Y-%m-%d)
          mkdir -p ${ARTIFACTS}
          cabal install --install-method=copy --installdir="${ARTIFACTS}" -fnightly -fembed_data_files pandoc-cli
          strip "${ARTIFACTS}/pandoc"
          cp COPYRIGHT ${ARTIFACTS}/
          echo "Built from ${GITHUB_SHA}" > ${ARTIFACTS}/README.nightly.txt
    - uses: actions/upload-artifact@v3
      with:
        name: nightly-macos
        path: nightly-macos
