#!/bin/ksh 
#===============================================================================
# ITK Telekommunik. AG    III   TTTTT   K  K        All rights reserved
# Joseph-von-              I      T     K K                                
#    Fraunhofer-Str. 23    I      T     KK                                
# D - 44227 Dortmund       I      T     K K                              
# Phone. (0231) 9747-0    III     T     K  K                            
#===============================================================================
#                                                                      
# Description:      Card-Configurator for CAPI-Driver
#                                                                     
# Product/Project:  Suprimo
#                                                                   
# Filename:         card_config
#                                                                  
# Subject:          Configurator for ITK-communication-cards for specified 
#                   hardware-platform
#                   Generates the following files from program-inputs and
#                   the Hardware-Platform-Profiles *.hw:
#                      Space.c, Node, Master, System    - for capi20-Driver
#                      cards.par                        - for Suprimo-Software
#
# Version:          @(#) $Revision: 1.7 $ $Date: 1997/12/02 12:50:48 $
#===============================================================================
# Update-History:
#  Date      User  Description of Change
# ------------------------------------------------------------------------------
# 07.11.1997  TH   Creation
#===============================================================================
#
integer PRIE_COUNT=0        # Number of Primary/EISA-cards
integer PRIP_COUNT=0        # Number of Primary/PCI-cards
integer MM_COUNT=0          # Number of MultiModem-cards
integer DM_COUNT=0          # Number of DigitalModem-cards
#
SYSDIR=/tmp/capi20          # Directory for Systemfiles
SPACEFILE=$SYSDIR/Space.c   # Filename for Space.c-file
NODEFILE=$SYSDIR/Node       # Filename for Node-file
MASTERFILE=$SYSDIR/Master   # Filename for Master-file
SYSTEMFILE=$SYSDIR/System   # Filename for System-file
CARDSFILE=cards.par         # Filename for cards.par-file
#
SEPARATOR=" "               # Separator for space.c-card-entries
integer CARD_COUNT_DRIVER=0 # Card-counter for CAPI-driver (without PCI-PnP-cards)
integer CARD_COUNT_PARAM=0  # Card-counter for cards.par-file
integer CARD_COUNT_ISDN=0   # Card-counter for ISDN-cards in cards.par
integer CARD_COUNT_MODEM=0  # Card-counter for Modem-cards in cards.par
integer PORT_COUNT_ISDN=0   # Port-counter for ISDN-cards
integer PORT_COUNT_MODEM=0  # Port-counter for ISDN-cards
integer PRI_PORTS=0         # Number of ports for PRI (30 (E1) or 23 (T1))
integer ii
integer jj
#
#
#########################################
# make_cards: Generate entry for all cards of 1 type
#   Parameters:
#     1: Cardname
#     2: Bus
#     3: Card-count
#########################################
function make_cards
{
    ii=1
    while [ $ii -le $3 ]
    do
	make_entry "$ii.$1:$2"
        if [ $? -ne 0 ]
        then
            echo "Hardware-infos for $ii.$1/$2 not found in $HWCONFIG"
            return 1
        fi
        ii=ii+1
    done
    return 0; 
}
#
#########################################
# make_entry: Generate entries for 1 card
#    Parameters:
#      1: Key in HW-configfile
#########################################
function make_entry
{
    line=`grep $1 $HWCONFIG`
    if [ $? -ne 0 ]
    then
        return 1
    fi
#   Get HW-infos for card
    xxx=`echo $line | cut -d':' -f2`;  bus=`echo $xxx`
    xxx=`echo $line | cut -d':' -f3`;  chans=`echo $xxx`
    xxx=`echo $line | cut -d':' -f4`;  vers=`echo $xxx`
    xxx=`echo $line | cut -d':' -f5`;  type=`echo $xxx`
    xxx=`echo $line | cut -d':' -f6`;  slot=`echo $xxx`
    xxx=`echo $line | cut -d':' -f7`;  io_start=`echo $xxx`
    xxx=`echo $line | cut -d':' -f8`;  io_end=`echo $xxx`
    xxx=`echo $line | cut -d':' -f9`;  irq=`echo $xxx`
    xxx=`echo $line | cut -d':' -f10`; ipl=`echo $xxx`
    xxx=`echo $line | cut -d':' -f11`; jump=`echo $xxx`
    xxx=`echo $line | cut -d':' -f12`; sm_start=`echo $xxx`
    xxx=`echo $line | cut -d':' -f13`; sm_end=`echo $xxx`
    xxx=`echo $line | cut -d':' -f14`; cardtype=`echo $xxx`
    if [ "$chans" = '$PRI' ]
    then
        chans=$PRI_PORTS
    fi
#
    echo "   Enter card $1 (slot:$slot,irq:$irq,io:$io_start,sm:$sm_start,ports:$chans)"
    if [ "$bus" != "PCI" ]
    then
        CARD_COUNT_DRIVER=CARD_COUNT_DRIVER+1
#       Append entry to Space.c-file
        echo "$SEPARATOR" \
         "{$vers, $type, $slot, $io_start, $io_end, $irq, $ipl, " \
         "$jump, $sm_start, $sm_end}  /* $1 */" >>$SPACEFILE
        SEPARATOR="," 
#       Append entry to System-file
        echo "capi20	Y	1	$ipl	4	$irq	" \
         "${io_start#*0x*}	${io_end#*0x*}	" \
         "${sm_start#*0x*}	${sm_end#*0x*}" >> $SYSTEMFILE
    fi
#   Append entry to cards.par
    if [ "$cardtype" = "I" ]
    then
        CARD_COUNT_ISDN=CARD_COUNT_ISDN+1
	echo ".icard_$CARD_COUNT_ISDN:	$CARD_COUNT_PARAM	$chans	# $1" >>$CARDSFILE
	PORT_COUNT_ISDN=PORT_COUNT_ISDN+$chans
    else
        CARD_COUNT_MODEM=CARD_COUNT_MODEM+1
	echo ".mcard_$CARD_COUNT_MODEM:	$CARD_COUNT_PARAM	$chans	# $1" >>$CARDSFILE
	PORT_COUNT_MODEM=PORT_COUNT_MODEM+$chans
    fi
    CARD_COUNT_PARAM=CARD_COUNT_PARAM+1
    return 0; 
}
#
#########################################
# create_files: Create Files
#########################################
function create_files
{
#
# Clean System-Directory
if [ -d $SYSDIR ]
then
    rm -f $SYSDIR/*
else
    mkdir $SYSDIR
fi
#
#
# Generate Node
echo 'clone     capi20  c       capi20'  >$NODEFILE
echo 'capi20    ixl     c       0'      >>$NODEFILE
# 
#
# Generate Master
echo 'capi20 I       iHcS    capi20  0       0       1       8       -1' >$MASTERFILE
#
# Generate System
rm -f $SYSTEMFILE
touch $SYSTEMFILE
# 
# Generate space.c
cat - <<EOD >$SPACEFILE
/*-------------------------------------------------------------------*/
/*                                                                   */
/*   ITK Telekommunikations AG                                       */
/*   Joseph-von-Fraunhofer-Str. 23                                   */
/*   D-44247 Dortmund                                                */
/*   Telefon 0231/9747-0                                             */
/*                                                                   */
/*   Copyright (C) 1995-1997 ITK AG, Dortmund                        */
/*-------------------------------------------------------------------*/
/*   ######## created automatically by "card_config" ############### */
/*-------------------------------------------------------------------*/
#include <sys/types.h>
#include "/usr/itk/capi/itk.h"
#include "config.h"
itkparam itkparams[MAX_CARDS]=
{
EOD
#
# 
# Generate cards.par
cp -f $CARDSFILE ${CARDSFILE}.old
cat - <<EOD >$CARDSFILE
#===============================================================
# ITK Telekommunik. AG III   TTTTT   K  K  All rights reserved
# Joseph-von            I      T     K K
#   Fraunhofer-Str.23   I      T     KK
# D - 44227 Dortmund    I      T     K K
# Phone (0231) 9747-0   I      T     K  K
#===============================================================
# Description:         Cards-Parameterfile
# Product/Project:     Suprimo
# Filename:            cards.par
# Subject:             Parameterfile for Communicationcards
#                      and lines, ISDN-interfaces
#===============================================================
# ISDN-Cards:
#  .icard_cnt: Count-of-ISDN-Cards
#  .icard_N:     Controller-No   Count-of-B-Channels
# Modemcards:
#  .mcard_cnt: Count-of-Modemcards
#  .mcard_N:     Controller-No   Count-of-Modems      [Modem-pool-id]
#
# Parameter	Ctrl	Chan	Remark
#-----------------------------------------------
EOD
#
return 0
}

#
#########################################
# finish_files: Finish Files
#########################################
function finish_files
{
#
# Finish System
if [ $CARD_COUNT_DRIVER = 0 ]
then
    echo "capi20	Y	1	5	4	0	0	" \
	 "0	0	0" > $SYSTEMFILE
fi
# Finish space.c
if [ $CARD_COUNT_DRIVER = 0 ]
then
    echo " 0" >> $SPACEFILE
fi
echo "};" >>$SPACEFILE
echo "int s_cardcounter=$CARD_COUNT_DRIVER;" >>$SPACEFILE
echo "OCTO_C s_octoControl={ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};" >>$SPACEFILE
#
# Finish cards.par
echo "#" >>$CARDSFILE
echo ".icard_cnt: $CARD_COUNT_ISDN		# Number of ISDN-Cards" >>$CARDSFILE
echo ".mcard_cnt: $CARD_COUNT_MODEM		# Number of Modem-Cards" >>$CARDSFILE
echo "#" >>$CARDSFILE
echo ".PORTS_ISDN:  $PORT_COUNT_ISDN	# Number of ISDN-Ports" >>$CARDSFILE
if [ $PORT_COUNT_MODEM -gt $PORT_COUNT_ISDN ]
then
    ii=$PORT_COUNT_ISDN
else
    ii=$PORT_COUNT_MODEM
fi
echo ".PORTS_MODEM: $ii	# Number of Modem-Ports" >>$CARDSFILE
chmod 664 $CARDSFILE
#
# copy Driver
cp /usr/itk/capi/Driver.o $SYSDIR || return 1
return 0
}


#########################################
# main
#########################################
#
echo "card_config V1.1: Configurator for communication-cards"
echo "======================================================"
echo "(Copyright ITK Telekommunikation AG)"
echo ""
#
# Detect HW-platform (EISA/PCI)
HW_BASE=""
/etc/hwconfig | grep 'name=pci'  >/dev/null  &&  HW_BASE="pci"
/etc/hwconfig | grep 'name=eisa' >/dev/null  &&  HW_BASE="eisa"
if [ "$HW_BASE" = "" ]
then
    echo "unknown Hardware-Base"
    exit 1
fi
echo "   Hardware-Base: $HW_BASE"
# 
# Get Hardware-Platform
while echo '   Select Hardware-Platform from following list:'
do
    ii=0
    for FILE in *_${HW_BASE}.hw
    do
        ii=ii+1
	PLATTEXT=`grep '#!' $FILE`
        echo "      $ii. $FILE:	$PLATTEXT"
    done
    echo "Number of Hardware-Platform (default:1): \c"
    read jj
    if [ jj -eq 0 ]
    then
        jj=1
    fi
    ii=0
    for FILE in *_${HW_BASE}.hw
    do
        ii=ii+1
	if [ ii -eq jj ]
        then
            break
        fi
    done
    if [ ii -eq jj ]
    then
        break
    fi
done
HWCONFIG=$FILE
echo "   Using Hard-Platform $HWCONFIG"
#
# Get Programm-Inputs
#
# Get PRI-Type (E1/T1)
while echo 'PRI-Type (E1:30 ports,T1:23 ports) (default:E1): \c'
do
    read PRI_TYPE
    if [ "$PRI_TYPE" = "" ] || [ "$PRI_TYPE" = "e1" ] || [ "$PRI_TYPE" = "E1" ]
    then
        PRI_TYPE="E1"
        PRI_PORTS=30
        break;
    fi
    if [ "$PRI_TYPE" = "T1" ] || [ "$PRI_TYPE" = "t1" ] 
    then
        PRI_TYPE="T1"
        PRI_PORTS=23
        break;
    fi
done
echo "   Number of ports for PRI-interface: $PRI_PORTS ($PRI_TYPE)"
#
# Get number of Primary/PCI-cards
if [ "$HW_BASE" = "pci" ]
then
    while echo 'Number of ITK Primary/PCI-cards             : \c'
    do
        read PRIP_COUNT
        if [ $PRIP_COUNT -ge 0 ]
        then
            break
        fi
    done
fi
#
# Get number of Primary/EISA-cards
if [ "$HW_BASE" = "eisa" ]
then
    while echo 'Number of ITK Primary/EISA-cards            : \c'
    do
        read PRIE_COUNT
        if [ $PRIE_COUNT -ge 0 ]
        then
            break
        fi
    done
fi
#
# Get number of DigitalModem-cards
while echo 'Number of ITK DigitalModem-cards (30 Modems): \c'
do
    read DM_COUNT
    if [ $DM_COUNT -ge 0 ]
    then
        break
    fi
done
#
# Get number of MultiModem-cards
while echo 'Number of ITK MultiModem-cards (8 Modems)   : \c'
do
    read MM_COUNT
    if [ $MM_COUNT -ge 0 ]
    then
        break
    fi
done
#
# 
# Prepare files
create_files
#
# Generate files for Primary/EISA-cards:
if [ $PRIE_COUNT -gt 0 ] 
then
    make_cards "Primary_____" "EISA" $PRIE_COUNT
    if [ $? -ne 0 ]
    then
        exit 1
    fi
fi
# Generate files for DigitalModem-cards:
if [ $DM_COUNT -gt 0 ] 
then
    make_cards "DigitalModem" "ISA" $DM_COUNT
    if [ $? -ne 0 ]
    then
        exit 1
    fi
fi
# Generate files for MultiModem-cards:
if [ $MM_COUNT -gt 0 ] 
then
    make_cards "MultiModem__" "ISA" $MM_COUNT
    if [ $? -ne 0 ]
    then
        exit 1
    fi
fi
# Generate files for Primary/PCI-cards:
if [ $PRIP_COUNT -gt 0 ] 
then
    make_cards "Primary_____" "PCI" $PRIP_COUNT
    if [ $? -ne 0 ]
    then
        exit 1
    fi
fi
# Finish files
finish_files || exit 1
#
#
#
echo "   configured: $CARD_COUNT_ISDN ISDN-cards  with $PORT_COUNT_ISDN ports" 
echo "               $CARD_COUNT_MODEM Modem-cards with $PORT_COUNT_MODEM ports" 
#
echo "Installing capi20-Driver ..."
PWDOLD=$PWD
cd $SYSDIR
/etc/conf/bin/idinstall -d capi20
/etc/conf/bin/idinstall -a capi20  ||  exit 1
cd $PWDOLD
echo "   capi20-Driver has been installed successfully"
#
while echo 'Build new UNIX-Kernel (y/n) ? \c'
do
    read ANS
    if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
    then
        ANS=Y
        break
    fi
    if [ "$ANS" = "n" ] || [ "$ANS" = "N" ]
    then
        break
    fi
done
if [ "$ANS" = "Y" ]
then
   echo "Building UNIX-Kernel ... "
   /etc/conf/bin/idbuild || exit 1
else
   echo "In order to use the new card-configuration a new Unix-Kernel must"
   echo "be built manually with command:"
   echo "      /etc/conf/bin/idbuild"
fi

