git:
  depth: 9999
language: c
sudo: required

matrix:
  include:

    # JOB 0  --  first because it's the fastest
    - env: STYLE_CHECK_JOB=1

    # JOB 1
    - env: JOB_TYPE=valgrind_health_check
      before_install:
        - chmod ug+x ./travis-scripts/*
        - ./travis-scripts/before_install.sh
      script:
        - sudo ./travis-scripts/valgrind.sh

    # JOB 2
    - env: CF_VERSION=master
      install:
        - sudo apt install -y software-properties-common
        - sudo add-apt-repository -y ppa:deadsnakes/ppa
        - sudo apt update
        - sudo apt -y install python3.6
        - sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
        - sudo curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
        - sudo python3 get-pip.py
        - sudo rm get-pip.py
        - sudo pip3 install cf-remote
        - BRANCH=$CF_VERSION
        - PKG_URL=`cf-remote --version $BRANCH list ubuntu16 agent | tail -n 1`
        - wget $PKG_URL
        - sudo dpkg -i ./$(basename ${PKG_URL})

    # JOB 3
    - env: CF_VERSION=3.15.x
      addons:
        apt:
          sources:
            - sourceline: "deb https://cfengine-package-repos.s3.amazonaws.com/pub/apt/packages stable main"
              key_url: https://cfengine-package-repos.s3.amazonaws.com/pub/gpg.key
          packages:
            - cfengine-community=3.15.*

    # JOB 4
    - env: CF_VERSION=3.12.x
      addons:
        apt:
          sources:
            - sourceline: "deb https://cfengine-package-repos.s3.amazonaws.com/pub/apt/packages stable main"
              key_url: https://cfengine-package-repos.s3.amazonaws.com/pub/gpg.key
          packages:
            - cfengine-community=3.12.*

script:
- sudo apt-get install -y fakeroot
- INSTDIR=$HOME/cf_install
- cd $TRAVIS_BUILD_DIR
- if [ x$STYLE_CHECK_JOB = x1 ];
  then
      sh tests/style_check.sh;
      exit;
  fi
  # Fetch the tags from upstream, even if we are running on a foreign clone;
  # Needed for determine-version.sh to work
- git remote add upstream https://github.com/cfengine/masterfiles.git;
  git fetch upstream 'refs/tags/*:refs/tags/*';
- ./autogen.sh --without-core --prefix=$INSTDIR
- make install
- /var/cfengine/bin/cf-promises --version
- /var/cfengine/bin/cf-promises -c -I -f $INSTDIR/masterfiles/update.cf
- /var/cfengine/bin/cf-promises -c -I -f $INSTDIR/masterfiles/promises.cf
- make dist
- export DIST_TARBALL=`echo cfengine-masterfiles-*.tar.gz`
- make tar-package
- export PKG_TARBALL=`echo cfengine-masterfiles-*.pkg.tar.gz`
- if echo $DIST_TARBALL | grep -E -q '[0-9]{1,2}\.[0-9]{1,2}\.tar\.gz';
  then
      IS_PRERELEASE=false;
  else
      IS_PRERELEASE=true;
  fi;
  export IS_PRERELEASE
- cd $TRAVIS_BUILD_DIR/..
- export RELEASE_TAG=$(/var/cfengine/bin/cf-agent --version | awk '{print $3}')
- export BRANCH="$RELEASE_TAG"
- if [ "$CF_VERSION" = "master" ]; then
    export BRANCH="master";
  fi
- git clone --depth=1 --branch $BRANCH https://github.com/cfengine/core
- chmod -R go-w .
- cp core/tests/acceptance/*.cf.sub masterfiles/tests/acceptance/
- umask 077
- cd masterfiles/tests/acceptance
- echo "CORE=$TRAVIS_BUILD_DIR/../core" > ./testall.env
- DEFRPMVERCMP=/bin/true  ./testall  --printlog  --bindir=/var/cfengine/bin


# We deploy when setting tags only, which basically never happens
# on master branch. Cherry-pick this part and alter it properly into
# the release branches.

deploy:
  skip_cleanup: true
  provider: releases
  # Remember to set it as private variable in Travis-CI settings
  api_key: $GITHUB_RELEASE_TOKEN
# Does not work, I get the error that "true" is not a boolean
#  prerelease: $IS_PRERELEASE
  file:
  - "$DIST_TARBALL"
  - "$PKG_TARBALL"
  on:
    tags: true
    # Avoid upload from all the multiple parallel matrix jobs
    condition: "$CF_VERSION = master"
