#!/bin/sh

expand_mes() {
    source /usr/lib/setup/INShd.mes
}

while [ 0 ]; do
    rm -f /tmp/SeTDS /tmp/SeTmount

    while [ 0 ]; do
	expand_mes
	dialog --title "Install from HDD parition" --inputbox "$hdd_mes" 20 70 2> /tmp/source.part
	if [ $? = 1 -o $? = 255 ]; then
	    rm -f /tmp/source.part
	    exit
	fi
	SLACK_DEVICE="`cat /tmp/source.part`"
	rm -f /tmp/source.part
	if [ "$SLACK_DEVICE" = "" ]; then
	    dialog --title "List of HDD partitions" --msgbox "`probe -l | grep "^/dev/sd[a-z]*" 2> /dev/null`" 22 75
	    continue;
	fi
	break;
    done

    dialog --title "Enter packages directory" --inputbox "$dir_mes" 19 65 2> /tmp/source.dir
    if [ $? = 1 -o $? = 255 ]; then
	rm -f /tmp/source.dir
	exit
    fi
    SLACK_SOURCE_LOCATION="`cat /tmp/source.dir`"
    rm -f /tmp/source.dir
    if mount | grep ${SLACK_DEVICE} 1> /dev/null 2> /dev/null ; then
	# This partition is already mounted, so we will have to
	# tweak things funny.
	rm -f /var/adm/mount 2> /dev/null
	rmdir /var/adm/mount 2> /dev/null
	PREFIX="`mount | grep ${SLACK_DEVICE}`"
	PREFIX="`echo "${PREFIX}" | cut -b14-`"
	end_of_line="1"
	while [ 0 ]; do
	    end_of_line="`expr $end_of_line + 1`"
	    if [ "`echo "$PREFIX" | cut -b$end_of_line`" = " " ]; then  # Found it!
		end_of_line="`expr $end_of_line - 1`"
		break;
	    fi
	done
	PREFIX="`echo "$PREFIX" | cut -b1-$end_of_line`"
	ln -sf $PREFIX /var/adm/mount 
    else
	SUCCESS=false
	echo "trying partition : ${SLACK_DEVICE}"
	mount ${SLACK_DEVICE} /var/adm/mount 1> /dev/null 2> /dev/null
	if [ $? = 0 ]; then # mounted successfully 
	    SUCCESS=true
	fi
	if [ ! "${SUCCESS}" = "true" ]; then # there was a mount error
	    sleep 5
	    expand_mes
	    dialog --title "Mount error" --menu "$mnterr_mes" 10 68 2 \
		   "Restart" "$mnterr_list01" \
		   "Ignore " "$mnterr_list02" 2> /tmp/dowhat
	    if [ $? = 1 -o $? = 255 ]; then
		rm -f /tmp/dowhat
		exit
	    fi
	    DOWHAT="`cat /tmp/dowhat`"
	    rm -f /tmp/dowhat
	    if [ "$DOWHAT" = "Restart" ]; then
		umount /var/adm/mount 2> /dev/null
		continue;
	    fi
	    echo
	fi # mount error
    fi
    
    if [ -d /var/adm/mount/${SLACK_SOURCE_LOCATION} ]; then
	echo "/var/adm/mount/${SLACK_SOURCE_LOCATION}" > /tmp/SeTDS
	echo "-source_mounted" > /tmp/SeTmount
	echo "/dev/null" > /tmp/SeTsource
	exit
    else
	expand_mes
	dialog --title "Cannot find packages directory" --yesno "$direrr_mes" 10 70
	if [ $? = 1 -o $? = 255 ]; then
	    rm -f /tmp/tmpmsg
	    exit
	fi
	rm -r /tmp/tmpmsg
    fi
    
    if [ ! -d /var/adm/mount/${SLACK_SOURCE_LOCATION}/00_base ]; then
	expand_mes
	dialog --title "Cannot find categorized directory" --yesno "$nodir_mes" 10 70
	if [ $? = 1 -o $? = 255 ]; then
	    rm -f /tmp/tmpmsg
	    exit
	fi
	rm -r /tmp/tmpmsg
    fi

done
