#!/bin/sh
# vhwconf : autodetect hardware 
# then setup hotplug, pcmcia, cdrom, serial,
# mouse, modem, network device
#
# mini version of vlautosetup. Does not setup:
# X-Window
# Network
# Alsa
#
# (c) Vector Linux, 2003
# Originally by Tony Brijeski (vlautosetup)
# Debugged by wile_coyote
# Stripped down by Eko M. Budi
#
# released under the GPL2 license 

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

check_root
check_xwindow

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

mkdir -p /etc/sysconfig
rm -f /etc/sysconfig/vector
echo "SAFE=yes" > /etc/sysconfig/kudzu

/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

}

######################################
# TMPFS
# Turn on tmpfs if RAM >= 256 and SWAP >= 256
auto_tmpfs() {
if [ -f /etc/rc.d/rc.tmpfs ]; then
    MEM=`grep -e "^Mem:" /proc/meminfo`
    MEM=`echo $MEM | cut -f2 -d ' '`
    SWAP=`grep -e "^Swap:" /proc/meminfo`
    SWAP=`echo $SWAP | cut -f2 -d ' '`
    echo $MEM $SWAP
    if [ $MEM -gt 268435456 ] && [ $SWAP -gt 268435456 ]; then
	infobox "You have plenty of RAM, enabling tmpfs" "TMPFS"
	chmod +x /etc/rc.d/rc.tmpfs
    else
	chmod -x /etc/rc.d/rc.tmpfs
    fi
fi
}

######################################
# HOTPLUG
auto_hotplug() {
# Turn on hotplug if the kernel support it
# and nohotplug is not defined
if [ -w /proc/sys/kernel/hotplug ] && \
    ! grep -qw nohotplug /proc/cmdline; then
  infobox "Enabling USB/PCI hotplug" "HOTPLUG"
  sleep 2
  chmod +x /etc/rc.d/rc.hotplug
  case `kernelversion` in
    2.6*)
	infobox "Enabling UDEV for kernel 2.6" "UDEV"
	sleep 2
	chmod +x /etc/rc.d/rc.udev
	;;
    2.4)
	chmod -x /etc/rc.d/rc.udev
	;;
  esac
fi
}

######################################
# PCMCIA socket detection
# wile_coyote was here
auto_pcmcia() {
if [ "$SOCKET0_DRIVER" ] && ! grep -qw nopcmcia /proc/cmdline; then
    case "$SOCKET0_DRIVER" in yenta_socket|i82365|tcic)
	infobox "Enabling $SOCKET0_DRIVER" "PCMCIA"
	chmod +x /etc/rc.d/rc.pcmcia
	/etc/rc.d/rc.pcmcia $> /dev/null
	sleep 5
    	## Probe again, probably we can catch something new
	infobox "Probing PCMCIA devices ..." "PCMCIA"
	rm -f /etc/sysconfig/vector
	/sbin/vlsetup 1>/dev/null 2>/dev/null
	if [ -f /etc/sysconfig/vector ]; then
	    . /etc/sysconfig/vector
	fi
	;;
    esac
fi
}


###############################################
# IDE-SCSI
enable_ide_scsi()
{
    if cat /etc/rc.d/rc.modules | \
        sed 's!^#/sbin/modprobe ide-scsi!/sbin/modprobe ide-scsi!' > \
        /etc/rc.d/rc.modules.new; then
	mv /etc/rc.d/rc.modules.new /etc/rc.d/rc.modules
	chmod +x /etc/rc.d/rc.modules
    fi
}

# simple, if we found out user has set lilo with ide-scsi,
# we need to enable ide scsi
auto_ide_scsi()
{
    if egrep -q "^[:space:]*append *=.*ide-scsi" /etc/lilo.conf; then
	enable_ide_scsi
    fi    
}

###############################################
# CD, CDWRITER, DVD detection
# Note: does not support OPT subfs for kernel 2.6 yet
#       SCSI naming is trivial (scd0 ...)

# add fstab entry
add_fstab()
{
    mkdir -p /mnt/$MNT
    if grep -qw "/mnt/$MNT" /etc/fstab; then
	if cat /etc/fstab | \
	   sed "s@.*/dev/.*[[:space:]]/mnt/$MNT[[:space:]].*@/dev/$DEV /mnt/$MNT $OPT@" > \
	   /etc/fstab.new; then
	    mv -f /etc/fstab.new /etc/fstab
	fi
    else
    	echo "/dev/$DEV /mnt/$MNT  $OPT" >> /etc/fstab
    fi
}

