#!/sbin/runscript
# Copyright 2008-2012 Calculate Ltd. http://www.calculate-linux.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

depend() {
	use localmount
	after bootmisc consolefont modules netmount
	after readahead-list ypbind autofs openvpn gpm lircmd
	after cupsd fbcondecor sshd cron wicd
	before xdm
	after acpid consolekit hald xfs
}

variable_value()
{
	local varname=$1
	cl-core-variables-show --only-value client.$varname
}

start() {
	# Mount remote Samba filesystems.
	# Identifing domain server by cl-client variables
	local SERVER=`variable_value cl_remote_host`
	local SERVERLIVE=`variable_value cl_remote_host_live`
	local SERVERPW=`variable_value cl_remote_pw`
	[[ -z $SERVERLIVE ]] && SERVERLIVE=$SERVER

	if [[ -n $SERVERLIVE ]]
	then 
		ebegin "Mounting domain resources"

		local NET_CONF=`cl-core-variables-show --only-value install.os_install_net_conf`
		if [[ $NET_CONF = "networkmanager" ]]
		then
		        for COUNT in $( seq 0 24 )
			do
        			NET_STATE=`qdbus --system org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.state`
				if [[ $NET_STATE = 40 ]]
				then
                                    ping -w4 -i0.5 -c3 $SERVERLIVE &>/dev/null && break || sleep 0.5
				else
				    break
				fi
			done
		else
			for COUNT in $( seq 0 24 )
			do
				ping -w4 -i0.5 -c3 $SERVERLIVE &>/dev/null && break || sleep 0.5
			done
		fi
		if [[ -z $SERVER ]]
		then
			cl-client $SERVERLIVE && cl-setup-system -l remote &>/dev/null
		else
			cl-client --mount
		fi
		res=$?
		eend $res "Some samba remote resources to mount"
	else
		true
	fi

}

stop(){
	ebegin "Unmount domain resources"
	[[ $( mount | grep ' /var/calculate/remote ' ) ]] &&
		umount /var/calculate/remote
	[[ $( mount | grep '/var/calculate/client-home on /home ' ) ]] &&
		umount /home
	eend 0
}

# vim:ts=4
