#!/bin/bash
# bash script to install or uninstall hsfpci modem driver
# uses "make install" in modem driver conexant-192-1ubuntu-1

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
SRCD=/usr/share/conexant-192-1ubuntu
KDIR=/lib/modules/`uname -r`

do_root() {
# if not root, echo
if [ ! $USER = root ] ; then
cat <<ROOTONLY
You must be the ROOT user to execute modem-slmodem
ROOTONLY
exit 1
fi
}

do_help() {
cat <<HELP
Usage: modem-hsf [OPTION]
OPTIONS are:
   -i or --install      < install the driver >
   -u or --uninstall    < uninstall the driver >
   -h or --help         < list this message >
Example:  modem-hsf --install
HELP
}

do_install() {
  # arrive here to install the driver

  # first check and make sure backups exist, if not, create them:
  [ -f $SRCD/Makefile.orig ] || cp -vp $SRCD/Makefile $SRCD/Makefile.orig
  [ -f $SRCD/inf/Makefile.orig ] || cp -vp $SRCD/inf/Makefile $SRCD/inf/Makefile.orig
  [ -f $SRCD/inf/linux_hsfi.inf.orig ] || cp -vp $SRCD/inf/linux_hsfi.inf $SRCD/inf/linux_hsfi.inf.orig
  [ -f $SRCD/inf/preprocess.sh.orig ] || cp -vp $SRCD/inf/preprocess.sh $SRCD/inf/preprocess.sh.orig
  [ -f $SRCD/modules/Makefile.orig ] || cp -vp $SRCD/modules/Makefile $SRCD/modules/Makefile.orig
  [ -f $SRCD/modules/serial_hsf.c.orig ] || cp -vp $SRCD/modules/serial_hsf.c $SRCD/modules/serial_hsf.c.orig

  # for ubuntu, the kernel headers are in /lib/modules/`uname -r`/build, not /usr/src/linux, so change Makefile
  cat $SRCD/modules/Makefile.orig | sed -e 's/^#.*KERNELDIR ?= \/lib\/modules\/$$(uname -r)\/build/        KERNELDIR ?= \/lib\/modules\/$$(uname -r)\/build/g' | sed -e 's/^.*KERNELDIR ?= \/usr\/src\/linux/#       KERNELDIR ?= \/usr\/src\/linux/g' > $SRCD/modules/Makefile

  # set initial values to null
  VendorID="none"
  DeviceID="none"
  Country="none"

  # first, let's try to determine the proper Vendor:Device ID's for this HSF
  # if .conf file exists, use it, if not try lspci

  if [ -f /etc/modem-hsfpci/modem-hsfpci.conf ] ; then
	. /etc/modem-hsfpci/modem-hsfpci.conf
  fi

  # if the modem-hsfpci.conf file did NOT specify vendor:device, try here
  if [ "$VendorID" = "none" ] || [ "$DeviceID" = "none" ] ; then
    if ( lspci | grep -i hsf ) ; then
	VendorID=$(lspci -n -s `lspci | grep -i hsf | awk '{ print $1}' | cut -c 6-10` | awk '{ print $3 }' | cut -c 1-4)
	DeviceID=$(lspci -n -s `lspci | grep -i hsf | awk '{ print $1}' | cut -c 6-10` | awk '{ print $3 }' | cut -c 6-9)
    elif ( lspci | grep -i modem ) ; then
	VendorID=$(lspci -s `lspci | grep -i modem | cut -d' ' -f1 | cut -c6-` -n | cut -d' ' -f3 | cut -d: -f1)
	DeviceID=$(lspci -s `lspci | grep -i modem | cut -d' ' -f1 | cut -c6-` -n | cut -d' ' -f3 | cut -d: -f2)
    else

	 cat <<FAILVALUES
modem-hsf cannot detect the vendor:device values.  Please enter them
in /etc/modem-hsfpci/modem-hsfpci.conf by uncommenting the line that
represents your modem's value.
FAILVALUES

	exit 1
    fi
  fi

  # next, let's set the country code, use USA if none is set in /etc/modem-hsfpci/modem-hsfpci.conf
  if [ "$Country" = "none" ] ; then
	Country=USA
  fi
  CountryCode=$(grep "$Country" $SRCD/inf/linux_hsfi.inf.orig -B1 | head -n1 | sed -e 's/^.*T35CODE,1,//g')

  echo
  echo VendorID is $VendorID  DeviceID is $DeviceID  Country is "$Country"  CountryCode is "$CountryCode"
  echo

  # OK, now we can set our specific parameters for the above Country, Vendor and Device

  # in inf directory:
  cat $SRCD/Makefile.orig | sed -e "s/14f1-2f00/$VendorID-$DeviceID/g" > $SRCD/Makefile
  cat $SRCD/inf/Makefile.orig | sed -e "s/BINS := linux_hsfi-14f1-2f00.bin linux_hsfi-8086-24c6.bin linux_hsfi-14f1-2f30.bin/BINS := linux_hsfi-14f1-2f00.bin/g" | sed -e "s/14f1/$VendorID/g;s/2f00/$DeviceID/g" > $SRCD/inf/Makefile
  cat $SRCD/inf/linux_hsfi.inf.orig | sed -e "s/VEN_14F1&DEV_2F00&SUBSYS_200214F1/VEN_$VendorID\&DEV_$DeviceID\&SUBSYS_00000000/g" > $SRCD/inf/linux_hsfi.inf
  cat $SRCD/inf/preprocess.sh.orig | sed -e "s/hsfconf_countryT35c=\"16,00\"/hsfconf_countryT35c=\"$CountryCode\"/g" > $SRCD/inf/preprocess.sh
  # in modules directory:
  cat $SRCD/modules/serial_hsf.c.orig | sed -e "s/0x14f1, 0x2f00, PCI_ANY_ID/0x$VendorID, 0x$DeviceID, PCI_ANY_ID/g" > $SRCD/modules/serial_hsf.c

  # test if they exist, if not make the install directories
  [ -d /etc/hsf ] || mkdir -p /etc/hsf
  [ -d $KDIR/misc ] || mkdir -p $KDIR/misc
  [ -d /etc/modutils ] || mkdir -p /etc/modutils

  # now let's enter the source directory and make the inf and modules and then copy them
  cd $SRCD && make clean && make && \
  echo Installing modules:
  cp -fv modules/{hsfbasic2.ko,hsfengine.ko,hsfich.ko,hsfosspec.ko,hsfserial.ko} $KDIR/misc && \
  cp -fv inf/linux_hsfi-$VendorID-$DeviceID.bin /etc/hsf/nvram.bin && \
  cp -fv inf/hsf /etc/modutils/hsf
  echo Country is $Country > /etc/hsf/"$Country"
  echo "Vendor ID is $VendorID and Device ID is $DeviceID" > /etc/hsf/$VendorID.$DeviceID
}

