#!/bin/sh

set -eu

set -x
cd $AUTOPKGTEST_TMP
mkdir config
cd config
chake init
set +x

rm -f nodes.yaml
cat > nodes.d/localhost.yaml <<EOF
local://$(hostname):
  run_list:
    - recipe[basics]
EOF
cat > cookbooks/basics/recipes/default.rb <<EOF
file "/tmp/CREATED_BY_CHAKE" do
  content "HELLO"
end
EOF

rake nodes

RETRYDELAY=10s
ATTEMPTS=10
i=0
rc=0
while true; do
  if rake; then
    break
  fi
  i=$((i+1))
  echo "Attempt ${i}/${ATTEMPTS} failed."
  if [ $i -lt $ATTEMPTS ]; then
    echo "Retrying in ${RETRYDELAY} ..."
    sleep "${RETRYDELAY}"
  else
    exit 1
  fi
done

test -f /tmp/CREATED_BY_CHAKE
