#!/bin/sh
# vhwconf : autodetect hardware 
# then setup pcmcia, mouse, cdrom, modem, network 
#
# mini version of vlautosetup. Does not set:
# X-Window
# Network 
# Alsa
#
# released under the GPL2 license (c)2003

vdir=`dirname $0`
. $vdir/vasm-functions

############################################
# PROBE FIRST
probe() {
TITLE="VectorLinux Hardware Auto Setup"
$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --infobox \
"\n Probing the hardware...Please wait until completed" 5 60

mkdir -p /etc/sysconfig

if [ -f /etc/sysconfig/vector ]; 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/vector ]; then
    $DCMD --backtitle "$BACKTITLE"  --title "ERROR" --msgbox \
"\nSorry, hardware probing failed.\nPress Enter to Quit." 7 60
    clean_exit 1
fi

}


######################################
# PCMCIA socket detection
# wile_coyote was here
auto_pcmcia() {
pcmcia=""
if [ "$SOCKET0_DRIVER" != "" ]; then
    if [ "$SOCKET0_DRIVER" = "yenta_socket" -o "$SOCKET0_DRIVER" = "i82365" -o "$SOCKET0_DRIVER" = "tcic" ]; then
	pcmcia="1"
	infobox "Setting up PCMCIA $SOCKET0_DRIVER"
    fi
    if [ ! -x /etc/rc.d/rc.pcmcia ]; then
	chmod +x /etc/rc.d/rc.pcmcia
	/etc/rc.d/rc.pcmcia $> /dev/null
	sleep 1
	## Probe again, probably we can catch something new
	infobox "Probing PCMCIA devices ..."
	/sbin/vlsetup 1>/dev/null 2>/dev/null
	if [ -f /etc/sysconfig/vector ]; then
	    . /etc/sysconfig/vector
	fi
    fi
    sleep 1
fi
}


###############################################
# CD, CDWRITER, DVD detection
set_cdwriter()
{
    infobox "Setting up CDWRITER $3"
    rm -f /dev/$1
    ln -s /dev/$2 /dev/$1
    chmod a+rw /dev/$2
    if ! grep -F -q "/dev/$1" /etc/fstab; then
	echo "/dev/$1 /mnt/$1  subfs fs=auto,user,rw 0 0" >> /etc/fstab
    fi
    sleep 2
}

set_cdrom()
{
    infobox "Setting up CDROM $3"
    rm -f /dev/$1
    ln -s /dev/$2 /dev/$1
    chmod a+r /dev/$2
    if ! grep -F -q "/dev/$1" /etc/fstab; then
	echo "/dev/$1 /mnt/$1  subfs fs=auto,user,ro   0   0" >> /etc/fstab
    fi
    sleep 2
}

auto_cd() {
cdr=""
cdw=""
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
	        model=`cat /proc/ide/$ide/$disk/model`
    		if dmesg | grep -s "$disk:" | grep -s "RW" > /dev/null; then
		    set_cdwriter cdwriter$cdw $disk $model
		    if [ $cdw ]; then	    
    			cdw=`expr $cdw + 1`
		    else
			cdw="1"
		    fi
		else
		    set_cdrom cdrom$cdr $disk $model
		    if [ $cdr ]; then	    
    			cdr=`expr $cdr + 1`
		    else
			cdr="1"
		    fi
		fi
	    fi
	fi
    done
done

## MUST add SATA or SCSI CDRROM HERE

}


###############################################
# Mouse detection
auto_mouse() {

if [ "$MOUSE0_DEVICE" ]; then
    infobox "Setting up mouse $MOUSE0_DEVICE"
    ln -sf $MOUSE0_DEVICE /dev/mouse
    sleep 3
    return 0
fi

for word in mice mouse Mouse; do
    line=`dmesg | grep $word`;
    if [ "$line" ]; then
	infobox "Setting up `echo $line | cut -f 2 -d :`"
	sleep 3
	return 0
    fi
done
infobox "No mouse is detected" "WARNING"
sleep 4
}

#######################################################################
# MODEM

