#!/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) 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() {

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

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

# enable if RAM > 250MB, and SWAP > 250MB
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 268435450 ] && [ $SWAP -gt 268435450 ]; then
	infobox "Enabling tmpfs for fast RAM based /tmp" "TMPFS"
	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() {

# 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)
	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
    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*)
	    infobox "Enabling UDEV for kernel 2.6" "UDEV"
	    sleep 2
	    chmod +x /etc/rc.d/rc.udev
	    /etc/rc.d/rc.udev
	;;
    esac
    # Launch the hotplug
    #/etc/rc.d/rc.hotplug start
    #sleep 2
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()
{
    # skip if it is not defined
    if grep -q "ide-scsi" /proc/cmdline ||
       grep -qe "^ *append *=.*ide-scsi" /etc/lilo.conf; then
	enable_ide_scsi
	modprobe 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()
{
    DEV=$1
    MNT=cdwriter$mcd
    infobox "Setting up $model" "CDWRITER"
    case `kernelversion` in
	2.6)
	    OPT="subfs    fs=auto,user,rw  0 0"
	    ;;
	*)
	    OPT="iso9660  noauto,user,rw   0   0"
	    ;;
    esac
    (cd /dev/; rm -f $MNT; ln -sf $DEV $MNT; chmod a+rw $DEV)
    add_fstab "$DEV" "$MNT" "$OPT"
    sleep 2
}

set_cdrom()
{
    DEV=$1
    MNT=cdrom$mcd
    infobox "Setting up $model" "CDROM"
    case `kernelversion` in
	2.6)
	    OPT="subfs    fs=auto,user,ro  0 0"
	    ;;
	*)
	    OPT="iso9660  noauto,user,ro   0 0"
    esac
    (cd /dev/; rm -f $MNT; ln -sf $DEV $MNT; chmod a+r $DEV)
    OPT="iso9660  noauto,user,ro   0   0"
    add_fstab "$DEV" "$MNT" "$OPT"
    sleep 2
}

set_dvd()
{
    DEV=$1
    MNT=dvd$mcd
    infobox "Setting up $model" "DVD"
    # HACK ME, need proper fs for DVD
    case `kernelversion` in
	2.6)
	    OPT="subfs    fs=auto,user,ro  0 0"
	    ;;
	*)
	    OPT="auto     noauto,user,ro   0 0"
    esac
    (cd /dev; rm -f $MNT; ln -s $DEV $MNT; chmod a+r $DEV)
    add_fstab "$DEV" "$MNT" "$OPT"
    sleep 2
}

## This detects a cd, dvd or cdrom
## The whole things become complicated because of ide-scsi !
set_cd()
{
    # if this kernel has been booted with ide-scsi
    # that's mean cdwriter is requested
    if echo "$model $model1" | egrep -q 'RW|rw|writer' ; then
	if grep -q "${disk}=ide-scsi " /proc/cmdline; then
	    modprobe ide-scsi
	    enable_ide_scsi
	    LINE=`dmesg | grep "^Attached scsi CD-ROM`
	    disk=`echo $LINE | cut -f 4 -d ' '`
	elif grep -qe "^ *append *${disk}=ide-scsi" /etc/lilo.conf; then
	    enable_ide_scsi
	    # guessing
	    disk="scd$scd"
	    scd=`expr $scd + 1`
	fi
        set_cdwriter $disk
    fi

    # Set as CD
    if echo "$model $model1" | egrep -qE '(CD|cd)' ; then
        set_cdrom $disk
    fi
    
    # also set as DVD, if it is
    if echo "$model $model1" | egrep -qE '(DVD|dvd)' ; then
        set_dvd $disk
    fi
    
    if [ "$mcd" ]; then
	mcd=`expr $mcd + 1`
    else
        mcd="1"
    fi
}

auto_cd() {
mcd=""
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 '|'`"
	    model1="`dmesg | grep ${disk}:`"
	    set_cd $disk $model $model1
	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() {
    # Skip if nomouse is defined (Who will disable this ?)
    if grep -qw nomouse /proc/cmdline || \
	grep -qe "^ *append *=.*nomouse" /etc/lilo.conf; 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
    sleep 3
    kill $PID1
    cat /tmp/modem_test | grep -q "OK"
    RESULT=$?
    rm /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; then
	return 0
    fi

  # 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() {

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

# 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()
{
    # Skip if nowireless is defined
    if grep -qw nowireless /proc/cmdline || \
	grep -qe "^ *append *=.*nonetwork" /etc/lilo.conf; then
	return 0
    fi

    # turn off by default
    chmod -x /etc/rc.d/rc.wireless &> /dev/null
    if which iwconfig &>/dev/null; then
	for DEV in eth0 eth1 eth2; 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_pcmcia
auto_hotplug
auto_ide_scsi
auto_cd
auto_mouse
auto_modem
auto_net
auto_wireless

clean_exit 0

