#!/bin/sh
# ollie-watchdog — ensure ollie FUSE mount stays healthy.
# Handles initial mount and recovery from stale mounts.
set -e

OLLIE="${OLLIE:-$HOME/mnt/ollie}"
INTERVAL="${OLLIE_WATCHDOG_INTERVAL:-10}"
NAMESPACE="${NAMESPACE:-/tmp/ns.$(id -un).$(echo "${DISPLAY:-:0}" | tr / _)}"
SOCK="$NAMESPACE/ollie"

while true; do
	if [ -S "$SOCK" ] && ! cat "$OLLIE/help" >/dev/null 2>&1; then
		logger -t ollie-watchdog "mount unhealthy, remounting"
		ollie-remount 2>/dev/null || true
	fi
	sleep "$INTERVAL"
done
