#!/bin/sh
#
art=$1
INPUTFILE=`cat /var/etc/automount/automounts.xml`
IPLIST=`echo $INPUTFILE | tr " " "\n" | grep "<ip>" | cut -d ">" -f2 | cut -d "<" -f1`
INPUT=`echo $INPUTFILE | tr " " "\n" | sed 's!</cifs>!#<cifs>!' | sed 's!</nfs>!#<nfs>!' | tr "\n" " " | sed 's/<?xml version="1.0" ?> <mountmanager>//'`
netdir=/media/net

if [ "$art" == "first" ]; then

	# here will be mounted only when hdd_replacement=True ... all other netmounts can be mounted later .. this is faster 

	if [ `echo $INPUTFILE | tr "<" "\n" | grep "hdd_replacement>" | grep True | tail -n1 | wc -l` -eq 1 ];then
		ENTRYS=`echo $INPUT | tr " " "\n" | grep "#" | wc -l`
		i=0
		while [[ $((++i)) -le $ENTRYS ]]; do
			CFGCHECK=`echo $INPUT | cut -d "#" -f$i`
			hdd_replacement=`echo $CFGCHECK | tr "<" "\n" | grep "hdd_replacement>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
			if [ "$hdd_replacement" == "True" ];then
				active=`echo $CFGCHECK | tr "<" "\n" | grep "active>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
				if [ "$active" == "True" ]; then
					IP=`echo $CFGCHECK | tr "<" "\n" | grep "ip>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					check=`ping -c1 -w1 $IP | wc -l`
					if [ "$check" = "6" ]; then

						sharename=`echo $CFGCHECK | tr "<" "\n" | grep "sharename>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						mounttype=`echo $CFGCHECK | tr " " "\n" | head -n1`
						ip=`echo $CFGCHECK | tr "<" "\n" | grep "ip>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						if [ "$mounttype" == "<cifs>" ]; then
							sharedir=`echo $CFGCHECK | tr "<" "\n" | grep "sharedir>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						elif [ "$mounttype" == "<nfs>" ]; then
							sharedir=`echo $CFGCHECK | tr "<" "\n" | grep "sharedir>" | cut -d ">" -f2`
						fi
						options=`echo $CFGCHECK | tr "<" "\n" | grep "options>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						rsize=`echo $CFGCHECK | tr "<" "\n" | grep "rsize>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						wsize=`echo $CFGCHECK | tr "<" "\n" | grep "wsize>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`

						if [ "$mounttype" == "<cifs>" ]; then
							username=`echo $CFGCHECK | tr "<" "\n" | grep "username>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							password=`echo $CFGCHECK | tr "<" "\n" | grep "password>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						elif [ "$mounttype" == "<nfs>" ]; then
							protocol=`echo $CFGCHECK | tr "<" "\n" | grep "protocol>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						fi		
						echo "[nmount first] active hdd replacament Share found in automounts.xml"
						echo "[nmount first] mounting..."

						if [ ! -d "$netdir/$sharename" ]; then	
							echo [nmount first] create mountdir $netdir/$sharename
							mkdir -p "$netdir/$sharename"
						fi

						if [ `cat /proc/mounts | grep "/media/hdd" | wc -l` -eq 0 ]; then
							if [ ! -d "/media/hdd" ]; then
								echo "[nmount first] create symlink $netdir/$sharename > /media/hdd"
								ln -s "$netdir/$sharename" /media/hdd
							else
								LENG=`ls -al "/media/hdd" | wc -l`
								if [ "$LENG" = "2" ]; then
									echo [nmount first] remove dir /media/hdd
									rm -r /media/hdd
									echo "[nmount first] create symlink $netdir/$sharename > /media/hdd"
									ln -s "$netdir/$sharename" /media/hdd
								fi
							fi
						fi

						if [ "$mounttype" == "<cifs>" ]; then
							echo [nmount first] mount -t cifs -o "$options",rsize="$rsize",wsize="$wsize",username=X-hidden-X,password=X-hidden-X //"$ip"/"$sharedir" "$netdir"/"$sharename"
							mount -t cifs -o "$options",rsize="$rsize",wsize="$wsize",username="$username",password="$password" //"$ip"/"$sharedir" "$netdir"/"$sharename"
						elif [ "$mounttype" == "<nfs>" ]; then
							echo [nmount first] mount -t nfs -o $protocol,$options,rsize=$rsize,wsize=$wsize $ip:$sharedir $netdir/$sharename
							mount -t nfs -o $protocol,$options,rsize=$rsize,wsize=$wsize $ip:$sharedir $netdir/$sharename
						fi

					else
						echo "[nmount first] Error: Share IP not reachable! Cannot mount!"
					fi
				else
					echo "[nmount first] skip mounting: hdd replacement share not active"
					touch /tmp/.nmountskip
				fi
			fi
		done
	else
		touch /tmp/.nmountskip
	fi

elif [ "$art" == "record" ]; then

	# here will be mounted only when hdd_replacement=True ... all other netmounts can be mounted later .. this is faster 

	if [ `echo $INPUTFILE | tr "<" "\n" | grep "hdd_replacement>" | grep True | tail -n1 | wc -l` -eq 1 ];then
		ENTRYS=`echo $INPUT | tr " " "\n" | grep "#" | wc -l`
		i=0
		while [[ $((++i)) -le $ENTRYS ]]; do
			CFGCHECK=`echo $INPUT | cut -d "#" -f$i`
			hdd_replacement=`echo $CFGCHECK | tr "<" "\n" | grep "hdd_replacement>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
			if [ "$hdd_replacement" == "True" ];then
				active=`echo $CFGCHECK | tr "<" "\n" | grep "active>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
				if [ "$active" == "True" ]; then
					IP=`echo $CFGCHECK | tr "<" "\n" | grep "ip>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					check=`ping -c1 -w1 $IP | wc -l`
					if [ "$check" = "6" ]; then

						sharename=`echo $CFGCHECK | tr "<" "\n" | grep "sharename>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						if [ `cat /proc/mounts | grep "$netdir/$sharename" | wc -l` -eq 0 ]; then
							mounttype=`echo $CFGCHECK | tr " " "\n" | head -n1`
							ip=`echo $CFGCHECK | tr "<" "\n" | grep "ip>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							if [ "$mounttype" == "<cifs>" ]; then
								sharedir=`echo $CFGCHECK | tr "<" "\n" | grep "sharedir>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							elif [ "$mounttype" == "<nfs>" ]; then
								sharedir=`echo $CFGCHECK | tr "<" "\n" | grep "sharedir>" | cut -d ">" -f2`
							fi
							options=`echo $CFGCHECK | tr "<" "\n" | grep "options>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							rsize=`echo $CFGCHECK | tr "<" "\n" | grep "rsize>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							wsize=`echo $CFGCHECK | tr "<" "\n" | grep "wsize>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`

							if [ "$mounttype" == "<cifs>" ]; then
								username=`echo $CFGCHECK | tr "<" "\n" | grep "username>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
								password=`echo $CFGCHECK | tr "<" "\n" | grep "password>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							elif [ "$mounttype" == "<nfs>" ]; then
								protocol=`echo $CFGCHECK | tr "<" "\n" | grep "protocol>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
							fi		
							echo "[nmount record] active hdd replacament Share found in automounts.xml"
							echo "[nmount record] mounting..."

							if [ ! -d "$netdir/$sharename" ]; then	
								echo [nmount record] create mountdir $netdir/$sharename
								mkdir -p "$netdir/$sharename"
							fi

							if [ `cat /proc/mounts | grep "/media/hdd" | wc -l` -eq 0 ]; then
								if [ ! -d "/media/hdd" ]; then
									echo "[nmount record] create symlink $netdir/$sharename > /media/hdd"
									ln -s "$netdir/$sharename" /media/hdd
								else
									LENG=`ls -al "/media/hdd" | wc -l`
									if [ "$LENG" = "2" ]; then
										echo [nmount record] remove dir /media/hdd
										rm -r /media/hdd
										echo "[nmount record] create symlink $netdir/$sharename > /media/hdd"
										ln -s "$netdir/$sharename" /media/hdd
									fi
								fi
							fi

							if [ "$mounttype" == "<cifs>" ]; then
								echo [nmount record] mount -t cifs -o "$options",rsize="$rsize",wsize="$wsize",username=X-hidden-X,password=X-hidden-X //"$ip"/"$sharedir" "$netdir"/"$sharename"
								mount -t cifs -o "$options",rsize="$rsize",wsize="$wsize",username="$username",password="$password" //"$ip"/"$sharedir" "$netdir"/"$sharename"
							elif [ "$mounttype" == "<nfs>" ]; then
								echo [nmount record] mount -t nfs -o $protocol,$options,rsize=$rsize,wsize=$wsize $ip:$sharedir $netdir/$sharename
								mount -t nfs -o $protocol,$options,rsize=$rsize,wsize=$wsize $ip:$sharedir $netdir/$sharename
							fi
						else
							echo "[nmount record] $netdir/$sharename is already mounted"
							touch /tmp/.nmountskip
						fi

					else
						echo "[nmount record] Error: Share IP not reachable! Cannot mount!"
					fi
				else
					echo "[nmount record] skip mounting: hdd replacement share not active"
					touch /tmp/.nmountskip
				fi
			fi
		done
	else
		touch /tmp/.nmountskip
	fi

else

	# here will be mounted everything from automounts.xml...maybe usefull....

	ENTRYS=`echo $INPUT | tr " " "\n" | grep "#" | wc -l`
	i=0
	while [[ $((++i)) -le $ENTRYS ]]; do
		CFGCHECK=`echo $INPUT | cut -d "#" -f$i`
		sharename=`echo $CFGCHECK | tr "<" "\n" | grep "sharename>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
		if [ `cat /proc/mounts | grep "$netdir/$sharename" | wc -l` -eq 0 ]; then
			active=`echo $CFGCHECK | tr "<" "\n" | grep "active>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
			if [ "$active" == "True" ]; then
				IP=`echo $CFGCHECK | tr "<" "\n" | grep "ip>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
				check=`ping -c1 -w1 $IP | wc -l`
				if [ "$check" = "6" ]; then

					mounttype=`echo $CFGCHECK | tr " " "\n" | head -n1`
					ip=`echo $CFGCHECK | tr "<" "\n" | grep "ip>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					if [ "$mounttype" == "<cifs>" ]; then
						sharedir=`echo $CFGCHECK | tr "<" "\n" | grep "sharedir>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					elif [ "$mounttype" == "<nfs>" ]; then
						sharedir=`echo $CFGCHECK | tr "<" "\n" | grep "sharedir>" | cut -d ">" -f2`
					fi
					options=`echo $CFGCHECK | tr "<" "\n" | grep "options>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					rsize=`echo $CFGCHECK | tr "<" "\n" | grep "rsize>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					wsize=`echo $CFGCHECK | tr "<" "\n" | grep "wsize>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`

					if [ "$mounttype" == "<cifs>" ]; then
						username=`echo $CFGCHECK | tr "<" "\n" | grep "username>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
						password=`echo $CFGCHECK | tr "<" "\n" | grep "password>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					elif [ "$mounttype" == "<nfs>" ]; then
						protocol=`echo $CFGCHECK | tr "<" "\n" | grep "protocol>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					fi		
					echo "[nmount all] active Share found in automounts.xml"
					echo "[nmount all] mounting..."

					if [ ! -d "$netdir/$sharename" ]; then	
						echo [nmount all] create mountdir $netdir/$sharename
						mkdir -p "$netdir/$sharename"
					fi

					hdd_replacement=`echo $CFGCHECK | tr "<" "\n" | grep "hdd_replacement>" | cut -d ">" -f2 | cut -d "/" -f1 | sed '/^ *$/d'`
					if [ "$hdd_replacement" == "True" ];then
						if [ `cat /proc/mounts | grep "/media/hdd" | wc -l` -eq 0 ]; then
							if [ ! -d "/media/hdd" ]; then
								echo "[nmount all] create symlink $netdir/$sharename > /media/hdd"
								ln -s "$netdir/$sharename" /media/hdd
							else
								LENG=`ls -al "/media/hdd" | wc -l`
								if [ "$LENG" = "2" ]; then
									echo [nmount all] remove dir /media/hdd
									rm -r /media/hdd
									echo "[nmount all] create symlink $netdir/$sharename > /media/hdd"
									ln -s "$netdir/$sharename" /media/hdd
								fi
							fi
						fi
					fi

					if [ "$mounttype" == "<cifs>" ]; then
						echo [nmount all] mount -t cifs -o "$options",rsize="$rsize",wsize="$wsize",username=X-hidden-X,password=X-hidden-X //"$ip"/"$sharedir" "$netdir"/"$sharename"
						mount -t cifs -o "$options",rsize="$rsize",wsize="$wsize",username="$username",password="$password" //"$ip"/"$sharedir" "$netdir"/"$sharename"
					elif [ "$mounttype" == "<nfs>" ]; then
						echo [nmount all] mount -t nfs -o $protocol,$options,rsize=$rsize,wsize=$wsize $ip:$sharedir $netdir/$sharename
						mount -t nfs -o $protocol,$options,rsize=$rsize,wsize=$wsize $ip:$sharedir $netdir/$sharename
					fi

				else
					echo "[nmount all] Error: Share IP not reachable! Cannot mount!"
				fi
			else
				echo "[nmount all] skip mounting: this Share is not active"
			fi
		else
			echo "[nmount all] $netdir/$sharename is already mounted"
		fi
	done

fi