#!/bin/bash

# This script was written by Wayne Piekarski - wayne@cs.unisa.edu.au

KERNEL=`uname -r | awk -F'-' '{ print $1 }'`
if [ "$KERNEL" = "2.4.17" ]; then
	DEV=old
elif [ "$KERNEL" = "2.4.18" ]; then
	DEV=old
elif [ "$KERNEL" = "2.4.19" ]; then
	DEV=new
elif [ "$KERNEL" = "2.4.20" ]; then
	DEV=new
elif [ "$KERNEL" = "2.4.21" ]; then
	DEV=new
elif [ "$KERNEL" = "2.4.22" ]; then
	DEV=new
else
	echo "This script does not understand kernel $KERNEL, must be 2.4.17-2.4.22"
	exit 1
fi

if [ -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 /usr/local/lib/libdc1394_control.so.X to decide what version"
	exit 1
fi


if [ ! "$1" = "--create" ]; then
	echo
	echo "By Wayne Piekarski - wayne@tinmith.net"
	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
