#!/bin/sh
#
# checkme $Revision: 1.14 $ $Date: 1995/04/07 20:50:45 $ (David Hinds)
#
#=======================================================================

if [ ! -r make.options ] ; then
  echo "Could not read make.options!"
  echo "Please run this script from the main PCMCIA source directory."
  exit 1
fi

rm -f .prereq.ok xtra.options

echo \
  "# Do not edit this file.  It is automatically created by 'make prereq'." \
  > xtra.options
echo "# " >> xtra.options

echo "Checking directories..."

LINUX=`grep LINUX make.options | sed -e 's/.* \(.*\)$/\1/'`

if [ ! -d ${LINUX} ] ; then
  echo "  Linux source directory $LINUX not found!"
  echo "    This package requires that you install a complete Linux kernel"
  echo "    source tree.  See the HOWTO for a list of FTP sites."
  exit 1
fi
echo "  Linux source directory $LINUX is OK."

if [ ! -d /var/run ] ; then
  echo "  Warning: /var/run not found."
  echo "    To fix, create an empty directory with this name."
else
  echo "  /var/run exists."
fi

#=======================================================================

echo ""
echo "Checking kernel version..."

if [ ! -f $LINUX/include/linux/version.h ] ; then
  echo "  $LINUX/include/linux/version.h does not exist!"
  echo "    This file is created by a normal kernel build, but may later be"
  echo "    deleted.  To recreate, do 'make include/linux/version.h' in your"
  echo "    Linux kernel source tree."
  exit 1
fi

REL=`grep UTS_RELEASE $LINUX/include/linux/version.h | \
     sed -e 's/.*\"\(.*\)\".*/\1/'`
if [ "$REL" = "" ] ; then
  echo "  $LINUX/include/linux/version.h is incomplete!"
  echo "    Please continue installing the PCMCIA package after completing"
  echo "    a successful kernel build."
  exit 1
fi
CUR=`uname -r`
echo "  Source tree is version $REL, current kernel is $CUR"
if [ $REL != $CUR ] ; then
  echo "  Warning: current kernel does not match source tree."
  echo "    Be sure to install your new kernel and reboot before using the"
  echo "    PCMCIA tools."
else
  VER=`grep UTS_VERSION $LINUX/include/linux/version.h | \
       sed -e 's/.*\"\(.*\)\".*/\1/'`
  CUR=`uname -v`
  if [ "$VER" = "$CUR" ] ; then
    echo "  Kernel build date matches source tree build date."
  else
    echo "  Warning: kernel build date does not match source tree build date."
  fi
fi

echo $REL | awk -F. '{
  if ( $1 < 1 ) exit 1
  if ( ($1 == 1) && ($2 < 1) ) exit 1
  if ( ($1 == 1) && ($2 == 1) && ($3 < 93) ) exit 1
  }'
if [ $? -eq 0 ] ; then
  echo "  Your kernel is recent enough."
else
  echo "  Your kernel is out of date.  Upgrade to 1.1.93 or higher."
  echo "    See the HOWTO for a list of FTP sites for current kernel sources."
  exit 1
fi

MODPATH=/lib/modules/$REL

if [ -d $MODPATH ] ; then
  echo "  Module directory $MODPATH exists."
else
  echo "  Module directory $MODPATH does not exist!"
  echo "    To fix, run 'make modules_install' in ${LINUX}."
  exit 1
fi

#=======================================================================

echo ""
echo "Checking kernel configuration..."

if [ ! -r ${LINUX}/.config ] ; then
  echo "  Could not read ${LINUX}/.config!"
  echo "    This is normally created by running 'make config' in your Linux"
  echo "    source tree.  Try re-configuring and re-installing your kernel."
  exit 1
fi

# Load kernel configuration variables
. ${LINUX}/.config

ok=1
if [ ${CONFIG_INET:-n} = "n" ] ; then

  echo "  Warning: CONFIG_INET is not set.  Networking is disabled."
  echo "    To use a PCMCIA network device, you should configure your kernel"
  echo "    with networking enabled."
  ok=0

else

  echo "  Networking support is enabled."
  if [ ${CONFIG_NETDEVICES:-n} = "y" ] ; then

    nm $MODPATH/net/8390.o 2>/dev/null | grep -q _kernel_version
    if [ $? -ne 0 ] ; then
      echo "  8390.o was not compiled as a loadable module!"
      echo "    This will interfere with some PCMCIA ethernet cards.  To fix,"
      echo "    re-configure your kernel and disable all normal ethernet card"
      echo "    drivers, and do 'make modules' and 'make modules_install'."
      for card in WD80x3 EL2 NE2000 HPLAN HPLAN_PLUS ULTRA E2100 AC3200
      do
	if [ `eval echo \\${CONFIG_$card:-n}` = "y" ] ; then
	  echo "    Re-configure with CONFIG_$card disabled."
	fi
      done
      ok=0
    fi

  fi

fi

