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

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
	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
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
	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()
{
    # 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()
{
####Added by Uelsk8s
infobox "Setting up cdrom devices" "CDROM"
#/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` /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

  # 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"
        (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
	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"
            (cd /dev/; rm -f modem; ln -sf $MODEM modem) &>/dev/null
            (cd /lib/udev/devices && rm -f modem && ln -sf $MODEM modem) &>/dev/null
	    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 detected." "Modem"
    sleep 2
  fi
  return 0
}

auto_net() {

    # 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
    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/modprobe.conf | grep -ve "alias *eth0" > /etc/modprobe.d/network.conf
    echo "alias eth0 $NETCARD0_DRIVER" >> /etc/modprobe.d/network.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
    infobox "Setting up $NETCARD1_FULLNAME" "NETWORK 2"
    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.d/network.conf
    echo "alias eth1 $NETCARD1_DRIVER" >> /etc/modprobe.d/network.conf
fi

#/sbin/vinetadd

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

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

    # turn off by default
    chmod -x /etc/rc.d/rc.wireless &> /dev/null
    if which iwconfig &>/dev/null; then
	if iwconfig 2>&1 | grep -e "[^[:alpha:]]" | \
	   grep -v 'no wireless extensions'; then
	    infobox "Turning on wireless support" "WIRELESS"
	    chmod +x /etc/rc.d/rc.wireless &> /dev/null
	    return 0
	fi
    fi
}

###################################################
# return the service status for a runlevel
# $1 = daemon name
# $2 = runlevel
srv_status()
{
   if [ -h /etc/rc.d/rc$2.d/S??$1 ]; then
        echo "on"
   else
        echo "off"
   fi
}

###################################################
#  return 0 if $1 in $2
is_selected() {
  for DD in $2; do
    if [ "$1" = "$DD" ]; then
       return 0;
    fi
  done
  return 1
}

select_services()
{

RUNLEVEL=$1

TITLE="SET SERVICES"
TEXT="\nNow select the services for run level $RUNLEVEL:";
DIMENSION="20 78 12"
[ "$CMD" ] && DIMENSION="20 78 12"

echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --checklist "$TEXT" $DIMENSION \' > $fmenu
ii=0
for PROG in $SERVICE_DIR/*; do
   if [ -f $PROG ]; then
     NAME=`basename $PROG`
     DESCRIPTION=`service_desc $NAME`
     if [ -z "$DESCRIPTION" ]; then
       DESCRIPTION=`grep "description:" $PROG | cut -d : -f 2 | sed -e 's/^ *//g'`
     fi
     if [ -z "$DESCRIPTION" ]; then
       DESCRIPTION="$NAME daemon"
     fi
     # echo -n $NAME $RUNLEVEL
     # srv_status $NAME $RUNLEVEL
     STATE=`srv_status $NAME $RUNLEVEL` 
     echo $NAME \"$DESCRIPTION \" $STATE \\ >> $fmenu
     let ii=ii+1
   fi
done
echo ' 2> $freply ' >> $fmenu

if [ $ii = 0 ]; then
  $DCMD "$BACKTITLE" --title " FATAL ERROR " --msgbox \
"\nStrange ... I could not find any service in $SERVICE_DIR\n
This is not my system. I'm outta here.\n" 0 0
clean_exit 1
fi

# cat $fmenu

. $fmenu

[ $? != 0 ] && return 1

if [ "$CMD" = "" ]; then
  CHOICES=`cat $freply | sed -e 's/"//g'`
else
  CHOICES=`cat $freply | sed -e 's!/! !g'`
fi

# launch another window to start the daemons
#$CMD $0 --activate "$RUNLEVEL" "$CHOICES"

if [ "$CHOICES" ]; then
TEXT="\n
These services have been activated for run level $RUNLEVEL:\n
$CHOICES\n"
else
TEXT="\n
All services have been disabled on level $RUNLEVEL.\n
Enjoy your lightest system."
fi

$DCMD --backtitle "$BACKTITLE" --title "SERVICES ACTIVATED" --msgbox "$TEXT" 0 0
}

select_runlevel()
{
while [ true ]; do
TITLE="SET SERVICES"
TEXT="\n
Linux has four working run levels. On each run\n
level it runs a set of services (background program).\n
You have the oportunity to set the services that will\n
be started for each run level now.\n
Choose a runlevel to start services on
or Next to use the defaults and continue with hardware setup"
DIMENSION="19 60 5"

  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
  --menu "$TEXT" $DIMENSION \
"2" "Text user interface desktop" \
"3" "Text user interface server" \
"4" "Graphical user interface desktop" \
"5" "Graphical user interface server" \
"NEXT" "Exit this menu" \
2> $freply

  [ $? != 0 ] && return 1

  RUNLEVEL=`cat $freply`
  [ $RUNLEVEL = "NEXT" ] && return 0
  
  select_services $RUNLEVEL
done
}

########################################################
vsrviceset()
{
# (c) Eko M. Budi, 2003
# (c) Vector Linux, 2003

SERVICE_DIR="/etc/rc.d/init.d"

# Turn off/on the selected daemons
# This routine is to be called by this script itself
if [ "$1" == "--activate" ]; then
   clear
   STOPPED=""
   STARTED=""
   UNTOUCHED=""
   LIST=$3
   RUNLEVEL=$2
   echo Activating services for run level $RUNLEVEL ....
   for PROG in $SERVICE_DIR/*; do
      NAME=`basename $PROG`
      STATUS=`srv_status $NAME $RUNLEVEL`
      is_selected $NAME "$LIST"
      if [ $? = 0 ]; then
         if [ "$STATUS" = "off" ]; then
            /sbin/service -s $NAME $RUNLEVEL
	 fi
      else
         if [ "$STATUS" = "on" ]; then
            /sbin/service -r $NAME $RUNLEVEL
	 fi
      fi
   done
   sleep 3
   exit 0
fi

}
auto_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/vector
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_wireless
#vsrviceset
#echo Service Configurator is starting ...
#select_runlevel
auto_laptop

clean_exit 0