mk_rc_serial() {
cat <<EOF
#!/bin/sh
# /etc/rc.d/rc.serial   `date '+%a %b %e %Y' 2>/dev/null`
#
# This rc.serial is started from: /etc/rc.d/rc.M
# This rc.serial file is created when you run: vmodemset
#
# /dev/ttyS0 = com1 ttyS1 = com2 ttyS2 = com3 ttyS3 = com4
#
# Try to autoconfigure modem device using setserial.

# If setserial is not there abort the configuration.
[ -x /sbin/setserial ] || exit 0

echo "Configuring modem serial port: /dev/$MODEM"

/sbin/setserial -v /dev/$MODEM auto_irq skip_test autoconfig session_lockout

EOF
}

# test modem using ATZ
test_modem()
{
    rm -f /tmp/modem_test
    cat $1 > /tmp/modem_test &
    PID1=$!
    echo "ATZ" > $1
    sleep 3
    kill $PID1
    cat /tmp/modem_test | grep -qe "OK"
    RESULT=$?
    rm /tmp/modem_test
    return $RESULT
}

auto_modem()
{
  # If MODEM has been set, try it first
  MODEM=`readlink /dev/modem`
  if [ "$MODEM" ]; then
    MODEM=`basename $MODEM`
    if test_modem /dev/$MODEM; then
	infobox "Setting up modem $MODEM ..."
	sleep 2
	return 0    
    fi
  fi
    
  # find classic modem on serial port
  for WORD in DSR CTS; do
    cat /proc/tty/driver/serial 2>/dev/null | grep $WORD | while read LINE; do
      NUM=`echo $LINE | cut -b1`
      if test_modem /dev/ttyS${NUM}; then
	MODEM="ttyS${NUM}"
	infobox "Setting up modem $MODEM ..."
	rm -f /dev/modem
	ln -s /dev/$MODEM /dev/modem
	mk_rc_serial > /etc/rc.d/rc.serial
        chmod +x /etc/rc.d/rc.serial
	sleep 2
	return 0    
      fi
    done
  done
 
  # Try WinModem
  for DEV in ttyLT0 ttySL0; do
    if [ -f /dev/$DEV ]; then
	if test_modem /dev/$DEV; then
	    MODEM="$DEV"
	    infobox "Setting up modem $MODEM ..."
	    rm -f /dev/modem
	    ln -s /dev/$MODEM /dev/modem
	    sleep 2
	    return 0    
	fi
    fi
  done

  # Confirm Vector Linux HW detection
  if [ "$MODEM_FULLNAME" ]; then
      $DCMD --backtitle "$BACKTITLE" --title "MODEM DETECTION" --msgbox \
"\nModem $MODEM_FULLNAME is detected.\n
However, it cannot be configured. If this is a winmodem,
you need to get the driver from www.linmodem.org." 9 60
    sleep 1
  else
    infobox "No modem could be detected." "WARNING"
    sleep 4
  fi
  return 0
}
    
auto_net() {
# Set up eth0 and eth1 but not calling netconf yet
if [ "$NETCARD0_DRIVER" ]; then
    infobox "Setting up network $NETCARD0_FULLNAME"
    if [ -f /etc/rc.d/rc.netdevice ]; then
	rm -f /etc/rc.d/rc.netdevice
    fi
    cat /etc/modules.conf | grep -ve "alias *eth0" > /etc/modules.conf
    echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modules.conf
    cat /etc/modprobe.conf | grep -ve "alias *eth0" > /etc/modprobe.conf
    echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modprobe.conf
    #modprobe eth0 > /dev/null 2>&1
    sleep 1
fi

##fi # This one looks like the extra one that was causing the error
if [ "$NETCARD1_DRIVER" ]; then
    infobox "Setting up network $NETCARD1_FULLNAME"
    cat /etc/modules.conf | grep -ve "alias *eth1 " > /etc/modules.conf
    echo "alias eth1 $NETCARD1_DRIVER" >> /etc/modules.conf
    cat /etc/modprobe.conf | grep -ve "alias *eth1" > /etc/modprobe.conf
    echo "alias eth1 $NETCARD1_DRIVER" >> /etc/modprobe.conf
    #modprobe eth1 > /dev/null 2>&1
    sleep 1
fi

if [ -z "$NETCARD0_DRIVER" ] && [ -z "$NETCARD1_DRIVER" ]; then
    infobox "No network card detected" "WARNING"
    sleep 2
fi
}

#############################
# MAIN
probe

. /etc/sysconfig/vector

auto_pcmcia
auto_cd
auto_mouse
auto_modem
auto_net

