#!/bin/sh

set -e

if test "$(dpkg-query --show --showformat '${db:Status-Status}' debci-worker 2>/dev/null)" != installed; then
  exit
fi

if ! which debci >/dev/null 2>&1; then
  exit
fi

log=$(mktemp)
cleanup() {
  rm -f "$log"
}
trap cleanup INT TERM EXIT

# only output anything if `debci setup` fails
rc=0
cd ~debci
sudo -u debci debci update-worker >"$log" 2>&1 || rc=$?
if [ $rc -ne 0 ]; then
  cat "$log"
fi
exit $rc
