#!/bin/bash
#
# Univention Home Mounter
#  unmount the home dirs
#
# SPDX-FileCopyrightText: 2004-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

MOUNTS='/var/lib/univention-home-mounter/mounts'
test -s "$MOUNTS" || exit 0

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "${MOUNTS%/*}" "$0" "$@" || :

mv "$MOUNTS" "$MOUNTS.$$"

now=$(date +%s)
while read line
do
	mp="${line% *}" seconds="${line##* }"
	if [ $((seconds+300)) -lt $now ]
	then
		mountpoint -q "$mp" || continue
		result=`LC_ALL=C umount "$mp" 2>&1` && continue
		echo "$result" | grep -Fq 'not mounted' && continue
	fi
	echo "$line" >>"$MOUNTS"
done <"$MOUNTS.$$"

rm -f "$MOUNTS.$$"