if [ ${CONFIG_SCSI:-n} = "y" ] ; then

  echo "  SCSI support is enabled."
  echo "# CONFIG_SCSI set in ${LINUX}/.config" >> xtra.options
  if [ ${CONFIG_MODVERSIONS:-n} = "y" ] ; then
    echo "  Warning: CONFIG_MODVERSIONS is set!"
    echo "    The current versions of the Qlogic and Bus Toaster drivers will"
    echo "    not work with this option turned on.  To fix, re-configure your"
    echo "    kernel with this option disabled."
    ok=0
  fi
  if [ ${CONFIG_SCSI_QLOGIC:-n} = "y" ] ; then
    echo "  Warning: CONFIG_SCSI_QLOGIC is set!"
    echo "    This may interfere with use of a Qlogic FastSCSI adapter."
    echo "    To fix, re-configure your kernel with this option disabled."
    ok=0
  fi
  if [ ${CONFIG_SCSI_AHA152X:-n} = "y" ] ; then
    echo "  Warning: CONFIG_SCSI_AHA152X is set!"
    echo "    This may interfere with use of a Bus Toaster SCSI adapter."
    echo "    To fix, re-configure your kernel with this option disabled."
    ok=0
  fi
  if fgrep -q PCMCIA ${LINUX}/drivers/scsi/aha152x.c ; then
    echo "  ${LINUX}/drivers/scsi/aha152x.c is OK."
    echo "# BAD_TOAST is disabled" >> xtra.options
  else
    echo "  ${LINUX}/drivers/scsi/aha152x.c is out of date."
    echo "    The Bus Toaster SCSI driver requires a patched aha152x.c file."
    echo "    To fix, install aha152x.patch from cb-iris.stanford.edu, or"
    echo "    upgrade to a 1.2.4 or higher kernel."
    echo "BAD_TOAST=y" >> xtra.options
  fi
  d="$CONFIG_BLK_DEV_SD$CONFIG_CHR_DEV_ST$CONFIG_BLK_DEV_SR$CONFIG_CHR_DEV_SG"
  if [ "$d" = "" ] ; then
    echo "  No top-level SCSI drivers have been configured!"
    echo "    To fix, configure your kernel with whichever top-level SCSI"
    echo "    drivers (disk, tape, cdrom, generic) you expect to use."
    exit 1
  else
    echo "  The following SCSI devices are configured:\c"
    if [ "$CONFIG_BLK_DEV_SD" = "y" ] ; then echo " disk\c" ; fi
    if [ "$CONFIG_CHR_DEV_ST" = "y" ] ; then echo " tape\c" ; fi
    if [ "$CONFIG_BLK_DEV_SR" = "y" ] ; then echo " cdrom\c" ; fi
    if [ "$CONFIG_CHR_DEV_SG" = "y" ] ; then echo " generic\c" ; fi
    echo ""
  fi

else

  echo "  SCSI support is disabled."
  echo "# CONFIG_SCSI is disabled" >> xtra.options

fi

if [ ${CONFIG_APM:-n} = "y" ] ; then
  echo "  APM support is enabled."
  if grep -q apm ${LINUX}/kernel/ksyms.c ; then
    echo "    APM revision level is OK."
    echo "# CONFIG_APM set in ${LINUX}/.config" >> xtra.options
  elif grep -q apm_syms ${LINUX}/drivers/char/apm_bios.c ; then
    echo "    APM revision level is OK."
    echo "# CONFIG_APM set in ${LINUX}/.config" >> xtra.options
  else
    echo "    Your APM package is not PCMCIA-compatible.  To fix, you should"
    echo "    either re-configure your kernel with APM disabled, or install"
    echo "    the updated APM patch available from the Linux PCMCIA ftp sites."
    echo "CONFIG_APM=" >> xtra.options
  fi
else
  echo "  APM support is disabled."
  echo "# CONFIG_APM is disabled" >> xtra.options
fi

if [ ${CONFIG_PCI:-n} = "y" ] ; then
  echo "  PCI support is enabled."
  echo "# CONFIG_PCI set in ${LINUX}/.config" >> xtra.options
else
  echo "  PCI support is disabled."
  echo "# CONFIG_PCI is disabled" >> xtra.options
fi

if [ $ok -eq 1 ] ; then
  echo "  Configuration options are OK."
else
  echo "  Your kernel configuration is not really OK."
fi

#=======================================================================

echo ""
echo "Checking module utilities..."

if [ ! -x /sbin/insmod -o ! -x /sbin/rmmod ] ; then
  echo "Module utilities (insmod, rmmod) missing from /sbin!"
  echo "  To fix, you should build and install the latest set of module tools,"
  echo "  available from FTP sites listed in the HOWTO.
  exit 1
fi

/sbin/insmod 2>&1 | grep -q 'sym=value'
if [ $? -ne 0 ] ; then
  echo "Module utilities are out of date!"
  echo "  To fix, you should build and install the latest set of module tools,"
  echo "  available from FTP sites listed in the HOWTO.
  exit 1
fi

echo "  Module utilities are OK."

#=======================================================================

echo ""
echo "Checking optional stuff..."

if [ -r /usr/lib/libforms.a ] ; then
  echo "  /usr/lib/libforms.a found."
  echo "HAS_FORMS=y" >> xtra.options
else
  echo "  /usr/lib/forms.a not found."
  echo "    If you want to build the 'cardinfo' PCMCIA control panel, you need"
  echo "    to install the Forms Library.  See the HOWTO for details."
  echo "# HAS_FORMS is disabled" >> xtra.options
fi

#=======================================================================

echo ""
echo "System setup is OK."
touch .prereq.ok
exit 0