set_cdwriter()
{
    MNT=$1
    infobox "Setting up $model" "CDWRITER"

    OPT="iso9660  noauto,user,ro   0   0"
    # need ide-scsi for kernel 2.4
    if [ "`kernelversion`" = "2.4" ]; then
	enable_ide_scsi
	# Change DEV to scd
	# This is a hack, based on limited info, not good
	# Please fix it if you have the hardware
	DEV="scd$scd"
	scd=`expr $scd + 1`
    fi
    (cd /dev/; rm -f $MNT; ln -sf $DEV $MNT; chmod a+r $DEV)
    add_fstab "$MNT" "$DEV" "$OPT"
    sleep 2
}


set_cdrom()
{
    MNT=$1
    infobox "Setting up $model" "CDROM"
    (cd /dev/; rm -f $MNT; ln -sf $DEV $MNT; chmod a+r $DEV)
    OPT="iso9660  noauto,user,ro   0   0"
    add_fstab "$MNT" "$DEV" "$OPT"
    sleep 2
}

set_dvd()
{
    MNT=$1
    infobox "Setting up $model" "DVD"
    (cd /dev; rm -f $MNT; ln -s $DEV $MNT; chmod a+r $DEV)
    # what is the fstype of a DVD ?
    OPT="auto  noauto,user,ro   0   0"
    add_fstab "$MNT" "$DEV" "$OPT"
    sleep 2
}

set_cd()
{
    DEV=$1

    # test cdwriter first, we might change the DEV
    if echo $model | egrep -q 'RW |CD-R/RW|CD-RW' ||
	dmesg | grep "$disk:" | egrep -q 'RW |CD-R/RW|CD-RW'; then
        set_cdwriter cdwriter$cdw $DEV $model
	if [ "$cdw" ]; then
	    cdw=`expr $cdw + 1`
	else
	    cdw="1"
	fi
    fi

    # Must be a cd, so just add it
    set_cdrom cdrom$cdr $disk $model
    if [ "$cdr" ]; then	    
    	cdr=`expr $cdr + 1`
    else
	cdr="1"
    fi
    
    # also set as DVD, if it is
    if echo $model | egrep -q 'DVD' ||
       dmesg | grep "$disk:" | egrep -q 'DVD'; then
        set_dvd dvd$dvd $disk $model
	if [ "$dvd" ]; then
	    dvd=`expr $dvd + 1`
	else
	    dvd="1"
	fi
    fi
}

auto_cd() {
cdw=""
cdr=""
dvd=""
scd="0"
## use probedisk if available
if which probedisk &>/dev/null; then
    if probedisk | egrep '(cdrom|cdwriter)' > $freply; then
	while read LINE; do
	    disk=`echo $LINE | cut -f1 -d '|'`
	    disk=${disk##*/}
	    model=`echo $LINE | cut -f3 -d '|'`
	    set_cd $disk $model
	done < $freply
    fi
    return 0
fi

## Use brute force detection
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`
    		set_cd $disk $model
	    fi
	fi
    done
done

}

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

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

# If the mouse is a USB, we can autodetect it:
if [ -r /proc/bus/usb/devices ]; then
 if cat /proc/bus/usb/devices | grep usb_mouse 1> /dev/null 2> /dev/null ; then
    infobox "Setting up USB mouse" "MOUSE"
    ( cd /dev ; rm -f mouse ; ln -sf input/mice mouse )
    sleep 3
    return 0
 fi
fi

infobox "No mouse is detected" "WARNING"
sleep 2
}

#######################################################################
# 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 &) 2>/dev/null
    PID1=$!
    echo "ATZ" > $1
    sleep 3
    kill $PID1
    cat /tmp/modem_test | grep -q "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, but cannot be configured.
If this is a winmodem, please consult 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 
if [ "$NETCARD0_DRIVER" ]; then
    infobox "Setting up $NETCARD0_FULLNAME" "NETWORK CARD"
    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
fi

if [ "$NETCARD1_DRIVER" ]; then
    infobox "Setting up $NETCARD1_FULLNAME" "NETWORK 2"
    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
fi

if [ -z "$NETCARD0_DRIVER" ] && [ -z "$NETCARD1_DRIVER" ]; then
    infobox "No network card detected" "WARNING"
    sleep 2
else
    depmod -A &>/dev/null
    modprobe eth0 &> /dev/null
    modprobe eth1 &> /dev/null
fi
}

auto_wireless()
{
    if which iwconfig &>/dev/null; then
	for DEV in eth0 eth1; do
	    if ! LC_ALL=C iwconfig $DEV 2>&1 | egrep -q \
		'(no wireless extensions|No such device)'; then
		infobox "Turning on wireless support" "WIRELESS"
		chmod +x /etc/rc.d/rc.wireless &> /dev/null
		return 0
	    fi
	done
    fi
}

#############################
# MAIN

probe

. /etc/sysconfig/vector

auto_tmpfs
auto_ide_scsi
auto_pcmcia
auto_cd
auto_mouse
auto_modem
auto_net
auto_wireless
auto_hotplug

clean_exit 0

