config_steps: &config_steps
  working_directory: ~/repo
  steps:
    - run:
        name: Install Build Tools
        command: |
          if [[ $DISTRO == "ALPINE" ]]; then
            apk add --update --no-cache make gcc g++ python git openssh
          fi

          if [[ $DISTRO == "CENTOS" ]]; then
            curl --silent --location "https://rpm.nodesource.com/setup_${NODEJS}.x" | bash -
            yum -y install centos-release-scl-rh epel-release https://centos6.iuscommunity.org/ius-release.rpm
            yum -y install devtoolset-3-gcc devtoolset-3-gcc-c++ git2u make
            yum -y install nodejs
            echo 'export PATH=/opt/rh/devtoolset-3/root/usr/bin/:$PATH' >> $BASH_ENV
          fi

    - checkout

    - run:
        name: Checkout code submodules
        command: git submodule update --init --recursive --remote

    - run:
        name: 'Configure Environment'
        command: |
          echo 'export JOBS=max' >> $BASH_ENV
          echo 'export PUBLISH=false' >> $BASH_ENV
          if [[ $CIRCLE_TAG == `git describe --tags --always HEAD` ]]; then
            echo 'export PUBLISH="true"' >> $BASH_ENV
          fi

    - run: npm install -g node-gyp
    - run: npm install --build-from-source --unsafe-perm
    - run: npm test
    - run: npm run prebuild-ci

    - run:
        name: Publish Binaries
        command: |
          source $BASH_ENV
          echo -e 'Publish? ->' $PUBLISH
          if [[ $PUBLISH == "true" ]]; then
            npm run prebuild-upload
          fi
          true

    - store_artifacts:
        path: ~/repo/prebuilds

version: 2

jobs:
  "node-4":
    <<: *config_steps
    docker:
      - image: centos:6.6
        environment:
          DISTRO: CENTOS
          NODEJS: 4

  "node-6":
    <<: *config_steps
    docker:
      - image: centos:6.6
        environment:
          DISTRO: CENTOS
          NODEJS: 6

  "node-8":
    <<: *config_steps
    docker:
      - image: centos:6.6
        environment:
          DISTRO: CENTOS
          NODEJS: 8

  "node-9":
    <<: *config_steps
    docker:
      - image: centos:6.6
        environment:
          DISTRO: CENTOS
          NODEJS: 9

  "node-4 musl":
    <<: *config_steps
    docker:
      - image: mhart/alpine-node:4
        environment:
          DISTRO: ALPINE
          NODEJS: 4

  "node-6 musl":
    <<: *config_steps
    docker:
      - image: mhart/alpine-node:6
        environment:
          DISTRO: ALPINE
          NODEJS: 6

  "node-8 musl":
    <<: *config_steps
    docker:
      - image: mhart/alpine-node:8
        environment:
          DISTRO: ALPINE
          NODEJS: 8

  "node-9 musl":
    <<: *config_steps
    docker:
      - image: mhart/alpine-node:9
        environment:
          DISTRO: ALPINE
          NODEJS: 9

build_releases: &build_releases
  filters:
    tags:
      only: /v[0-9]+(\.[0-9]+)*(\-\w+)?/

workflows:
  version: 2
  build:
    jobs:
      - "node-4":
          <<: *build_releases
      - "node-4 musl":
          <<: *build_releases
      - "node-6":
          <<: *build_releases
      - "node-6 musl":
          <<: *build_releases
      - "node-8":
          <<: *build_releases
      - "node-8 musl":
          <<: *build_releases
      - "node-9":
          <<: *build_releases
      - "node-9 musl":
          <<: *build_releases
