#!/bin/sh

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

T_PX="`cat /tmp/SeTT_PX`"
rm -f /tmp/SeTmount /tmp/SeTDS /tmp/SeTCDdev

modprobe isofs
modprobe nls_cp932
modprobe sr_mod

expand_mes
dialog --title "scanning DVD devices" --infobox "$scan_mes"  3 60
for device in /dev/sr0 /dev/sr1 /dev/sr2 ; do
    mount -o ro -t iso9660 $device /var/adm/mount 1> /dev/null 2> /dev/null
    if [ $? = 0 ]; then
	DRIVE_FOUND=$device
	break
    fi
done
umount /var/adm/mount 1> /dev/null 2> /dev/null

expand_mes
if [ "$DRIVE_FOUND" = "" ]; then
    dialog --title "Can't find DVD device" --msgbox "$not_found_mes" 14 70
    exit
else
    dialog --title "Found DVD device" --msgbox "$found_mes" 5 65
    CD_DEVICE="$DRIVE_FOUND"
fi

# The CD will be mounted automatically later on:
if [ "$DRIVE_FOUND" != "" ]; then
    echo "$CD_DEVICE       /cdrom        iso9660       user,ro,noauto,exec,iocharset=utf8  0   0" > /tmp/SeTcdfstab
fi

while [ 0 ] ; do
    dialog --title "Select install method" \
	   --menu "$method_mes" 15 74 4 \
	   "plamo" "$method_list1" \
	   "custom" "$method_list2" \
	   2> /tmp/SeTreturn
    if [ $? = 1 -o $? = 255 ]; then
	rm -f /tmp/cdtype /tmp/SeTreturn
	exit
    fi
    break
done

SLACK_SOURCE_LOCATION="`cat /tmp/SeTreturn`"

if [ "$SLACK_SOURCE_LOCATION" = "custom" ]; then
    dialog --title "Select packages directory" --inputbox "$src_select_mes" 15 72 \
	   2> /tmp/slacksource
    if [ $? = 1 -o $? = 255 ]; then
	rm -f /tmp/cdtype /tmp/slacksource
	exit
    fi
    rm -f /tmp/tmpmsg
    SLACK_SOURCE_LOCATION="`cat /tmp/slacksource`"
    rm -f /tmp/slacksource
fi

while [ 0 ]; do
    mount -o ro -t iso9660 $CD_DEVICE /var/adm/mount 1> /dev/null 2> /dev/null
    if [ ! $? = 0 ]; then
	expand_mes
	dialog --title "Couldn't mount ${CD_DEVICE}" --menu "$err_mes" 11 60 3 \
	       "1" "$err_list1" \
	       "2" "$err_list2" \
	       "3" "$err_list3" 2> /tmp/errordo
	if [ $? = 1 -o $? = 255 ]; then
	    rm -f /tmp/SeTDS /tmp/SeTmount /tmp/SeTCDdev /tmp/errordo
	    exit;
	fi
	DOWHAT="`cat /tmp/errordo`"
	rm -f /tmp/errordo
	if [ $DOWHAT = 1 ]; then
	    rm -f /tmp/SeTDS /tmp/SeTmount /tmp/SeTCDdev
	    exit;
	elif [ $DOWHAT = 2 ]; then
	    continue;
	elif [ $DOWHAT = 3 ]; then
	    break;
	fi
    else
	if [ ! -d /var/adm/mount/$SLACK_SOURCE_LOCATION ]; then
	    expand_mes
	    dialog --title "Error: Cannot find directory" --msgbox "$err_nodir_mes" 8 70
	    rm -f /tmp/SeTmount /tmp/SeTDS /tmp/SeTCDdev   
	    exit
	fi
	break;
    fi
done

# since we've mounted successfully, let's refresh the link:
if [ ! -d $T_PX/dev ]; then
    mkdir $T_PX/dev
    chown root.root $T_PX/dev
    chmod 755 $T_PX/dev
fi
( cd $T_PX/dev ; ln -sf $CD_DEVICE cdrom )
echo "/var/adm/mount/$SLACK_SOURCE_LOCATION" > /tmp/SeTDS
echo "-source_mounted" > /tmp/SeTmount
echo "$CD_DEVICE" > /tmp/SeTCDdev
echo "/dev/null" > /tmp/SeTsource

