#!/bin/bash
##
#  Korinf project
#
#  Local chroot mount related functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 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 Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##


MOUNT_MODULE_INCLUDED=1

mount_linux()
{
	local MOUNTPARAM

	#   
	BUILDROOT=`mktemp -d $CHROOTDIR/$dist_name-XXXXXX`
	mkdir -p $CHROOTDIR
	$SUDO chown $LOCUSER $CHROOTDIR
	BUILDERHOME=$BUILDROOT-home
	mkdir -p $BUILDERHOME/{,tmp,RPM,RPM/BUILD,RPM/RPMS} || fatal "Can't create $BUILDERHOME"
	chmod g+rwx $BUILDROOT $BUILDERHOME
	# FIXME: why LOCUSER??
	$SUDO chown $LOCUSER $BUILDROOT $BUILDERHOME -R

	[ -z "$BUILDROOT" ] && echo "Skip mount due empty BUILDROOT" && return 1
	echo "Mount $LOCALLINUXFARM/$dist to $BUILDROOT (user $INTUSER)..."

	# FIXME:
	[ -d $LOCALLINUXFARM ] && MOUNTPARAM="--bind" || MOUNTPARAM="-o soft"
	$SUDO mount $LOCALLINUXFARM/$dist $BUILDROOT $MOUNTPARAM || { warning "Cannot mount..." ; return 1 ; }
	$SUDO chmod o+rx $BUILDROOT
	ls -l $BUILDROOT
	test -d $BUILDROOT/etc/ || fatal "Linux system is missed in $BUILDROOT"

	$SUDO su - $LOCUSER -c "mkdir -p $BUILDROOT/{proc,home/$INTUSER,srv/wine}"
	echo Mount proc...
	$SUDO mount -t proc proc $BUILDROOT/proc || { warning "Cannot mount proc" ; return 1 ; }

	echo Mount local home $BUILDERHOME ...
	$SUDO mount $BUILDERHOME $BUILDROOT/home/$INTUSER --bind || { warning "Cannot mount home" ; return 1 ; }

	#echo Mount srv/wine...
	#$SUDO mount /net/wine $BUILD/srv/wine --bind || { warning "Cannot mount home" ; return 0 ; }
}


end_umount()
{
	local i u ERR
	ERR=0
	if [ -z "$BUILDROOT" ] ; then
		[ -z "$ADEBUG" ] || echo "Skip umount due empty BUILDROOT"
		return 1
	fi
	echo "Unmounting and cleaning..."
	for i in /proc/bus/usb /proc/sys/fs/binfmt_misc /proc /home/$INTUSER / ; do
		u=$BUILDROOT$i
		if [ -d $u ] && mount | grep $i >/dev/null ; then
			$SUDO umount -v $u || $SUDO fuser -v $u
			#|| echo "Failed $u umount"
			#$SUDO umount $i || $SUDO umount $i -l || ERR=1
		fi
	done
	[ $ERR = 0 ] && echo "DONE" || echo "Umount FAILED"
	if [ -z "$ADEBUG" ] ; then
		echo "Removing $BUILDROOT $BUILDERHOME ..."
		$SUDO su - $LOCUSER -c "rm -rf $BUILDERHOME" || ERR=1
		$SUDO su - $LOCUSER -c "rmdir -v $BUILDROOT" || ERR=1
		[ $ERR = 0 ] && echo "DONE" || echo "Removing FAILED"
	else
		echo "Skip removing due ADEBUG set"
	fi
	BUILDROOT=""
	return $ERR
}

