version: 2
jobs:
  build:
    working_directory: /root/SRB2
    docker:
      - image: debian:stretch
        environment:
          CC: ccache gcc -m32
          PKG_CONFIG_LIBDIR: /usr/lib/i386-linux-gnu/pkgconfig
          LIBGME_CFLAGS: -I/usr/include
          LIBGME_LDFLAGS: -lgme
          CCACHE_COMPRESS: true
          WFLAGS: -Wno-unsuffixed-float-constants
          GCC49: true
      #- image: ubuntu:trusty
      #  environment:
      #    CC: ccache gcc -m32
      #    PKG_CONFIG_LIBDIR: /usr/lib/i386-linux-gnu/pkgconfig
      #    LIBGME_CFLAGS: -I/usr/include
      #    LIBGME_LDFLAGS: -lgme
      #    CCACHE_COMPRESS: true
      #    WFLAGS: -Wno-unsuffixed-float-constants
      #    GCC48: true
    resource_class: large
    steps:
      - run:
          name: Add i386 arch
          command: dpkg --add-architecture i386
      - run:
          name: Add STJr PPA
          command: |
            apt-get -qq update
            apt-get -qq -y install dirmngr
            apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 0B1702D71499D9C25F986507F240F4449D3B0EC6
            echo "deb http://ppa.launchpad.net/stjr/srb2/ubuntu trusty main" >> /etc/apt/sources.list
      - run:
          name: Make APT cache folder
          command: mkdir -p /root/.cache/apt/archives/partial
      - run:
          name: Make APT cache usage by _apt
          command: chown -Rv _apt:root /root/.cache/apt/archives/partial
      - run:
          name: Update APT listing
          command: apt-get -qq update
      - run:
          name: Support S3 upload
          command: apt-get -qq -y install ca-certificates
      - restore_cache:
          keys:
            - v1-SRB2-APT
      - run:
          name: Install SDK
          command: apt-get -o Dir::Cache="/root/.cache/apt" -qq -y --no-install-recommends install git build-essential libpng-dev:i386 libsdl2-mixer-dev:i386 libgme-dev:i386 libcurl4-openssl-dev:i386 libopenmpt-dev:i386 gettext ccache wget gcc-multilib upx openssh-client
      - run:
          name: make md5sum
          command: find /root/.cache/apt/archives -type f -print0 | sort -z | xargs -r0 md5sum > /root/.cache/apt_archives.md5
      - save_cache:
          key: v1-SRB2-APT-{{ checksum "/root/.cache/apt_archives.md5" }}
          paths:
            - /root/.cache/apt
      - checkout
      - run:
          name: Compile without network support
          command: make -C src LINUX=1 ERRORMODE=1 -k NONET=1 -j4
      - run:
          name: wipe build
          command: make -C src LINUX=1 cleandep
      - run:
          name: rebuild depend
          command: make -C src LINUX=1 clean
      - run:
          name: make master depend file
          command: find make/linux/SDL/deps/ -type f -print0 | sort -z | xargs -r0 cat > make/linux/SDL.deps
      - restore_cache:
          keys:
            - v1-SRB2-{{ .Branch }}-{{ checksum "make/linux/SDL.deps" }}
      - run:
          name: Compile
          command: make -C src LINUX=1 ERRORMODE=1 -k -j4
      - store_artifacts:
          path: /root/SRB2/bin/
          destination: bin
      - save_cache:
          key: v1-SRB2-{{ .Branch }}-{{ checksum "make/linux/SDL.deps" }}
          paths:
            - /root/.ccache