#! /bin/sh
MODBASE=`uname -r`
CC=gcc
SRCDIR=/usr/src/linux-`uname -r`
SRCDIRS=`echo /usr/src/*`
PCMCIA=/usr/src
KVER=`uname -r|cut -d'.' -f2`

########################################
# Find kernel sources                  #
########################################
find_source(){
    for i in ${SRCDIRS} 
    do
	if [ -d $i ]
	then 
	    LDIR=$i
	    if [ -d ${i}/Documentation ]
	    then
		SRCDIR=${LDIR}
		break 
	    fi
	else
	    SRCDIR=""
	fi
    done
}

###############################################
# Set pcmcia source installation  path        #
###############################################

pcmcia_src(){
    MDIR=""

    while [ ! -d ${MDIR}/wireless ]
    do    
      echo -n "Please enter the path where pcmcia-cs is installed: "
      read MDIR
      if [ ${MDIR}H = "H" ]
      then 
         echo "No path entered installation aborted"
         exit 1
      fi

      if [ -d ${MDIR}/wireless ]
      then
  	PCMCIA=${MDIR}
      else
	echo ${MDIR} " appears to be the incorrect path to the pcmcia-cs package";
      fi
   done
}



ask4src(){
    echo -n "Please enter the path to your kernel sources: "
    read SRCDIR
    if [ ${SRCDIR}H = "H" ]
    then
      echo "Module compilation aborted."
      exit 1;
    fi
}


find_source

if [ ${SRCDIR}X = X ] 
then
    while [ ! -d ${SRCDIR}/Documentation ]
	do
	    echo "Cannot locate source in ${SRCDIR} "
	    ask4src
	done
fi


##################################################
# Look for 2.4 Series kernel for koala driver    #
# build is different on 2.2 kernel with pcmcia-cs#
# installed                                      #
##################################################

if [ ${KVER} = '4' ]
then

    echo "Compiling driver modules using sources in ${SRCDIR} .."

    KCMP='${CC} -MD  -O2 -Wall -Wstrict-prototypes -pipe -I${SRCDIR}/include -D__KERNEL__ -DMODULE -c cb20_cb.c'
else
    pcmcia_src
    KCMP='${CC} -MD  -O2 -Wall -Wstrict-prototypes -pipe -I${SRCDIR}/include -I${PCMCIA}/include  -D__KERNEL__ -DMODULE -c cb20_cb.c'
    echo "Compiling driver modules using sources in ${SRCDIR} .."
fi

eval ${KCMP}


cp cb20_cb.o /lib/modules/${MODBASE}/pcmcia
/sbin/depmod
