#!/bin/sh
# vhwconf : autodetect hardware
# then setup pcmcia, hotplug, cdrom, mouse, modem, network device, wireless
#
# mini version of vlautosetup. Does not setup:
# X-Window
# Network
# Alsa
#
# (c) VLocity Linux, 2003
# Originally by Tony Brijeski (vlautosetup)
# Fixed for VL 4.3 by Robert & wile_coyote 
# Moved to vhwconf for VL 5.x by Eko M. Budi
# Bug hunted with JohnVan, BigPaws, GbHil
#
# released under the GPL2 license 

vdir=`dirname $0`
. $vdir/vasm-functions
. /etc/rc.d/functions-service
SERVICE_DIR="/etc/rc.d/init.d"

check_root
#check_xwindow

############################################
# PROBE FIRST
probe() {
echo "Probing Hardware"
mkdir -p /etc/sysconfig
rm -f /etc/sysconfig/vlocity
echo "SAFE=yes" > /etc/sysconfig/kudzu
/sbin/vlsetup 1>/dev/null 2>/dev/null
if [ ! -f /etc/sysconfig/vlocity ]; then
    clean_exit 1
fi
}

######################################
# TMPFS
# Turn on tmpfs if RAM >= 256 and SWAP >= 256
auto_tmpfs() {

# Skip if notmpfs is defined
if grep -qw notmpfs /proc/cmdline || \
   grep -qe "^ *append *=.*notmpfs" /etc/lilo.conf; then
   return 0
fi

if [ ! -f /etc/rc.d/rc.tmpfs ]; then
    return 0
fi

# disable by default
chmod -x /etc/rc.d/rc.tmpfs

# enable if RAM > 250MB, and SWAP > 250MB
if [ 'kernelversion' = "2.4" ]; then
    LINE=`grep -e "^Mem:" /proc/meminfo`
    [ -z "$LINE" ] && return 0
    MEM=`echo $LINE | cut -f2 -d ' '`

    LINE=`grep -e "^Swap:" /proc/meminfo`
    [ -z "$LINE" ] && return 0
    SWAP=`echo $LINE | cut -f2 -d ' '`
    if [ $MEM -gt 256000000 ] && [ $SWAP -gt 512000000 ]; then
	chmod +x /etc/rc.d/rc.tmpfs
	sleep 2
    else
	chmod -x /etc/rc.d/rc.tmpfs
    fi
else
    LINE=`grep -e "^MemTotal:" /proc/meminfo`
    [ -z "$LINE" ] && return 0
    MEM=`echo $LINE | cut -f2 -d ' '`

    LINE=`grep -e "^SwapTotal:" /proc/meminfo`
    [ -z "$LINE" ] && return 0
    SWAP=`echo $LINE | cut -f2 -d ' '`
    if [ $MEM -gt 256000 ] && [ $SWAP -gt 512000 ]; then
	chmod +x /etc/rc.d/rc.tmpfs
	sleep 2
    else
	chmod -x /etc/rc.d/rc.tmpfs
    fi
fi

}


######################################
# PCMCIA socket detection
# wile_coyote was here
auto_pcmcia() {
echo "probing pcmcia"
# Skip if nopcmcia is defined 
if grep -qw nopcmcia /proc/cmdline || \
   grep -qe "^ *append *=.*nopcmcia" /etc/lilo.conf; then
    return 0
fi
# Ok, here we go
chmod -x /etc/rc.d/rc.pcmcia
chmod -x /etc/rc.d/rc.apm
if [ "$SOCKET0_DRIVER" ] ; then
    case "$SOCKET0_DRIVER" in yenta_socket|i82365|tcic)
	echo "Enabling 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
	rm -f /etc/sysconfig/vlocity
	/sbin/vlsetup 1>/dev/null 2>/dev/null
	if [ -f /etc/sysconfig/vlocity ]; then
	    . /etc/sysconfig/vlocity
	fi
	;;
    esac
    chmod +x /etc/rc.d/rc.apm
fi
}

######################################
# HOTPLUG
auto_hotplug() {
# Skip if nohotplug is defined 
if grep -qw nohotplug /proc/cmdline || \
   grep -qe "^ *append *=.*nohotplug" /etc/lilo.conf; then
    return 0
fi
# disabled it by default
chmod -x /etc/rc.d/rc.hotplug
chmod -x /etc/rc.d/rc.udev

if [ -w /proc/sys/kernel/hotplug ]; then
#    infobox "Enabling USB/PCI hotplug" "HOTPLUG"
#    chmod +x /etc/rc.d/rc.hotplug
    case `kernelversion` in
	2.6*)
	    chmod +x /etc/rc.d/rc.udev
	    #/etc/rc.d/rc.udev start
	;;
    esac
fi

}

###############################################
# IDE-SCSI
enable_ide_scsi()
{
    # Note, the new rc.modules does not need this
    # but this won't hurt
    sed -i \
    's!^#/sbin/modprobe ide-scsi!/sbin/modprobe ide-scsi!' \
    /etc/rc.d/rc.modules
    chmod +x /etc/rc.d/rc.modules
}

