#!/bin/bash
# barium helper scripts
# author: rosalinux.ru: betcher_
if [ -f $(dirname $0)/lib ] ;  then
    . $(dirname $0)/lib
else 
    . $(which lib) || exit 1
fi
HLP (){
	echo $(basename $0) - util to remove module from root aufs
	echo "Usage: 
	barium $(basename $0) module_name
or
	barium $(basename $0) layer_number
see barium ls to get number of layer"
	exit  
}

[ "$(check_union)" != 'aufs' ] && exitmsg "Supported only aufs based rootfs" ${LINENO}
[ "$1" ] || exitmsg 'Need a module name as $1, or layer nuber (see barium ls)' ${LINENO}
[ "$1" == '-h' -o "$1" == '--help' ] && HLP
if echo $1 |grep -E -q '^[[:digit:]]{1,3}$' ; then 
	mpoint="$(ls_aufs |sed -n $(($1 + 1))p)"
else
	mpoint="${SYSMNT}/bundles/$1"
fi	
if [ -d $mpoint ] ; then
	mount -t aufs -o remount,del:"${mpoint}"/ aufs / || exitmsg "Can't remove layer from the aufs, see lsof" ${LINENO}
	update_caches $mpoint
	umount $mpoint 
	rmdir $mpoint || exitmsg "The module disabled, but the bundle still exists" ${LINENO}
else
	exitmsg "Can't find $1 in $SYSMNT/bundles" ${LINENO}
fi