do_depmod() {
  depmod
}

do_uninstall() {
  lsmod | grep hsfser && modprobe -vr hsfserial
  lsmod | grep hsfeng && modprobe -vr hsfengine 
  lsmod | grep hsfbas && modprobe -vr hsfbasic2
  lsmod | grep hsfoss && modprobe -vr hsfosspec
  lsmod | grep hsfich && modprobe -vr hsfich
  echo Removing modules:
  rm -fv $KDIR/misc/{hsfbasic2.ko,hsfengine.ko,hsfich.ko,hsfosspec.ko,hsfserial.ko}
  rm -frv /etc/hsf
  rm -fv /etc/modutils/hsf
  cd $SRCD && make clean
}

case "$1" in
  -i|--install)
	do_root
	do_uninstall
	do_install
	do_depmod
	# now it's all installed, let's load the modules, which creates /dev/ttySHSF0 and udev rules create /dev/modem
	modprobe -v hsfserial
	echo
	echo "Modem driver should be installed and available on /dev/modem."
	echo "If not, your modem may not be supported."
	echo
        exit 0
        ;;
  -u|--uninstall)
	do_root
	do_uninstall
	do_depmod
        exit 0
        ;;
  -h|--help)
	do_root
	do_help
        exit 0
        ;;
  *)
        echo "Usage: modem-hsf --help"
        exit 1
        ;;
esac

exit 0

