#!/bin/bash

KERNEL=`uname -r | awk -F'-' '{ print $1 }'`
case $KERNEL in
	2.4.17|2.4.18)
		DEV=old
		;;
	2.4.19)
		DEV=new
		;;
	2.4.2?)
		DEV=new
		;;
	2.6.*)
		DEV=new
		;;
	*)
		echo "This script does not understand kernel $KERNEL"
		exit 1
esac

if [ -f /usr/local/lib/libdc1394_control.so.11 ] || [ -f /usr/lib/libdc1394_control.so.11 ]; then
	LIBDCFILE=libdc1394_control.so.11
	LIBDC=new
elif [ -f /usr/local/lib/libdc1394_control.so.10 ] || [ -f /usr/lib/libdc1394_control.so.10 ]; then
	LIBDCFILE=libdc1394_control.so.10
	LIBDC=new
elif [ -f /usr/local/lib/libdc1394_control.so.9 ] || [ -f /usr/lib/libdc1394_control.so.9 ]; then
	LIBDCFILE=libdc1394_control.so.9
	LIBDC=new
elif [ -f /usr/local/lib/libdc1394_control.so.8 ] || [ -f /usr/lib/libdc1394_control.so.8 ]; then
	LIBDCFILE=libdc1394_control.so.8
	LIBDC=old
else
	echo "Could not find libdc1394_control.so.X in either /usr/lib or /usr/local/lib"
	exit 1
fi


if [ ! "$1" = "--create" ]; then
	echo
	echo "Script creates devices for 1394 interfaces"
	echo "These interfaces were changed in 2.4 v17-18 and libDC v8-9"
	echo "Current kernel is $KERNEL, will use $DEV style major/minor numbers"
	echo "Current libdc is $LIBDCFILE, will use $LIBDC style device interface"
	echo
	echo "Run the script with --create to perform operations"
	echo
	exit 1
fi


if [ "$DEV" = "new" ]; then
	MAJ=171
	MIN=16
else
	MAJ=172
	MIN=0
fi
echo "Devices will be created for $DEV kernel $KERNEL with device ($MAJ, $MIN)"


echo "Creating $LIBDC style /dev/video1394 with subdirectories for $LIBDCFILE"
if [ "$LIBDC" = "new" ]; then
  rm -rf /dev/video1394
  mkdir /dev/video1394
  chmod ugo+rx /dev/video1394
  mknod /dev/video1394/0 c $MAJ $[$MIN+0]
  mknod /dev/video1394/1 c $MAJ $[$MIN+1]
  mknod /dev/video1394/2 c $MAJ $[$MIN+2]
  mknod /dev/video1394/3 c $MAJ $[$MIN+3]
  chmod ugo+rw /dev/video1394/?
  chmod ugo+x /dev/video1394
else
  rm -rf /dev/video1394
  mknod /dev/video1394 c $MAJ $MIN
  chmod ugo+rw /dev/video1394
fi

# Print out the final device layout
ls -al /dev/video1394


echo
echo
echo "Creating /dev/raw1394 entries"
mkdir /dev/raw1394 c 171 0
chmod ugo+rw /dev/raw1394
chmod ugo-x /dev/raw1394
ls -al /dev/raw1394
