#!/usr/bin/env bash
# originally taken from https://github.com/symfony/symfony/blob/master/.travis.yml
# Copyright (c) 2017 Fabien Potencier

nanoseconds() {
    local cmd="date"
    local format="+%s%N"
    local os=$(uname)
    if hash gdate > /dev/null 2>&1; then
      cmd="gdate"
    elif [[ "$os" = Darwin ]]; then
      format="+%s000000000"
    fi
    $cmd -u $format
}
export -f nanoseconds
# tfold is a helper to create folded reports
tfold () {
    local title=$1
    local fold=$(echo $title | sed -r 's/[^-_A-Za-z0-9]+/./g')
    shift
    local id=$(printf %08x $(( RANDOM * RANDOM )))
    local start=$(nanoseconds)
    echo -e "travis_fold:start:$fold"
    echo -e "travis_time:start:$id"
    echo -e "\\e[1;34m$title\\e[0m"
    bash -xc "$*" 2>&1
    local ok=$?
    local end=$(nanoseconds)
    echo -e "\\ntravis_time:end:$id:start=$start,finish=$end,duration=$(($end-$start))"
    (exit $ok) &&
        echo -e "\\e[32mOK\\e[0m $title\\n\\ntravis_fold:end:$fold" ||
        echo -e "\\e[41mKO\\e[0m $title\\n"
    (exit $ok)
}
export -f tfold
