#!/bin/sh
#
# vlautosetup - Created by Tony Brijeski
#
# released under the GPL2 license (c)2003
#

# just execute config-stage1 now
vdir=`dirname $0`
exec $vdir/config-stage1 auto-setup


#######################################################
# Leave this here for historical reason

if [ $DISPLAY != "" ]; then
    DCMD="Xdialog --wrap" 
    CMD="rxvt -e "
else
    DCMD="dialog"
    CMD=""
fi

$DCMD --backtitle "VLocityLinux Hardware Auto Setup"  --title \
"VLocityLinux Hardware Auto Setup" --infobox \
"\n Probing the hardware...Please wait until completed" 5 60

if [ -f /etc/sysconfig/vlocity ]; then
    rm -rf /etc/sysconfig/*
fi
if [ ! -f /etc/sysconfig/kudzu ]; then
    echo "SAFE=yes" > /etc/sysconfig/kudzu
fi

/sbin/vlsetup 1>/dev/null 2>/dev/null

if [ ! -f /etc/sysconfig/vlocity ]; then
$DCMD --backtitle "VLocityLinux Hardware Auto Setup"  --title \
"VLocityLinux Hardware Auto Setup" --msgbox \
"\n Hardware Probe Failed...Press Enter to Return" 5 60
exit
fi

. /etc/sysconfig/vlocity

if [ "$MOUSE0_DEVICE" != "" ]; then
    (cd /dev; rm -f mouse; ln -sf $MOUSE0_DEVICE mouse)
    mousesetup="1"
fi

## Kocil was here, fix for X.org
## 
if [ -x /usr/X11R6/bin/XFree86 ]; then
    if [ "$XMODULE" != "" ]; then
	/sbin/vlmkxf86cfg
	xf86setup="1"
    fi
elif [ -x /usr/X11R6/bin/Xorg ]; then
    # use vxconf for now, until vlmkxf86cfg upgraded for xorg
    /sbin/vxconf
    xf86setup="1"
    XSERVER=`/usr/X11R6/bin/Xorg -version | grep "X Protocol Version"`
fi

# Added for PCMCIA socket detection
# wile_coyote was here
#
pcmcia=""
if [ "$SOCKET0_DRIVER" != "" ]; then
    if [ "$SOCKET0_DRIVER" = "yenta_socket" -o "$SOCKET0_DRIVER" = "i82365" -o "$SOCKET0_DRIVER" = "tcic" ]; then
	pcmcia="1"
    fi
    if [ ! -x /etc/rc.d/rc.pcmcia ]; then
	chmod +x /etc/rc.d/rc.pcmcia
    fi	
fi
#  
# End PCMCIA section
#

if [ "$NETCARD0_DRIVER" != "" ]; then
    if [ -f /etc/rc.d/rc.netdevice ]; then
	rm -f /etc/rc.d/rc.netdevice
    fi
    cat /etc/modules.conf | grep -F "eth0" | grep -F "$NETCARD0_DRIVER" >/dev/null
    if [ $? != "0" ]; then
	echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modules.conf
	echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modprobe.conf
    fi
    ## Kocil was here, fix assigning networkcard and launch new vnetconf if exist
    networkcard="1"
    if [ -x /sbin/vnetconf ]; then
	/sbin/vnetconf
    else
	netconfig
    fi
fi

##fi # This one looks like the extra one that was causing the error
if [ "$NETCARD1_DRIVER" != "" ]; then
cat /etc/modules.conf | grep -F "eth1" | grep -F "$NETCARD1_DRIVER" >/dev/null
    if [ $? != "0" ]; then   
	echo "alias eth1 $NETCARD1_DRIVER" >> /etc/modules.conf
	echo "alias eth1 $NETCARD1_DRIVER" >> /etc/modprobe.conf
	networkcard="2"
    fi
fi

if [ "$networkcard" = "" ]; then
$DCMD --title "NO NETWORK CARD DETECTED" --yesno \
"No network card was detected on your system. This sometimes happens with laptop users depending on pcmcia services or if your using a modem connection.\n
If you need to setup your network connection for a laptop say yes here. Modem users can say no." 10 60
    if [ $? = "0" ]; then
        ## Kocil was here,launch new vnetconf if exist
	if [ -x /sbin/vnetconf ]; then
	    /sbin/vnetconf
	else
	    netconfig
	fi
    else
	## set hostname, important
	/sbin/vnameset    
    fi
fi

## Kocil was here, launch modified alsaconf if exist
if [ "$SOUND_DRIVER" ]; then
    if [ -x /sbin/valsaconf ]; then
        /sbin/valsaconf
    else
        alsaconf
    fi
    soundsetup="1"
fi

## Kocil was here, add modem
if [ "$MODEM_FULLNAME" ]; then
   if echo $MODEM_FULLNAME | grep -qv WinModem; then
      /sbin/vmodemset
      modemsetup="1"
   else
      $DCMD --title "WINMODEM DETECTED" --msgbox \
"\n$MODEM_FULLNAME is detected.\n
However this modem needs proprietary driver from www.linmodem.org,\n
thus we will skip modem configuration." 9 70
   fi
fi

infobox "Setting up CD/CDWRITER/DVD ..."

#start looking for cd devices
rm -f /dev/cdwriter
rm -f /dev/cdwriter1
rm -f /dev/cdrom
rm -f /dev/cdrom1
cd=0
cdw=0
for ide in ide0 ide1; do
for disk in hda hdb hdc hdd hde hdf hdg hdh; do
   if [ -r /proc/ide/$ide/$disk/media ] ; then
     if grep -s -q "cdrom" /proc/ide/$ide/$disk/media; then
     if dmesg | grep -s "$disk:" | grep -s "RW" > /dev/null; then
     cd1="1"
     cd1t=`cat /proc/ide/$ide/$disk/model`
        cdw=`expr $cdw + 1`
	CDWRITER0="/dev/$disk"
	(cd /dev; rm -f cdwriter; ln -sf $disk cdwriter)
	## Kocil was here, add chmod for all users
	chmod a+rw $CDWRITER0
	if ! grep -F -q "/dev/cdwriter" /etc/fstab; then
	echo "/dev/cdwriter /mnt/cdwriter subfs fs=auto,user,ro 0 0" >> /etc/fstab
	fi
	
	if [ $cdw = "2" ]; then
	cd1="1"
     cd1t=`cat /proc/ide/$ide/$disk/model`
	CDWRITER1="/dev/$disk"
	( cd /dev; rm -f cdwriter1; ln -sf $disk cdwriter1)
	## Kocil was here, add chmod for all users
	chmod a+rw $CDWRITER1
	if ! grep -F -q "/dev/cdwriter1" /etc/fstab; then
	echo "/dev/cdwriter1 /mnt/cdwriter1 subfs fs=auto,user,ro 0 0" >> /etc/fstab
	fi
	fi
		
	else
	cd2="1"
     cd2t=`cat /proc/ide/$ide/$disk/model`
	cd=`expr $cd + 1`
	CDR0="/dev/$disk"
	(cd /dev; rm -f cdrom; ln -sf $disk cdrom)
	## Kocil was here, add chmod for all users
	chmod a+r $CDR0
	if ! grep -F -q "/dev/cdrom" /etc/fstab; then
	echo "/dev/cdrom /mnt/cdrom subfs fs=auto,user,ro 0 0" >> /etc/fstab
	fi	
	if [ $cd = "2" ]; then
	cd2="1"
     cd2t=`cat /proc/ide/$ide/$disk/model`
	CDR1="/dev/$disk"
	(cd /dev; rm -f cdrom1; ln -sf $disk cdrom1)
	## Kocil was here, add chmod for all users
	chmod a+r $CDR1
	if ! grep -F -q "/dev/cdrom1" /etc/fstab; then
	echo "/dev/cdrom1 /mnt/cdrom1 subfs fs=auto,user,ro 0 0" >> /etc/fstab
	fi
	fi
	fi
	fi
   fi
done
done
if [ ! $cdw = "1" ]; then
cd1=0
fi
if [ ! $cd = "1" ]; then
cd2=0
fi

autosetup=""
if [ "$xf86setup" = "1" ]; then 
    autosetup="$autosetup Video Card=$XDESC \n"
fi
if [ "$mousesetup" = "1" ]; then
    autosetup="$autosetup Mouse=$MOUSE0_FULLNAME \n"
fi
if [ "$pcmcia" = "1" ]; then
    autosetup="$autosetup PCMCIA Slot(s)=$SOCKET0_FULLNAME \n"
fi    
if [ "$networkcard" = "1" ]; then
    autosetup="$autosetup NetCard =$NETCARD0_FULLNAME \n"
fi
if [ "$networkcard" = "2" ]; then
    autosetup="$autosetup NetCard =$NETCARD0_FULLNAME \n"
    autosetup="$autosetup NetCard =$NETCARD1_FULLNAME \n"
fi
if [ "$soundsetup" = "1" ]; then
    autosetup="$autosetup Sound Card=$SOUND_FULLNAME\n"
fi
if [ "$modemsetup" = "1" ]; then
    autosetup="$autosetup Modem=$MODEM_FULLNAME\n"
fi
if [ "$cd1" = "1" ]; then
    autosetup="$autosetup CDROM1=$cd1t\n"
fi
if [ "$cd2" = "1" ]; then
    autosetup="$autosetup CDROM2=$cd2t\n"
fi


$DCMD --backtitle "VLocityLinux Hardware Auto Setup"  --title \
"VLocityLinux Hardware Auto Setup" --msgbox \
"Automatic Hardware Setup Complete.\n\
The Following Items have been automatically setup: \n 
\n $autosetup \n\
\n\
Press OK to continue." 15 76