# simple, if we found out user has set lilo with ide-scsi,
# we need to enable ide scsi
auto_ide_scsi()
{
    if grep -q "ide-scsi" /proc/cmdline; then
	enable_ide_scsi
	modprobe ide-scsi &> /dev/null
    elif grep -e "^ *append *=.*ide-scsi" /etc/lilo.conf &>/dev/null; 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 ...)

set_cd()
{
#/sbin/vsetcdrom >/dev/null
if ! cat /etc/fstab | grep -wq "/dev/cdrom" ; then
   cat /etc/fstab | \
   sed -e "/# CDROM,/a /dev/cdrom	/mnt/cdrom	iso9660		users,noauto,ro" > /etc/fstab.new
   mv -f /etc/fstab.new /etc/fstab
   rm fstab.new 2> /dev/null
fi

chmod +s /usr/bin/smbmnt 2>/dev/null
chmod +s /usr/sbin/pppd 2>/dev/null
ln -s $(which smbspool 2>/dev/null) /usr/lib/cups/backend/smb 2>/dev/null

}



###############################################
# Mouse detection
auto_mouse() {
    # Skip if nomouse is defined (Who will disable this ?)
    if grep -qw nomouse /proc/cmdline 2>/dev/null || \
	grep -qe "^ *append *=.*nomouse" /etc/lilo.conf &>/dev/null 2>&1; then
	return 0
    fi

    # use external now ...
    $vdir/vmouseset --auto
}

#######################################################################
# 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 2>/dev/null
    sleep 3
    kill $PID1 &> /dev/null
    cat /tmp/modem_test | grep -q "OK"
    RESULT=$?
    rm -f /tmp/modem_test
    return $RESULT
}

auto_modem()
{

    # Skip if nomodem is defined
    if grep -qw nomodem /proc/cmdline || \
	grep -qe "^ *append *=.*nomodem" /etc/lilo.conf &>/dev/null; then
	return 0
    fi
  echo "Probing for 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
	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}"
        (cd /dev/; rm -f modem; ln -sf $MODEM modem) &>/dev/null
        (cd /lib/udev/devices && rm -f modem && ln -sf $MODEM modem) &>/dev/null
	mk_rc_serial > /etc/rc.d/rc.serial
        chmod +x /etc/rc.d/rc.serial
	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"
            (cd /dev/; rm -f modem; ln -sf $MODEM modem) &>/dev/null
            (cd /lib/udev/devices && rm -f modem && ln -sf $MODEM modem) &>/dev/null
	    return 0    
	fi
    fi
  done
}

auto_net() {
    echo "Probing for Ethernet"
    # Skip if nonetwork is defined
    if grep -qw nonetwork /proc/cmdline || \
	grep -qe "^ *append *=.*nonetwork" /etc/lilo.conf &>/dev/null; then
	return 0
    fi

# Set up eth0 and eth1 
if [ "$NETCARD0_DRIVER" ]; then
 if [ "$NETCARD0_DRIVER" = "sk98lin" ]; then
	 NETCARD0_DRIVER=skge
 fi
    if [ -f /etc/rc.d/rc.netdevice ]; then
	rm -f /etc/rc.d/rc.netdevice
    fi
    cat /etc/modprobe.conf | grep -ve "alias *eth0" > /etc/modprobe.conf
    echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modprobe.conf
    if [ ! -h /etc/modules.conf ]; then
	cat /etc/modules.conf | grep -ve "alias *eth0" > /etc/modules.conf
	echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modules.conf
    fi
fi

if [ "$NETCARD1_DRIVER" ]; then
 if [ "$NETCARD1_DRIVER" = "sk98lin" ]; then
	 NETCARD1_DRIVER=skge
 fi
    if [ ! -h /etc/modules.conf ]; then
	cat /etc/modules.conf | grep -ve "alias *eth1 " > /etc/modules.conf
	echo "alias eth1 $NETCARD1_DRIVER" >> /etc/modules.conf
    fi
    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
    echo "No network card detected" "WARNING"
else
    depmod -A &>/dev/null
    modprobe $NETCARD0_DRIVER &> /dev/null
    modprobe $NETCARD1_DRIVER &> /dev/null
fi
}

auto_laptop()
{
echo "Checking for laptop"
##check cardbus for laptop?
if /sbin/lspci|grep -i cardbus >/dev/null;then
 if /sbin/lspci|grep -i cardbus|grep -i Toshiba >/dev/null;then
  echo "/sbin/modprobe toshiba" >>/etc/rc.d/rc.modules
 elif modprobe thinkpad_acpi &>/dev/null;then
  echo "/sbin/modprobe thinkpad_acpi" >>/etc/rc.d/rc.modules
 elif modprobe msi_laptop &>/dev/null;then
  echo "/sbin/modprobe msi_laptop" >>/etc/rc.d/rc.modules
 elif /sbin/lspci -v|grep -i Sony >/dev/null;then
  echo "/sbin/modprobe sony_laptop" >>/etc/rc.d/rc.modules
 fi
fi
}

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

probe

. /etc/sysconfig/vlocity
chmod -x /etc/rc.d/rc.tmpfs
#auto_tmpfs
#auto_pcmcia
auto_hotplug
auto_ide_scsi
#auto_cd
set_cd
#auto_mouse
auto_modem
auto_net
auto_laptop
clean_exit 0

