#!/bin/bash
# script to install modules:
#   modinst modules.{zip,tar,tar.gz} 
# 
#      the correct kernel release will be obtained from one of the modules,
#
#      modules archiv can be packed eg by: cd modules && zip -9 modules *
#
# uncomment the code below if your 'mktemp -d' works correctly !!
#

# default prefix for installation:
INSTALL_MOD_PATH=

# remember from where we have been called:
CALL_PATH=`pwd`

if [  $# != 1 ]; then
    echo "usage: $0 modules.{zip,tar,tar.gz}"
    exit 1
fi

KERNELRELEASE=`uname -r`

if [ ! -r $1 ]; then
    echo "cant access modules archive $1"
    exit 1
fi

# where to unpack..
# mktemp is nice but at least one of my versions refuses the '-d' flag
#TEMPDIR=`mktemp -d /usr/tmp/modinstXXXXXX` || (echo "couldn't make TEMPDIR"; exit 1)

TEMPDIR=/usr/tmp/modinst_${KERNELRELEASE}
echo "temporary dir: $TEMPDIR"
mkdir -p $TEMPDIR || (echo "could not make $TEMPDIR"; exit 1)
cd $TEMPDIR 

# get archive real name
case $1 in
	/*) ARCHNAME=$1
	    ;;
	*) ARCHNAME=$CALL_PATH/$1
	    ;;
esac

# unpack using appropriate packer
case $1 in
	*.tar.gz)  tar xzf $ARCHNAME;;
	*.tar)	   tar xf $ARCHNAME;;
	*.zip)     unzip $ARCHNAME;;
	*)	   echo "Hm.. packer format for $1 not supported"
		   cd $CALL_PATH 
		   rmdir $TEMPDIR
		   exit 1;;
esac

## attempt to find out kernel release for which the modules were compiled
MOD=(`ls *.o`)
MOD_RELSE=`strings -a $MOD | grep "kernel_version=" | sed s/kernel_version=//`
## now do something clever with this info..

if [ -z "$MOD_RELSE" ];then
    echo "could not find out kernel_version for modules"
    rm -rf $TEMPDIR
    exit 1
fi

##if [ $# = 2 ]; then 
##    if [ "$MOD_RELSE" != "$KERNEL_RELEASE"]; then
##	echo "you have specified wrong kernel version, modules"
##	echo "are compiled for $MOD_RELSE"
##	exit 1
##    fi
##fi

echo "installing modules for kernel release $MOD_RELSE"

# following adapted from Makefile:

	MODLIB=$INSTALL_MOD_PATH/lib/modules/$MOD_RELSE

	MODULES="" 
	inst_mod() { These="`cat $1`"; MODULES="$MODULES $These"; \
		mkdir -p $MODLIB/$2; cp $These $MODLIB/$2; \
		echo Installing modules under $MODLIB/$2; \
	}; 

	mkdir -p $MODLIB; 

	if [ -f BLOCK_MODULES  ]; then inst_mod BLOCK_MODULES  block;  fi; 
	if [ -f NET_MODULES    ]; then inst_mod NET_MODULES    net;    fi; 
	if [ -f IPV4_MODULES   ]; then inst_mod IPV4_MODULES   ipv4;   fi; 
	if [ -f IPV6_MODULES   ]; then inst_mod IPV6_MODULES   ipv6;   fi; 
	if [ -f SCSI_MODULES   ]; then inst_mod SCSI_MODULES   scsi;   fi; 
	if [ -f FS_MODULES     ]; then inst_mod FS_MODULES     fs;     fi; 
	if [ -f NLS_MODULES    ]; then inst_mod NLS_MODULES    fs;     fi; 
	if [ -f PARTBL_MODULES ]; then inst_mod PARTBL_MODULES partbl; fi; 
	if [ -f CDROM_MODULES  ]; then inst_mod CDROM_MODULES  cdrom;  fi; 
	if [ -f VIDEO_MODULES  ]; then inst_mod VIDEO_MODULES  video;  fi; 
	if [ -f HAM_MODULES    ]; then inst_mod HAM_MODULES    net;    fi; 
	if [ -f SOUND_MODULES  ]; then inst_mod SOUND_MODULES  sound;  fi; 
	if [ -f FC4_MODULES    ]; then inst_mod FC4_MODULES    fc4;    fi; 
	if [ -f IRDA_MODULES   ]; then inst_mod IRDA_MODULES   net;    fi; 

	ls *.o > $MODLIB/.allmods; 
	echo $MODULES | tr ' ' '\n' | sort | comm -23 $MODLIB/.allmods - > $MODLIB/.misc; 
	if [ -s $MODLIB/.misc ]; then inst_mod $MODLIB/.misc misc; fi; 
	rm -f $MODLIB/.misc $MODLIB/.allmods; 
	
cd ..
rm -rf $TEMPDIR

# see if we should run modprobe..

if [ "$MOD_RELSE" = "$KERNELRELEASE" ]; then
    echo "modules installed for current kernel, running depmod"
    depmod -a
fi
