#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# make folders
mkdir -p \
    /config/crontabs

## root
# if crontabs do not exist in config
if [[ ! -f /config/crontabs/root ]]; then
    # copy crontab from system
    if crontab -l -u root; then
        crontab -l -u root >/config/crontabs/root
    fi

    # if crontabs still do not exist in config (were not copied from system)
    # copy crontab from included defaults (using -n, do not overwrite an existing file)
    cp -n /etc/crontabs/root /config/crontabs/
fi
sed -i "s|s6-setuidgid abc php[0-9]* -f /config/www/nextcloud/cron.php|s6-setuidgid abc php -f /app/www/public/cron.php|" /config/crontabs/root
# set permissions and import user crontabs
lsiown root:root /config/crontabs/root
crontab -u root /config/crontabs/root
