#! /bin/sh
# Autoconfiguration for fstab file

iffstab=/etc/fstab
offstab=/etc/fstab.new
bffstab=/etc/fstab.psplus
ddriver24="usbdevfs"
ddriver26="usbfs"

newcfg24="none /proc/bus/usb usbdevfs defaults,devmode=0666 0 0"
newcfg26="none /proc/bus/usb usbfs defaults,devmode=0666 0 0"

kernelv24=`uname -r|grep -o 2.4.`
kernelv26=`uname -r|grep -o 2.6.`


# arg 1 is a device driver name
# arg 2 is a new configuration string
configure_kernel()
{
    testres=`cat $iffstab|grep -i $1`
    if [ -n "$testres" ]; then
	FR1=`cat $iffstab|grep -i -v $1 >$offstab`;
	echo "# commented out by PowerSoft Plus installer" >>$offstab;
	echo "# $testres" >>$offstab;
    else
	FR1=`cat $iffstab >$offstab`;
    fi
    echo $2 >>$offstab;
}

answer="no"
echo -n "Would you like to have fstab file configured automatically? (yes/no) [no]"
read answer
if [ -z "$answer" ]; then
    answer="no";
    echo -n "$answer";
    echo "";
fi

case "$answer" in
    n|no|N|NO|No|non|NON|Non)
	echo ""
	echo "**************************************************************"
	echo "* Bear in mind that in order to enable mounting USB devices, *"
	echo "* it is necessary to add following line to /etc/fstab file:  *"
	echo "*                                                            *"
    if [ "$kernelv24" = "2.4." ]; then
	echo "*  none /proc/bus/usb usbdevfs defaults,mode=0666 0 0        *"
	echo "*                                                            *"
    elif [ "$kernelv26" = "2.6." ]; then
	echo "*  none /proc/bus/usb usbfs defaults,mode=0666 0 0           *"
	echo "*                                                            *"
    else
	echo "* for 2.4 kernels:                                           *"
	echo "*  none /proc/bus/usb usbdevfs defaults,mode=0666 0 0        *"
	echo "*                                                            *"
	echo "* and for 2.6 kernels:                                       *"
	echo "*  none /proc/bus/usb usbfs defaults,mode=0666 0 0           *"
	echo "*                                                            *"
    fi
	echo "* Remove also ftdi_sio and usbserial modules                 *"
	echo "**************************************************************"
	echo ""
    exit 0
esac

modres=`lsmod |egrep -i -e \(ftdi_sio\|usbserial\)`
if [ -n "$testres" ]; then
    answer="no"
    echo "Incompatible modules detected (ftdi_sio and/or usbserial)"
    echo -n "Would you like to have them removed automatically? (yes/no) [no]"
    read answer
    if [ -z "$answer" ]; then
	answer="no";
	echo -n "$answer";
	echo "";
    fi
    case "$answer" in
	n|no|N|NO|No|non|NON|Non)
	    echo ""
	    echo "Bear in mind to remove them, in order to enable this new installed driver."
	;;
	y|yes|Y|YES|Yes|oui|OUI|Oui)
	    modprobe -r ftdi_sio
	    modprobe -r usbserial
	    echo "Modules successfully removed."
	;;
    esac
fi

if [ "$kernelv24" = "2.4." ]; then
    echo "configuring for 2.4 kernel..."
    configure_kernel "$ddriver24" "$newcfg24"
elif [ "$kernelv26" = "2.6." ]; then
    echo "configuring for 2.6 kernel..."
    configure_kernel "$ddriver26" "$newcfg26"
else
    echo "Unknown kernel version. Exiting."
    exit 0
fi

echo "Please review new fstab configuration file displayed below:"
echo "--------------------------------------------------------------"
cat $offstab
echo "--------------------------------------------------------------"
echo "is this new configuration correct? (yes/no) [no]"
answer="no"
read answer
if [ -z "$answer" ]; then
    answer="no"
    echo -n "$answer"
    echo ""
fi
case "$answer" in
    n|no|N|NO|No|non|NON|Non)
	echo ""
	echo "Proposed configuration has been written to $offstab. ok?"
	read
	exit 0
	;;
	
    y|yes|Y|YES|Yes|oui|OUI|Oui)
	mv "$iffstab" "$bffstab"
	mv "$offstab" "$iffstab"
	sleep 1
        mount -a
	echo "Configuration successfully updated."
	echo "Finished!"
	exit 0
	;;
esac

echo "Finished!"

exit 0
