#!/bin/bash

# mkcyc - 7/23/97 release - (C) 1997 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".

clear
echo
echo "Cyclom Driver for Linux - Device Creation"
echo "Copyright (C) Cyclades Corporation, 1997"
echo
echo -e "How many Cyclom-Y ports do you have \n\r(8,16,24,32,40,48,56,64,80,88,96,104,112,120,128)? \c"
read NPORT
case $NPORT in
	"8"|"16"|"24"|"32"|"40"|"48"|"56"|"64"|"80"|"88"|"96"|"104"|"112"|"120"|"128")

		echo "Creating $NPORT devices..." ;;
	*)
		echo "Invalid number of ports. Finishing..."
		exit ;;
esac

MAJORT=19
MAJORM=20
PORT=0

# Determine the proper minor number from the kernel version

RELEASE=$(uname -r)
let REL1=${RELEASE%%.*}
REL3=${RELEASE#*.}
let REL2=${REL3%%.*}
# For some unknown reason, bash won't let me 'let' the next line.
# 'let'ting the result works, however...
REL3=${RELEASE##*.}
let REL3=$REL3
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.

while true
do
	if [ "$PORT" = "$NPORT" ]
	then
		echo "Done."
		/bin/chown uucp /dev/cub*
		/bin/chgrp uucp /dev/cub*
		/bin/chown root /dev/ttyC*
		/bin/chgrp tty /dev/ttyC*
		exit
	fi
	if [ -e /dev/ttyC$PORT ]
	then
		rm -f /dev/ttyC$PORT
	fi
        if [ -e /dev/cub$PORT ]
        then
                rm -f /dev/cub$PORT
        fi

	/bin/mknod /dev/ttyC$PORT c $MAJORT $MINOR
	/bin/mknod /dev/cub$PORT  c $MAJORM $MINOR
	PORT=`expr $PORT + 1`
	MINOR=`expr $MINOR + 1`
done
