#!/usr/bin/env bash

set -euo pipefail

health_check_res=0
heath_metrics_path="${MERGE_CONFLICT_CHECK_DAEMON__LOCAL_REPO_CLIENT__HEALTH_METRICS_PATH:-/tmp/health_metrics.json}"

if [ ! -f $heath_metrics_path ]; then
    jq -n '{fetch_failure_count: 0}' >$heath_metrics_path
fi

fetch_failure_count=$(cat $heath_metrics_path | jq '.fetch_failure_count // 0')
[ "$fetch_failure_count" -gt 0 ] && health_check_res=1

jq -n '{fetch_failure_count: 0}' >$heath_metrics_path

exit $health_check_res
