#!/bin/bash

# mkcyc - 05/05/2000 release - (C) 1997-2000 Cyclades Corporation
# This version expanded to autodetect the kernel version, and
# set the minor base of our device to the proper value (0 or 32).
# Distribution permitted under the terms of the GNU "copyleft".

echo
echo "Cyclades Linux Driver - Device Creation"
echo "Copyright (C) Cyclades Corporation, 1997-2000"
echo
echo -e "How many Cyclades ports do you have?(1-256) \c"
read NPORT
if [ "$NPORT" = "" ]; then
	echo "Invalid number of ports. Finishing..."
	exit 1
elif [ $NPORT -le 0 -o $NPORT -gt 256 ]; then
	echo "Invalid number of ports. Finishing..."
	exit 1
else
	echo "Creating $NPORT devices..."
fi

MAJORT=19
MAJORM=20
PORT=0

# Determine the proper minor number from the kernel version

RELEASE=`uname -r | awk -F- '{print $1}'`
let REL1=${RELEASE%%.*}
REL23=${RELEASE#*.}
let REL2=${REL23%.*}
let REL3=${REL23#*.}

if [ $RELEASE = $(echo $REL1.$REL2.$REL3) ]; then
	echo -n Kernel version $RELEASE
else
	echo
	echo ++++++++++++++++++++++++++++++++++++++++++++++
	echo Error in parsing kernel version! Please inform
	echo support@cyclades.com.  Thanks.
	echo ++++++++++++++++++++++++++++++++++++++++++++++
	echo
	echo Got release: $RELEASE
	echo Parsing results: $REL1.$REL2.$REL3
fi

# Default minor=0 (kernel >= 1.3.94)
MINOR=0

if [ $REL1 -lt 2 ]; then
	MINOR=32
	if [ $REL1 = 1 ] && [ $REL2 = 3 ] && [ $REL3 -ge 94 ]; then
		MINOR=0
	fi
fi

echo , using minor $MINOR.

# Remove previously existing devices
rm -rf /dev/ttyC*
rm -rf /dev/cub*

while true
do
	if [ "$PORT" = "$NPORT" ]
	then
		echo "Done."
		if [ $REL2 -ge 2 ] ; then
			/bin/chown uucp /dev/ttyC*
			/bin/chgrp uucp /dev/ttyC*
			/bin/chmod 660 /dev/ttyC*
		else
			/bin/chown uucp /dev/cub*
			/bin/chgrp uucp /dev/cub*
			/bin/chown root /dev/ttyC*
			/bin/chgrp tty /dev/ttyC*
		fi
		exit
	fi

	/bin/mknod /dev/ttyC$PORT c $MAJORT $MINOR
	if [ $REL2 -ge 2 ] ; then
		/bin/ln -s /dev/ttyC$PORT /dev/cub$PORT
	else
		/bin/mknod /dev/cub$PORT  c $MAJORM $MINOR
	fi
	PORT=`expr $PORT + 1`
	MINOR=`expr $MINOR + 1`
done
