# Configuration for Travis CI
language: rust
sudo: required
services:
  - docker
addons:
  apt:
    packages:
      - libssl-dev

stages:
  - build
  - test
  - name: release
    if: tag =~ ^v(\d+\.)*\d+$

jobs:
  include:
    ################################
    # Build stage                  #
    ################################

    # Linux with Rust stable/beta/nightly
    - stage: build
      rust: stable
      env: TARGET=x86_64-unknown-linux-gnu
      cache: cargo
      script: &build-script
        - cargo build --verbose --all
        - cargo build --no-default-features --verbose --all
        - cargo build --features no-color --verbose --all
    - stage: build
      rust: beta
      env: TARGET=x86_64-unknown-linux-gnu
      cache: cargo
      script: *build-script
    - stage: build
      rust: nightly
      env: TARGET=x86_64-unknown-linux-gnu
      cache: cargo
      script: *build-script

    # macOS with Rust stable
    - stage: build
      rust: stable
      os: osx
      env: TARGET=x86_64-apple-darwin
      cache: cargo
      script: *build-script

    ################################
    # Test stage                   #
    ################################

    - stage: test
      env: TARGET=x86_64-unknown-linux-gnu
      cache: cargo
      script: cargo test --verbose --all

    ################################
    # Release stage                #
    ################################

    # Cargo crate release
    - stage: release
      env: TARGET=x86_64-unknown-linux-gnu
      cache: cargo
      script:
        - echo "Creating release crate on crates.io..."
        - echo $CARGO_TOKEN | cargo login
        - cargo publish --verbose

    # GitHub binary release for Linux on x86/x86_64
    - stage: release
      rust: stable
      env: TARGET=x86_64-unknown-linux-gnu TARGET_SIMPLE=linux-x64 DEB_ARCH=amd64
      cache: cargo
      script: &script-github-release
        - |
          if [ $TARGET == "x86_64-unknown-linux-gnu" ] || [ $TARGET == "x86_64-apple-darwin" ]; then
            echo "Creating release binary on GitHub for $TARGET..."
            cargo build --release --verbose --all
            cp target/release/ffsend ./ffsend
          else
            echo "Creating release binary on GitHub for $TARGET (cross compiled)..."
            cross build --target $TARGET --release --verbose --all
            cp target/$TARGET/release/ffsend ./ffsend
          fi
        - tar -czvf ./ffsend-$TARGET_SIMPLE-$TRAVIS_TAG.tar.gz ffsend
        - |
          if [ -n "$DEB_ARCH" ]; then
            ./pkg/create_deb
          fi
        - mv ./ffsend ./ffsend-$TARGET_SIMPLE-$TRAVIS_TAG
      deploy: &deploy-github-release
        provider: releases
        api_key: $GITHUB_OAUTH_TOKEN
        skip_cleanup: true
        overwrite: true
        file_glob: true
        file:
          - ffsend_*.deb
          - ffsend-$TARGET_SIMPLE-$TRAVIS_TAG.tar.gz
          - ffsend-$TARGET_SIMPLE-$TRAVIS_TAG
        on:
          tags: true
          branch: master
    - stage: release
      rust: stable
      env: TARGET=i686-unknown-linux-gnu TARGET_SIMPLE=linux-i686 DEB_ARCH=i386
      cache: cargo
      install: &install-github-release-cross
        - cargo install cross
      script: *script-github-release
      deploy: *deploy-github-release

    # GitHub binary release for Linux on arch
    - stage: release
      rust: stable
      env: TARGET=aarch64-unknown-linux-gnu TARGET_SIMPLE=linux-aarch64
      cache: cargo
      install: *install-github-release-cross
      script: *script-github-release
      deploy: *deploy-github-release
    - stage: release
      rust: stable
      env: TARGET=arm-unknown-linux-gnueabi TARGET_SIMPLE=linux-arm
      cache: cargo
      install: *install-github-release-cross
      script: *script-github-release
      deploy: *deploy-github-release
    - stage: release
      rust: stable
      env: TARGET=armv7-unknown-linux-gnueabihf TARGET_SIMPLE=linux-armv7
      cache: cargo
      install: *install-github-release-cross
      script: *script-github-release
      deploy: *deploy-github-release

    # GitHub binary release for macOX
    - stage: release
      rust: stable
      os: osx
      env: TARGET=x86_64-apple-darwin TARGET_SIMPLE=osx-x64
      cache: cargo
      script: *script-github-release
      deploy: *deploy-github-release

# TODO: add Windows architecture (using AppVeyor)
# TODO: add (Free)BSD architecture
# TODO: enfore the git tag/crate version equality for releases
# TODO: disable addons/rust installation for GitHub release job
