on:
  repository_dispatch:
    types: manual-nightly
  schedule:
    - cron: '0 0 * * *'

name: Nightly Release

jobs:
  "Generate_Source":
    runs-on: ubuntu-latest

    steps:
      - name: Update APT Cache
        run: |
          sudo apt-get update

      - name: Install Build Dependencies
        run: |
          sudo apt-get install \
            automake \
            autopoint \
            autoconf-archive \
            build-essential \
            binutils-dev \
            gettext \
            intltool \
            libsdl2-dev \
            libsdl2-mixer-dev \
            libsdl2-image-dev \
            libgl1-mesa-dev \
            libxml2-dev \
            libfreetype6-dev \
            libpng-dev \
            libopenal-dev \
            libvorbis-dev \
            libiberty-dev

      - name: Checkout Naev Repository
        uses: actions/checkout@v2
        with:
          submodules: true

      - name: Generate source archive
        run: |
          ./autogen.sh
          ./configure
          make dist-gzip
          make dat/VERSION

      - name: Collect Artifacts
        run: |
          mkdir -p dist/out
          mv naev-*.tar.gz dist/out/source.tar.gz
          cp dat/VERSION dist/out

      - name: Upload Source Artifact
        uses: actions/upload-artifact@v2
        with:
          name: naev-dist
          path: ${{ github.workspace }}/dist/out/source.tar.gz
          if-no-files-found: error

      - name: Upload Version Artifact
        uses: actions/upload-artifact@v2
        with:
          name: naev-version
          path: ${{ github.workspace }}/dist/out/VERSION
          if-no-files-found: error

      - name: Upload Deployment Script Artifact
        uses: actions/upload-artifact@v2
        with:
          name: naev-steam-deployment
          path: ${{ github.workspace }}/utils/ci/steam/*
          if-no-files-found: error

  "Build_Naev_Release":
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-16.04
            buildoutput: naev-linux-x86-64
            shell: bash
            make: make
          - os: windows-latest
            buildoutput: naev-win64
            shell: msys2 {0}
            make: mingw32-make
    
    defaults:
      run:
        shell: ${{ matrix.shell }}
            
    runs-on: ${{ matrix.os }}

    steps:
      - name: Update APT Cache
        if: ${{ runner.os == 'Linux'}}
        run: |
          sudo apt-get update

      - name: Install Build Dependencies
        if: ${{ runner.os == 'Linux'}}
        run: |
          sudo apt-get install \
            automake \
            autopoint \
            autoconf-archive \
            build-essential \
            binutils-dev \
            gettext \
            intltool \
            libsdl2-dev \
            libsdl2-mixer-dev \
            libsdl2-image-dev \
            libgl1-mesa-dev \
            libxml2-dev \
            libfreetype6-dev \
            libpng-dev \
            libopenal-dev \
            libvorbis-dev \
            libiberty-dev \
            libluajit-5.1-dev \
            libsuitesparse-dev
            
      - name: Install MinGW Packages
        if: ${{ runner.os == 'Windows'}}
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          update: true
          install: mingw-w64-x86_64-libtool mingw-w64-x86_64-toolchain mingw-w64-x86_64-clang mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_image mingw-w64-x86_64-libxml2 mingw-w64-x86_64-libpng mingw-w64-x86_64-openal mingw-w64-x86_64-libvorbis mingw-w64-x86_64-binutils mingw-w64-x86_64-itstool mingw-w64-x86_64-freetype mingw-w64-x86_64-gettext mingw-w64-x86_64-python-pip mingw-w64-x86_64-luajit mingw-w64-x86_64-nsis mingw-w64-x86_64-make mingw-w64-x86_64-pkg-config mingw-w64-x86_64-suitesparse autoconf autoconf-archive automake-wrapper git intltool python tar

      - name: Checkout Naev Repository
        uses: actions/checkout@v2
        with:
          submodules: true
          
      - name: Build Naev for Win64
        if: ${{ runner.os == 'Windows'}}
        run: |
          ./autogen.sh
          ./configure --disable-debug
          $MAKE -j"$(nproc --all)"
        env:
          CFLAGS: "-O3"
          MAKE: "${{ matrix.make }}"
          
      - name: Build and Package Naev for Linux64
        if: ${{ runner.os == 'Linux'}}
        run: |
          ./utils/buildAppImage.sh -n -s "$(pwd)" -b "$(pwd)" -o "$(pwd)/dist"
        env:
          CFLAGS: "-O3"
          
      - name: Build Windows Installer
        if: ${{ runner.os == 'Windows'}}
        run: |
          ./extras/windows/packageWindows.sh -n -s "$(pwd)" -b "$(pwd)" -o "$(pwd)/dist"
          
      - name: Upload Artifacts
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.buildoutput }}
          path: |
            ${{ github.workspace }}/dist/out/*
          if-no-files-found: error
            
  "Build_Naev_Steam_Release":
    runs-on: ubuntu-latest

    container:
      image: dosowisko/steam-runtime-amd64
      volumes:
        - ${{ github.workspace }}:${{ github.workspace }}

    steps:
      - name: Clone Naev Repository
        run: |
          git clone https://github.com/naev/naev.git ${{ github.workspace }}

      - name: Install Additional Build Dependencies
        run: |
          sudo apt-get -y install autoconf-archive intltool

      - name: Build Naev for Steam on Linux64
        run: |
          cd ${{ github.workspace }}
          ./autogen.sh
          ./configure --disable-debug --build=x86_64-linux-gnu
          make -j$(nproc --all)
          make data
        env:
          CFLAGS: "-O3 -std=gnu11"

      - name: Collect Steam Linux64 Artifacts
        run: |
          mkdir -p ${{ github.workspace }}/dist/out/

          mv ${{ github.workspace }}/src/naev ${{ github.workspace }}/dist/out/naev.x64
          chmod +x ${{ github.workspace }}/dist/out/naev.x64

          tar -cJvf ${{ github.workspace }}/dist/out/steam-ndata.tar.xz dat
      
      - name: Upload Naev Binary Artifact
        uses: actions/upload-artifact@v1
        with:
          name: naev-steamruntime
          path: ${{ github.workspace }}/dist/out/naev.x64

      - name: Upload Naev Data Artifact
        uses: actions/upload-artifact@v1
        with:
          name: naev-ndata
          path: ${{ github.workspace }}/dist/out/steam-ndata.tar.xz

  "Upload_Naev_Release":
    strategy:
      fail-fast: true
      matrix:
        include:
          - releasetype: github
          - releasetype: steam
          - releasetype: itch
            
    runs-on: ubuntu-latest
    needs: [Generate_Source, Build_Naev_Release, Build_Naev_Steam_Release]
    if: ${{ github.repository == 'naev/naev' }}

    steps:
      - name: Create Release Staging and Output Areas
        run: |
          mkdir -p {temp,dist/{staging,out/{lin64,win64}}}
          
      - name: Update APT Cache
        if: ${{ matrix.releasetype == 'steam' }}
        run: |
          sudo add-apt-repository multiverse
          sudo dpkg --add-architecture i386
          sudo apt-get update

      - name: Install SteamCMD
        if: ${{ matrix.releasetype == 'steam' }}
        run: |
          echo steamcmd steam/question select "I AGREE" | sudo debconf-set-selections
          sudo apt-get install lib32gcc1 libsdl2-2.0-0:i386 steamcmd
          
      - name: Install Butler
        if: ${{ matrix.releasetype == 'itch' }}
        run: |
          wget -O temp/butler.zip https://broth.itch.ovh/butler/linux-amd64/LATEST/archive/default
          unzip temp/butler.zip
          chmod +x butler
          ./butler -V

      - name: Download Artifacts
        uses: actions/download-artifact@v2
        with:
          path: ${{ github.workspace }}/dist/staging

      - name: Build Github Release
        if: ${{ matrix.releasetype == 'github' }}
        run: |
          BUILD_DATE="$(date +%Y%m%d)"
          VERSION="$(<"dist/staging/naev-version/VERSION")"
          SUFFIX="$VERSION.$BUILD_DATE"

          mv dist/staging/naev-linux-x86-64/*.AppImage dist/out/naev-$SUFFIX-linux-x86-64.AppImage
          mv dist/staging/naev-win64/naev*.exe dist/out/naev-$SUFFIX-win64.exe
          mv dist/staging/naev-dist/source.tar.gz dist/out/naev-$SUFFIX-source.tar.gz

          chmod +x dist/out/naev-$SUFFIX-linux-x86-64.AppImage

      - name: Upload Github Release
        if: ${{ matrix.releasetype == 'github' }}
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: "nightly"
          prerelease: true
          title: "Nightly Build"
          files: |
            ${{ github.workspace }}/dist/out/*
          
      - name: Build and Upload itch.io Release
        if: ${{ matrix.releasetype == 'itch' }}
        run: |
          BUILD_DATE="$(date +%Y%m%d)"
          VERSION="$(<"dist/staging/naev-version/VERSION")"
          SUFFIX="$VERSION.$BUILD_DATE"

          mv dist/staging/naev-linux-x86-64/*.AppImage dist/out/lin64/naev-$SUFFIX-linux-x86-64.AppImage
          chmod +x dist/out/lin64/naev-$SUFFIX-linux-x86-64.AppImage

          tar -Jxf "dist/staging/naev-win64/steam-win64.tar.xz" -C "dist/out/win64"
          tar -Jxf "dist/staging/naev-ndata/steam-ndata.tar.xz" -C "dist/out/win64"

          ./butler push --userversion="$SUFFIX" dist/out/lin64 naev/naev:linux-x86-64-nightly
          ./butler push --userversion="$SUFFIX" dist/out/win64 naev/naev:windows-x86-64-nightly
        env:
          BUTLER_API_KEY: ${{ secrets.BUTLER_API_KEY }}

      - name: Build and Upload Steam Release
        if: ${{ matrix.releasetype == 'steam' }}
        run: |
          chmod -R +x dist/staging/naev-steam-deployment
          cp -r dist/staging/naev-steam-deployment/* ${{ github.workspace }}
          ./SteamDeploy.sh -n -v "${{ github.workspace }}/dist/staging/naev-version" -t "${{ github.workspace }}/dist/staging" -o "${{ github.workspace }}/dist/out"
        env:
          STEAMCMD_USER: ${{ secrets.STEAMCMD_USER }}
          STEAMCMD_PASS: ${{ secrets.STEAMCMD_PASS }}
          TFA_IMAP: ${{ secrets.TFA_IMAP }}
          TFA_PASS: ${{ secrets.TFA_PASS }}
          TFA_USER: ${{ secrets.TFA_USER }}
