#!/bin/sh
#
# Copyright 1999 by Digi International (www.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.
#

#======================================================================
#
# This script is a dumb wrapper for "dgrp_mk_spec", designed to
# allow the mass creation of ports for a PortServer.  If the number
# of ports supplied is smaller than the number of ports which currently
# exist, those ports which should no longer exist will be deleted.
#
#======================================================================

#----------------------------------------------------------------------
#
#  global constants
#
#----------------------------------------------------------------------

MAXCHAN=64
TTY_PREFIX="/dev/tty"
CU_PREFIX="/dev/cu"
PR_PREFIX="/dev/pr"

DGRP_MK_SPEC="/usr/bin/dgrp/config/dgrp_mk_spec"
if [ ! -x $DGRP_MK_SPEC ]
then
	echo "You do not have x permission for $DGRP_MK_SPEC"
	exit 1
fi

#----------------------------------------------------------------------
#
#  usage
#
#----------------------------------------------------------------------

usage() {
	cat <<EOF

USAGE:  $0 [ options ] <ID> <major> <port_count>

       ID         - device ID
       major      - device's major number
       port_count - Number of ports to create for this device

       options:

           -v            Verbose
           -m <mode>     Set the default file protection mode
           -o <owner>    Set the default user ID of the file owner
                         Value must be an integer.
           -g <group>    Set the default group ID of the file owner
                         Value must be an integer.

EOF
	exit 1
}

#----------------------------------------------------------------------
#
#  zerofill
#
#----------------------------------------------------------------------

zerofill() {
	echo `expr "00$1" : '.*\(..\)$'`
}


#----------------------------------------------------------------------
#
#  main
#
#----------------------------------------------------------------------

verbosity=0
default_mode=""
default_owner=""
default_group=""

id=""
major=""
port_count=""

set -- `getopt m:o:g:v $*`
if [ $? != 0 ]
then
	usage
fi

for i in $*
do
	case $i in
	-m)	default_mode="-m $2"  ; shift ; shift ;;
	-o)	default_owner="-o $2" ; shift ; shift ;;
	-g)	default_owner="-g $2" ; shift ; shift ;;
	-v)	verbosity=`expr $verbosity + 1` ; shift ;;
	--)	shift ; break;;
	esac
done

if [ $# != 3 ]
then
	usage
fi

id=$1
major=$2
port_count=$3

i=0

while [ $i -lt $MAXCHAN ]
do
	port=`zerofill $i`

	if [ $i -lt $port_count ]
	then
		#
		# For each device to be created, call the "creation" program
		#

		${DGRP_MK_SPEC} $default_mode $default_owner \
		                $default_group $id \
		                $major $i

		rc=$?

		if [ $rc -ne 0 ]
		then
			exit 1
		fi

		if [ $verbosity -ge 2 ]
		then
			echo "Devices for port $i have been prepared."
		fi
	else
		#
		# For each device which should not exist, try to "rm" it.
		#

		port=`zerofill $i`
		for prefix in ${TTY_PREFIX} ${CU_PREFIX} ${PR_PREFIX}
		do
			file="${prefix}${id}${port}"

			if [ -c $file ]
			then
				rm -f $file

				rc=$?

				if [ $rc -ne 0 ]
				then
					echo "Could not delete $file" >&2
				else
					if [ $verbosity -ge 1 ]
					then
						echo "$file deleted"
					fi
				fi
			fi
		done
	fi

	i=`expr $i + 1`
done
