#!/bin/sh
TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
	mkdir -p $TMP
fi

. /usr/lib/setup/INCISO

if [ -f $TMP/Punattended ]; then
	eval $(grep "^SMB_SERVER=" $TMP/Punattended)
	eval $(grep "^SMB_ROOT=" $TMP/Punattended)
fi

REMOTE_SERVER=$SMB_SERVER
REMOTE_PATH=$SMB_ROOT

while [ 0 ]; do
	rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTsource

	umount -f /var/log/mount 2>/dev/null
	if [ -n "$(mount | grep /var/log/mount)" -o -d /var/log/mount/lost+found -o -d /var/log/mount/recycled -o -r /var/log/mount/io.sys ]; then
		cat <<- EOF > $TMP/tempmsg
		
		Setup failed to unmount a required directory:
		
		/var/log/mount
		
		Please reboot the machine an try again.
		
		EOF
		dialog \
		  --title "ERROR" \
		  --yes-label "REBOOT" \
		  --no-label "CANCEL" \
		  --yesno "$(cat $TMP/tempmsg)" \
		  11 70
		RET=$?
		rm -f $TMP/tempmsg
		if [ $RET -eq 0 ]; then
			reboot
		else
			exit 1
		fi
	fi

	while [ 0 ]; do
		cat <<- EOF > $TMP/tempmsg
		
		Please specify which Samba share on your network contains the
		contents (or an ISO image) of a Salix installation disc.
		
		Example: //192.168.0.1/public
		
		Note: Use the <Shares> button to request a list of shares and
		services from the server.
		 
		EOF
		dialog \
		  --title "SAMBA SHARE ADDRESS" \
		  --help-button --help-label "Shares" \
		  --inputbox "$(cat $TMP/tempmsg)" \
		  16 70 \
		  $REMOTE_SERVER \
		  2> $TMP/remote
		RET=$?
		REMOTE_SERVER="$(cat $TMP/remote)"
		rm -f $TMP/tempmsg $TMP/remote
		if [ $RET -eq 2 ]; then
			cat <<- EOF > $TMP/tempmsg
			
			Please enter the IP address of your Samba server.
			 
			EOF
			dialog \
			  --title "ENTER IP ADDRESS" \
			  --inputbox "$(cat $TMP/tempmsg)" \
			  10 70 \
			  $IPADD \
			  2> $TMP/ipadd
			RET=$?
			IPADD="$(cat $TMP/ipadd)"
			rm -f $TMP/tempmsg $TMP/ipadd
			if [ $RET -ne 0 ]; then
				continue
			fi
			smbclient -NL $IPADD 2>/dev/null | sed '/^Anonymous login successful/d' > /$TMP/tempmsg
			if [ $? -eq 0 ]; then
				REMOTE_SERVER="//$IPADD/"
			fi
			dialog \
			  --title "SERVICE LIST" \
			  --textbox "$TMP/tempmsg" \
			  25 80
			rm -f $TMP/tempmsg
			continue
		fi
		break
	done

	if [ $RET -ne 0 ]; then
		exit 1
	fi

	# load the 'cifs' module:
	modprobe cifs 2>/dev/null
	
	mkdir -p /var/log/mount 2>/dev/null
	mount -t cifs -o ro,guest,sec=none $REMOTE_SERVER /var/log/mount 2>/dev/null
	if [ $? -ne 0 ]; then
		cat <<- EOF > $TMP/tempmsg
		
		Failed to mount Samba share:
		
		$REMOTE_SERVER
		
		Please check and try again.
		
		EOF
		dialog \
		  --title "MOUNT FAILURE" \
		  --exit-label "OK" \
		  --textbox "$TMP/tempmsg" \
		  11 70
		rm -f $TMP/tempmsg
		continue
	else
		cat <<- EOF > $TMP/tempmsg
		
		Successfully mounted Samba share:
		
		$(mount | grep $REMOTE_SERVER)
		
		EOF
		dialog \
		  --title "MOUNT SUCCESS" \
		  --exit-label "OK" \
		  --textbox "$TMP/tempmsg" \
		  9 70
		  rm -f $TMP/tempmsg
	fi

	while [ 0 ]; do
		cat <<- EOF > $TMP/tempmsg
	
		Now please provide the path to a directory containing the contents
		(or an ISO image) of a Salix installation disc.
		
		Note: The path you enter must be relative to the mount point so
		'/var/log/mount/salix-xfce' should be entered as '/salix-xfce'.
		 
		EOF
		dialog \
		  --title "SELECT SOURCE DIRECTORY" \
		  --help-button --help-label "Browse" \
		  --inputbox "$(cat $TMP/tempmsg)" \
		  14 70 \
		  $REMOTE_PATH \
		  2> $TMP/sourcedir
		RET=$?
		REMOTE_PATH="$(cat $TMP/sourcedir)"
		rm -f $TMP/sourcedir $TMP/tempmsg
		if [ $RET -eq 2 ]; then
			if [ -z "$REMOTE_PATH" -o ! -d "/var/log/mount$REMOTE_PATH" ]; then
				REMOTE_PATH="/"
			fi
			dialog \
			  --title "SELECT A DIRECTORY" \
			  --dselect "/var/log/mount$REMOTE_PATH" \
			  12 80 \
			  2> $TMP/sourcedir
			REMOTE_PATH="$(cat $TMP/sourcedir | sed 's|^/var/log/mount||')"
			rm -f $TMP/sourcedir $TMP/tempmsg
			continue
		fi
		break
	done
	
	if [ $RET -ne 0 ]; then
		continue
	fi

	SRCDIR="$(echo "/var/log/mount$REMOTE_PATH" | sed 's|/$||')"

	check_iso_image $SRCDIR /var/log/mntiso
	if [ $? -eq 0 ]; then
		if [ -d /var/log/mntiso/salix ]; then
			echo "/var/log/mntiso/salix" > $TMP/SeTDS
		else
			cat <<- EOF > $TMP/tempmsg
			
			The ISO image you specified is not valid:
			
			$(basename $SOURCEISO)
			
			Please check and try again.
			
			EOF
			dialog \
			  --title "INVALID ISO IMAGE" \
			  --exit-label "OK" \
			  --textbox "$TMP/tempmsg" \
			  11 70
			rm -f $TMP/tempmsg
			umount -f /var/log/mntiso 2>/dev/null
			continue
		fi
	elif [ -d $SRCDIR/salix -a -f $SRCDIR/PACKAGES.TXT.gz ]; then
		echo "$SRCDIR/salix" > $TMP/SeTDS
	elif [ -d $SRCDIR/core -a -d $SRCDIR/basic -a -d $SRCDIR/full ]; then
		echo "$SRCDIR" > $TMP/SeTDS
	else
		cat <<- EOF > $TMP/tempmsg
		
		The directory you specified is not valid:
		
		$REMOTE_PATH
		
		Please check and try again.
		
		EOF
		dialog \
		  --title "INVALID SOURCE DIRECTORY" \
		  --exit-label "OK" \
		  --textbox "$TMP/tempmsg" \
		  11 70
		rm -f $TMP/tempmsg
		continue
	fi
	echo "-source_mounted" > $TMP/SeTmount
	echo "/dev/null" > $TMP/SeTsource
	break
done
