#!/bin/bash source $HOME/.profile

ANSI_RED=“033[31;1m” ANSI_GREEN=“033[32;1m” ANSI_RESET=“033[0m” ANSI_CLEAR=“033[0K”

TRAVIS_TEST_RESULT= TRAVIS_CMD=

function travis_cmd() {

local assert output display retry timing cmd result

cmd=$1
TRAVIS_CMD=$cmd
shift

while true; do
  case "$1" in
    --assert)  assert=true; shift ;;
    --echo)    output=true; shift ;;
    --display) display=$2;  shift 2;;
    --retry)   retry=true;  shift ;;
    --timing)  timing=true; shift ;;
    *) break ;;
  esac
done

if [[ -n "$timing" ]]; then
  travis_time_start
fi

if [[ -n "$output" ]]; then
  echo "\$ ${display:-$cmd}"
fi

if [[ -n "$retry" ]]; then
  travis_retry eval "$cmd"
else
  eval "$cmd"
fi
result=$?

if [[ -n "$timing" ]]; then
  travis_time_finish
fi

if [[ -n "$assert" ]]; then
  travis_assert $result
fi

travis_result $result
return $result

}

travis_time_start() {

travis_timer_id=$(printf %08x $(( RANDOM * RANDOM )))
travis_start_time=$(travis_nanoseconds)
echo -en "travis_time:start:$travis_timer_id\r${ANSI_CLEAR}"

}

travis_time_finish() {

local result=$?
travis_end_time=$(travis_nanoseconds)
local duration=$(($travis_end_time-$travis_start_time))
echo -en "travis_time:end:$travis_timer_id:start=$travis_start_time,finish=$travis_end_time,duration=$duration\r${ANSI_CLEAR}"
return $result

}

function travis_nanoseconds() {

local cmd="date"
local format="+%s%N"
local os=$(uname)

if hash gdate > /dev/null 2>&1; then
  cmd="gdate" # use gdate if available
elif [[ "$os" = Darwin ]]; then
  format="+%s000000000" # fallback to second precision on darwin (does not support %N)
fi

$cmd -u $format

}

travis_assert() {

local result=${1:-$?}
if [ $result -ne 0 ]; then
  echo -e "\n${ANSI_RED}The command \"$TRAVIS_CMD\" failed and exited with $result during $TRAVIS_STAGE.${ANSI_RESET}\n\nYour build has been stopped."
  travis_terminate 2
fi

}

travis_result() {

local result=$1
export TRAVIS_TEST_RESULT=$(( ${TRAVIS_TEST_RESULT:-0} | $(($result != 0)) ))
if [ $result -eq 0 ]; then
  echo -e "\n${ANSI_GREEN}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
else
  echo -e "\n${ANSI_RED}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
fi

}

travis_terminate() {

pkill -9 -P $$ &> /dev/null || true
exit $1

}

travis_wait() {

local timeout=$1

if [[ $timeout =~ ^[0-9]+$ ]]; then
  # looks like an integer, so we assume it's a timeout
  shift
else
  # default value
  timeout=20
fi

local cmd="$@"
local log_file=travis_wait_$$.log

$cmd &>$log_file &
local cmd_pid=$!

travis_jigger $! $timeout $cmd &
local jigger_pid=$!
local result

{
  wait $cmd_pid 2>/dev/null
  result=$?
  ps -p$jigger_pid &>/dev/null && kill $jigger_pid
} || return 1

if [ $result -eq 0 ]; then
  echo -e "\n${ANSI_GREEN}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
else
  echo -e "\n${ANSI_RED}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
fi

echo -e "\n${ANSI_GREEN}Log:${ANSI_RESET}\n"
cat $log_file

return $result

}

travis_jigger() {

# helper method for travis_wait()
local cmd_pid=$1
shift
local timeout=$1 # in minutes
shift
local count=0

# clear the line
echo -e "\n"

while [ $count -lt $timeout ]; do
  count=$(($count + 1))
  echo -ne "Still running $count of $timeout: $@\r"
  sleep 60
done

echo -e "\n${ANSI_RED}Timeout ${timeout} minutes reached. Terminating \"$@\"${ANSI_RESET}\n"
kill -9 $cmd_pid

}

travis_retry() {

local result=0
local count=1
while [ $count -le 3 ]; do
  [ $result -ne 0 ] && {
    echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" >&2
  }
  "$@"
  result=$?
  [ $result -eq 0 ] && break
  count=$(($count + 1))
  sleep 1
done

[ $count -gt 3 ] && {
  echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" >&2
}

return $result

}

travis_fold() {

local action=$1
local name=$2
echo -en "travis_fold:${action}:${name}\r${ANSI_CLEAR}"

}

decrypt() {

echo $1 | base64 -d | openssl rsautl -decrypt -inkey ~/.ssh/id_rsa.repo

}

mkdir -p <%= BUILD_DIR %> cd <%= @working_dir %>