#! /bin/sh

BASEMNTDIR=$HOME/mnt
USEROPT=`cat ~/personal/.credentials|grep 'username='`
[ -n "$USEROPT" ] || USEROPT="username=Sviataslau_Svirydau"
PSWOPT=`cat ~/personal/.credentials|grep 'password='`
[ -z "$PSWOPT" ] || PSWOPT=",$PSWOPT"

find_hosts()
{
	find $BASEMNTDIR -type d -maxdepth 1|grep -v 'mnt$'|sed -e 's:.*/::'|grep -i ^ep
}

mount_host_shares()
{
	local host="$1"
	#while
	local share=""
	find $BASEMNTDIR/$host -type d -maxdepth 1|grep -v "$host\$"|sed -e 's:.*/::'| \
		while read share;do
		#echo "found: //$host/$share"
		local m=`mount|grep "$host/$share"`
		if [ -z "$m" ]; then
			echo "Mounting //$host/$share"
			mount.cifs "//$host/$share" "$BASEMNTDIR/$host/$share" -o "domain=${DOMAIN-MINSK},$USEROPT$PSWOPT"
		fi
	done
}

find_hosts|while read h; do
	#echo $host
	mount_host_shares $h
done

