#!/bin/sh -Cef
# Simple wrapper for rclone polling.
# Set rclone options using the standard RCLONE_ environment variables in addition to the RCLONELOOP_ specific ones.
# Author: Georg Pfuetzenreuter <georg.pfuetzenreuter@suse.com>
#
# Copyright 2026 SUSE LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0

_log() {
	echo "$(date -Iminutes)": "$@"
}

if [ -z "$RCLONELOOP_SLEEP" ] || [ -z "$RCLONELOOP_SRC" ] || [ -z "$RCLONELOOP_DST" ]
then
	_log The variables RCLONELOOP_SLEEP, RCLONELOOP_SRC, RCLONELOOP_DST are mandatory. Aborting.
	exit 1
fi

set -u

RCLONELOOP_CMD="${RCLONELOOP_CMD:-sync}"

while :
do
	_log Starting "$RCLONELOOP_CMD"

	rclone --stats 0 --verbose "$RCLONELOOP_CMD" "$RCLONELOOP_SRC" "$RCLONELOOP_DST" || :

	_log Next "$RCLONELOOP_CMD" in "$RCLONELOOP_SLEEP", sleeping now

	sleep "$RCLONELOOP_SLEEP"
done
