#!/bin/bash
#
# BuildMyMakeDev.sh - v0.1
# made by d1s4st3r (http://xoomer.alice.it/mental_insomnia/)
#
#
# This script is intented to take information from your local
# device files (situated in "/dev") and build a new script to
# create them again on another "/dev" directory. I made it
# because I'm working on a custom GNU/Linux distro and I don't
# want to mknod all devices by hand... :-P
# Tested on a system without udev, currently creates char and
# block devices only.
# Easily customizable.
#
# To run it, follow these simple steps:
#   1) run ./BuildMyMakeDev.sh from current directory to
#      build the script (held in $DESTFILE) that will create
#      your device files later
#   2) read, understand, modify if needed, then run the new
#      script to create the new device files (for example, in
#      a chrooted environment)


TEMPFILE=`tempfile`
DESTFILE="/tmp/MyMakeDev.sh" # feel free to change this

echo -n "Taking information from local system's device files..."
ls -la /dev | grep -e ^[cb] > $TEMPFILE
echo " done."

echo -n "Building $DESTFILE..."
echo '#!/bin/bash' > $DESTFILE
echo 'cd /dev' >> $DESTFILE

while read line
do
	DEVNAME=`echo $line | awk '{ print $10 }'`
	DEVTYPE=`echo $line | awk '{ print $1 }' | cut -c 1`
	MAJOR=`echo $line | sed -e 's/,/ /' | awk '{ print $5 }'`
	MINOR=`echo $line | awk '{ print $6 }'`

	if [ -z "$(echo $MINOR | sed 's/[[:digit:]]//g')" ] ; then
		echo "mknod $DEVNAME $DEVTYPE $MAJOR $MINOR" >> $DESTFILE
	fi
done < $TEMPFILE

echo 'cd -' >> $DESTFILE

rm $TEMPFILE
echo " done."

