#!/usr/bin/perl

# Copyright 1999 by Digi International (www.digi.com)
#     Jeff Randall <Jeff_Randall@digi.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the 
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#


#
#	Make a bunch of nodes for dense modem products
#	Used by /proc/dgdm/mknod.  This is called thusly:
#
#
#	                NAME              	BOARD   MAJOR   MINOR   COUNT
#	dgdm_mknod  /dev/dg/dgdm/ram%b        0       72      0       1
#

if ( @ARGV != 5 ) {
	die "Usage: dgdm_mknod node-proto board# major# first_minor# count\n"
}

$nameproto=$ARGV[0];
$board=$ARGV[1];
$major=$ARGV[2];
$firstminor=$ARGV[3];
$count=$ARGV[4];

$index=0;
$minor=$firstminor;

while ($index < $count) {
	#
	#	Compute padded versions of index number
	#
	if ( $index < 10 ) {
		$index00="0$index";
		$index000="00$index";
	}
	elsif ( $index <100 ) {
		$index00=$index;
		$index000="0$index";
	}
	else {
		$index00=$index;
		$index000=$index;
	}

	#
	#	Expand board and port number escapes
	#
	$name = $nameproto;
	$name =~ s/%b/$board/;
	$name =~ s/%p/$index00/;
	$name =~ s/%n/$index000/;

	#
	#	Make the node if it doesn't already exist
	#
	if ( ! -c $name ) {
		system "/bin/mknod", $name, "c", $major, $minor
	}

	++$minor;
	++$index;
}

#
#	Buggy ttyname(3) in linux requires tty devices right in /dev !!!
#
#	make links for all of them now.
#
if ( $nameproto =~ /.*tty.*/ ) {
	$name = $nameproto;
	$name =~ s/%b/$board/;
	$name =~ s/%p/\*/;
	system "/bin/sh", "-c", "/bin/ln -f $name /dev"
}
