<%
    import random

    with open("/conf/base/etc/crontab.template") as f:
        system_crontab = f.read()

    system_advanced = middleware.call_sync("system.advanced.config")
    resilver = middleware.call_sync("pool.resilver.config")
    legacy_replication = middleware.call_sync("replication.query", [["transport", "=", "LEGACY"], ["enabled", "=", True]])
%>\
${system_crontab}

45 3 * * * root /usr/local/bin/midclt call pool.scrub.run freenas-boot ${system_advanced['boot_scrub']} > /dev/null 2>&1

## Don't run these on TrueNAS HA passive controllers
## Redmine 55908
% if middleware.call_sync("system.is_freenas") or middleware.call_sync("failover.status") != "BACKUP":
    % for job in middleware.call_sync("cronjob.query", [["enabled", "=", True]]):
${' '.join(middleware.call_sync('cronjob.construct_cron_command', job["schedule"], "root", f"midclt call cronjob.run {job['id']}"))}
    % endfor

    % for job in middleware.call_sync("rsynctask.query", [["enabled", "=", True]]):
${' '.join(middleware.call_sync('cronjob.construct_cron_command', job["schedule"], "root", f"midclt call rsynctask.run {job['id']}"))}
    % endfor

    % for job in middleware.call_sync("cloudsync.query", [["enabled", "=", True]]):
${' '.join(middleware.call_sync('cronjob.construct_cron_command', job["schedule"], "root", f"midclt call cloudsync.sync {job['id']}"))}
    % endfor

    % for job in middleware.call_sync("pool.scrub.query", [["enabled", "=", True]]):
${' '.join(middleware.call_sync('cronjob.construct_cron_command', job["schedule"], "root", f"midclt call pool.scrub.run {job['pool_name']} {job['threshold']}"))}
    % endfor

    % if resilver["enabled"]:
        ${resilver["begin"].split(":")[1]} ${resilver["begin"].split(":")[0]} * * * root \
        /usr/local/bin/midclt call pool.configure_resilver_priority > /dev/null 2>&1

        ${resilver["end"].split(":")[1]} ${resilver["end"].split(":")[0]} * * * root \
        /usr/local/bin/midclt call pool.configure_resilver_priority > /dev/null 2>&1
    % endif

    % if legacy_replication:
* * * * * root /usr/local/bin/python /usr/local/www/freenasUI/tools/autosnap.py > /dev/null 2>&1
    % else:
15 4 * * * root /usr/local/bin/python /usr/local/www/freenasUI/tools/autosnap.py > /dev/null 2>&1
    % endif

    % if middleware.call_sync("datastore.query", "system.update", [["upd_autocheck", "=", True]]):
${random.randint(0, 59)} \
${random.randint(1, 4)} \
* * * root /usr/local/bin/midclt call update.download > /dev/null 2>&1
    % endif

@weekly root /usr/local/sbin/update-smart-drivedb > /dev/null 2>&1
% endif
