#!/bin/sh

test -x /usr/local/bin/cscli || exit 0

# splay hub upgrade and crowdsec reload
sleep "$(jot -r 1 1 300)"

# favor the opnsense plugin's cron if it's there
test -e /usr/local/etc/cron.d/oscrowdsec.cron && exit 0

/usr/local/bin/cscli --error -o human hub update

upgraded=$(/usr/local/bin/cscli --error -o human hub upgrade)
if [ -n "$upgraded" ]; then
    service crowdsec onestatus && service crowdsec onereload
fi

exit 0
