#!/bin/bash

# Author: tomekk                                                                                                                                                                         
# e-mail:  tomekk/@/tomekk/./org                                                                                                                                                  
# home page: http://tomekk.org/
#
# Version 0.1
#
# This file is Copyrighted under the GNU Public License.
# http://www.gnu.org/copyleft/gpl.html
##########################################################

# fetch root and filesystem type
D=`mount | grep 'on / type' | awk -F" " {'print $1'}`
F=`mount | grep 'on / type' | awk -F" " {'print $5'}`
PWD=`pwd`

# run from /boot
if [ $PWD == "/boot" ]; then
	echo "current linux: `uname -r`" 
	echo "root on: $D"
	echo "root fs type: $F"
	echo 
	
	# get versions from /lib/modules
	# i'm using one kernel version BUT after installation of new kernel (fe. slackpkg)  i'm always see two or more kernels...
	# it's because some extra modules stays in old kernel directory (fe. nvidia module)
	# because i'm lazy, i want to pick the version from a simple list than do it manually (i mean, listing the directory etc.)
	linux=(`for v in /lib/modules/*; do echo $v | xargs basename; done;`)

	# gen array and print some simple list
	IDX=0	
	for version in ${linux[*]}
       	do
		((IDX++))
		echo "[${IDX}] $version"
	done

	# get user input
	echo -n "your choice: "
	read choice

	# some basic tests
	if ! [[ $choice =~ ^[0-9]+$ ]] || [ ${#choice} -gt 2 ]; then
		echo "yeah sure..."
		exit
	fi

	if [ $choice -gt $IDX ] || [ $choice -le 0 ]; then
		echo "yeah sure..."
		exit
	fi

	# some info
	echo
	echo "running mkinitrd with opts: -R -u -c -k ${linux[($choice - 1)]} -m ext2:ext4 -f $F -r $D"

	# remove old tree and make new img
	rm -rf initrd-tree
	mkinitrd -R -u -c -k ${linux[($choice - 1)]} -m ext2:ext4 -f $F -r $D
else
	echo "crap: $0 - please put this script into /boot dir"
fi
