#!/bin/sh
set -e

# Copyright 2009 Holger Levsen (holger@layer-acht.org)
# Copyright © 2012-2013 Andreas Beckmann (anbe@debian.org)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>

#
# check for stale mounts and chroots in $PIUPARTS_TMPDIR
#


. /usr/share/piuparts/lib/read_config.sh

get_config_value PIUPARTS_TMPDIR global tmpdir


MOUNTS="$(mktemp)"
cp /proc/mounts "$MOUNTS"

if [ "$(grep -v trash "$MOUNTS" | grep -c "$PIUPARTS_TMPDIR")" -gt 1 ] ; then
	echo "More than one mountpoint below $PIUPARTS_TMPDIR detected!"
	echo
	grep "$PIUPARTS_TMPDIR" "$MOUNTS"
	echo
	echo "Zero or one mountpoint is normal for piuparts operation, more is not."
	echo "Please investigate and cleanup."
	echo
fi
rm "$MOUNTS"

LS_TMP=$(ls --color=never -l "$PIUPARTS_TMPDIR")
if [ "$(echo "$LS_TMP" | wc -l)" -gt 12 ] ; then
	echo "More than ten directories in $PIUPARTS_TMPDIR detected!"
	echo
	echo "$LS_TMP"
	echo
	echo "$(du -shx "$PIUPARTS_TMPDIR" 2>/dev/null)"
	echo
	echo "One is normal for piuparts operation, more is not."
	echo "Please investigate and cleanup."
	echo
fi
