#!/bin/bash
set -e

function verbose_run()
{
  if $VERBOSE; then
    echo "$ $@"
  fi
  "$@"
}

verbose_run /system/internal/inituidgid

if [[ ! -e /cache ]]; then
  verbose_run mkdir /cache
  verbose_run chown app: /cache
fi

verbose_run /system/internal/setuser app \
  mkdir -p "/output/log" "/cache/mock_cache" "/cache/mock_lib"
verbose_run /system/internal/setuser app rm -rf /output/log/*
if $CLEAN; then
  verbose_run /system/internal/setuser app rm -rf /output/*-*
fi

verbose_run rm -rf /var/lib/mock
verbose_run ln -s /cache/mock_lib /var/lib/mock
verbose_run rm -rf /var/cache/mock
verbose_run ln -s /cache/mock_cache /var/cache/mock
verbose_run chown root:mock /cache/mock_cache
verbose_run chown root:mock /cache/mock_lib

export BUNDLE_GEMFILE=/home/app/Gemfile

if [[ "$TASK" != -T ]]; then
  TASK="$TASK finish"
fi

verbose_run exec /system/internal/my_init --skip-runit --skip-startup-files --quiet -- \
  /system/internal/setuser app \
  bundle exec drake -j$CONCURRENCY -f /system/internal/build_tasks.rb \
  $TASK --trace
