[Unit]
Description=rclone: Remote FUSE filesystem for cloud storage config %i
Documentation=man:rclone(1)

[Service]
Type=notify
Environment="EXPRESSION=%i"
ExecStart= \
    bash -c ' \
        args=; \
        target="$(echo "$EXPRESSION" | cut -d":" -f1)"; \
        dirname="$(echo "$EXPRESSION" | cut -d":" -f2)"; \
        cacheDuration="$(echo "$EXPRESSION" | cut -d":" -f3)"; \
        \
        if [ ! -z "$cacheDuration" ]; \
        then \
            args="--vfs-cache-max-age=$cacheDuration"; \
        fi; \
        \
        destination="`cd "%h/.mnt"; realpath -m "$dirname"`"; \
        mkdir -p "$destination"; \
        mkdir -p /tmp/rclone; \
        /usr/bin/rclone mount \
            --config="%h/.config/rclone/rclone.conf" \
            --vfs-cache-mode full \
            --log-file /tmp/rclone/$target.log \
            $args \
            $target: "$destination";'
ExecStop=bash -c '/bin/fusermount -zu "`cd "%h/.mnt"; realpath "$(echo "$EXPRESSION" | cut -d":" -f2)"`"'

Restart=always
RestartSec=10

[Install]
WantedBy=rclone.target