#!/bin/bash

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

#
# Schedule automatic updates
#

action=${1:-"check"}
timestamp=$2
cmd="sleep \$(( RANDOM % 18000 )); /usr/sbin/update-packages"

if [ "$action" == "add" ]; then
    crontab -l | grep -q "$cmd" || echo "5 2 * * * $cmd" >> /etc/crontabs/root
elif [ "$action" == "remove" ]; then
    crontab -l | grep -v "$cmd" | sort | uniq | crontab -
else
    crontab -l | grep -q "$cmd"
fi
