#!/bin/bash

UNAME_MINUS_R=`echo $LINUX_SRC | sed 's/^linux-//'`

if [ -f $BUILD/linux/.config ] ; then
        CONFIGURE_OPTIONS="`md5sum $BUILD/linux/.config` $INTERSIL"
else
        # kernel config file is missing, so we need to
        # run the configuration again next time.
        CONFIGURE_OPTIONS="`date` $INTERSIL"
fi

if [ "$PCMCIA_DRIVERS" = "kernel" ] ; then
        KERNELOPT="PCMCIA CARDBUS $PCMCIA_NETWORK_DRIVERS $PCMCIA_DISK_DRIVERS"
else
        KERNELOPT_REMOVE="PCMCIA CARDBUS"
fi

UNTAR_OPTIONS="$UNTAR_OPTIONS $KERNELOPT"

function ml_configure() {
        # fail if the kernel has not been configured.
        if [ ! -f  $BUILD/linux/.config ] ; then
                echo "Cannot find kernel config file???"
		exit 1
        fi
	cd $PACKAGE/$PACKAGE

	# do 'make clean' to ensure any old dependency files are gone
	make clean

	ln -sf ../pcmcia/config.h include/linux
	ln -sf ../pcmcia/version.h include/linux
	ln -sf ../pcmcia/compile.h include/linux
	ln -sf ../linux include/static
        perl -pi -e 's/write_bool HAS_FORMS/HAS_FORMS=n ; write_bool HAS_FORMS/' Configure
	./Configure -n --kernel=$BUILD/linux --target=$ROOT --nocardbus --srctree

	# if Intersil is configured, we need to disable ISA support in cardmgr....
	# something about resource conflicts....
	if [ "$INTERSIL" = "yes" ] ; then
	    perl -i.bak -pe 's/^.*CONFIG_ISA.*$/#undef CONFIG_ISA/' include/pcmcia/config.h
	fi
}

function ml_compile() {
	make CC="$CC" CFLAGS="$CFLAGS" -C $PACKAGE/$PACKAGE all
	make CC="$CC" CFLAGS="$CFLAGS" -C $PACKAGE/$PACKAGE/modules all
	make CC="$CC" CFLAGS="$CFLAGS" -C $PACKAGE/$PACKAGE/cardmgr probe
}

# pcmcia_install_disk
# Install drivers and configuration files for pcmcia ide disks
# Currently, pcmcia ide disks are only working with pcmcia-cs drivers
# This assumes that they are present.
function pcmcia_install_disk() {
    # install our custom ide start script
    install -D -m 755 $BUILD/$PACKAGE/ide.start $ROOT/etc/pcmcia/ide

    if [ "$PCMCIA_DRIVERS" = "pcmcia-cs" ] ; then
            for i in $PCMCIA_DISK_DRIVERS ; do
		if [ -f clients/$i.o ] ; then
                    cp -a clients/$i.o $ROOT/lib/modules/$UNAME_MINUS_R/pcmcia
		fi
            done
    fi
}

# pcmcia_install_net
# Install drivers and configuration files for pcmcia network cards
function pcmcia_install_net() {
    # create our custom network start script
    install -D -m 755 $BUILD/$PACKAGE/network.start $ROOT/etc/pcmcia/network

    if [ "$PCMCIA_DRIVERS" = "pcmcia-cs" ] ; then
	    # network drivers, either in modules/ or wireless/
	    for i in $PCMCIA_NETWORK_DRIVERS ; do
		if [ -f modules/$i.o ] ; then
		    cp -a modules/$i.o $ROOT/lib/modules/$UNAME_MINUS_R/pcmcia
		elif [ -f wireless/$i.o ] ; then
		    cp -a wireless/$i.o $ROOT/lib/modules/$UNAME_MINUS_R/pcmcia
		elif [ -f clients/$i.o ] ; then
		    cp -a clients/$i.o $ROOT/lib/modules/$UNAME_MINUS_R/pcmcia
		fi
	    done
    fi
}

function ml_installroot() {
        # fool the stupid kernel config tester
	cd $PACKAGE/$PACKAGE
        echo "CHECK=" >> .config.out

	# make sure the right directories are there
	for i in sbin etc/pcmcia ; do
	    mkdir -p $ROOT/$i
	done

	# make sure that the modules directory is there no matter what,
	# since other packages may install modules.
	mkdir -p $ROOT/lib/modules/$UNAME_MINUS_R/pcmcia

	# do pcmcia-cs specific installation of modules
	if [ "$PCMCIA_DRIVERS" = "pcmcia-cs" ] ; then
 	    for i in pcmcia_core i82365 ds ; do
		cp -a modules/$i.o $ROOT/lib/modules/$UNAME_MINUS_R/pcmcia
	    done

            ml_transpose $BUILD/$PACKAGE/init.d/pcmcia.pcmcia-cs $ROOT/etc/init.d/pcmcia 755
	else
            ml_transpose $BUILD/$PACKAGE/init.d/pcmcia.kernel $ROOT/etc/init.d/pcmcia 755
	fi

	# make those really links....
        [ -L $ROOT/var/lock ] || ln -sf /tmp $ROOT/var/lock

	if [ -d $ROOT/var/lib/pcmcia ] ; then
	    rmdir $ROOT/var/lib/pcmcia
	elif [ ! -d $ROOT/var/lib ] ; then
	    mkdir $ROOT/var/lib
	fi
	[ -L $ROOT/var/lib/pcmcia ] || ln -sf /tmp/pcmcia $ROOT/var/lib

	# executables
        install --mode=755 -s cardmgr/cardmgr cardmgr/cardctl $ROOT/sbin

	# config files...
	install --mode=644 etc/config $ROOT/etc/pcmcia
	install --mode=644 etc/config.opts $ROOT/etc/pcmcia

	# call functions for different types of devices
	[ "$PCMCIA_DISK" = "yes" ] && pcmcia_install_disk

	[ "$PCMCIA_NET" = "yes" ] && pcmcia_install_net
}
