#!/bin/ksh
###############################################################
#
#  Installationscript for ITK-CAPI20-Driver
#
#
#  ITK Telekommunikations AG
#  Emil-Figge-Strasse 80
#  D-44247 Dortmund  
#  Telefon 0231/9747-0
#
#  Copyright (C) 1995-1997 ITK AG, Dortmund
#
#  $Revision: 3.4 $
#  $Date: 1997/03/06 10:44:17 $
#
###############################################################
typeset -i NEWI=0
typeset -i RECON=0
typeset -i JUMPER=0
typeset -i ACT_VEC5=0
typeset -i ACT_VEC9=0
typeset -i ACT_VECa=0
typeset -i ACT_VECb=0
BAS_1=0
BAS_2=0
BAS_3=0
BAS_4=0
BAS_5=0
BAS_6=0
TYP=0
REV=0
HwTyp='HW_ISA_S0_25'
IRQ=0
IPL=5
SMSTART=0
SMEND=0
IOBASE=0
IOEND=0
ISCHEME=0
SCHEME=0
BASE=0
SLOT=0
SHOW=1
EISA='C80'
ADAPTER=''
CHECK=''
STRI=''
STR=''
BUS=''
typeset -i SCRATCH=0
typeset -i CC=0        #CardCounter
typeset -i BUSTYPE=0   #PCI = 1
MORE=0
INPATH='/usr/itk/capi'
IPATH=''
NULL=''
OCTO='0x0'
OCTO_STR='OCTO_C s_octoControl={'
OSTYPE=SCO
#
#
#
#
#
open()
{
clear
echo 'Installation- and Reconfigurationscript for ITK-CAPI20-Driver'
echo '+-+-++-+-+-+-+-+-++-+-++-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+\n'
}
#
#
jumper()
{
open
echo 'How is your jumper setting ?:\n'
echo '      J4       J3'
echo '-------------------'
echo '1:  closed   closed'
echo '2:  closed    open'
echo '3:   open    closed'
echo '4:   open     open'
JUMPER=0
while echo '\nPlease type a number [1-4]:\c'
do
read JUMPER
case $JUMPER in
      [1234]) break;;
#              if [ $SCRATCH -gt 3 ]; then
#                 HELP=1
#              else
#                 HELP=0
#              fi
#              SCRATCH=$SCRATCH+4
#              JUMPER=$JUMPER-1 
#              SCRATCH=$SCRATCH-$JUMPER 
#              if [ $SCRATCH -gt 7  -a $HELP -eq 1 ]; then
#                 SCRATCH=$SCRATCH-4
#              elif [ $SCRATCH -gt 3  -a $HELP -eq 0 ]; then
#                 SCRATCH=$SCRATCH-4
#              fi
#              break;;
           *) open
              echo 'How is your jumper setting ?:\n'
              echo '      J4       J3'
              echo '-------------------'
              echo '1:  closed   closed'
              echo '2:  closed    open'
              echo '3:   open    closed'
              echo '4:   open     open'
              ;;
esac
done
SCRATCH=$JUMPER
}
#
#
smspace()
{
open
START=0
echo 'Which SharedMemory-adress do you want to use ?:\n'
echo 'SM-Space = 32kByte\n'
echo '1: 0xA0000'
echo '2: 0xA8000'
echo '3: 0xC0000'
echo '4: 0xC8000'
echo '5: 0xD0000'
echo '6: 0xD8000'
echo '7: 0xE0000'
echo '8: 0xE8000\n'
while echo 'Please type a number [1-8]: \c' 
do
read START
case $START in
     1)SMSTART=A0000
     SMEND=A7FFF
     break;;
     2)SMSTART=A8000
     SMEND=AFFFF
     break;;
     3)SMSTART=C0000
     SMEND=C7FFF
     break;;
     4)SMSTART=C8000
     SMEND=CFFFF
     break;;
     5)SMSTART=D0000
     SMEND=D7FFF
     break;;
     6)SMSTART=D8000
     SMEND=DFFFF
     break;;
     7)SMSTART=E0000
     SMEND=E7FFF
     break;;
     8)SMSTART=E8000
     SMEND=EFFFF
     break;;
     *) ;;
esac
done
return
}
#
#
#
slot()
{
open
LOT=0
echo 'Which EISA-Slot do you use for the '$ADAPTER' ?:  '
while echo '[Number must be between 1 and 8]:\c '
do
read LOT
case $LOT in
    [12345678]) SLOT=$LOT
         return SLOT;;
    *)open
      echo 'Which EISA-Slot do you use for the '$ADAPTER' ?:  '
      ;;
esac
done
}
#
#
#
iobase()
{
open
echo 'Which I/O-Base do you want to use?:\n'
case $TYP in
 1) iobase2
    jumper
    break;; 
 [234]) if [ $ISCHEME -eq 1 ]; then
             iobase2
             jumper
       elif [ $ISCHEME -eq 2 ]; then
             slot
             IOBASE=$SLOT'000'
             IOEND=$SLOT'007'
             SCRATCH=6
       fi
    break;;
esac
}
#
#
#
iobase2()
{
ENT=4
typeset -i C=0
if [ "$BAS_1" != "DONE" ];then
   /etc/conf/bin/idcheck -a -l 380 -u 387
   if [ $? -eq 0 -o "$BAS_1" = "380" ]; then
      echo '1: 0x380'
      C=$C+1
   fi
fi
if [ "$BAS_2" != "DONE" ];then
   /etc/conf/bin/idcheck -a -l 388 -u 38F
   if [ $? -eq 0 -o "$BAS_2" = "388" ]; then
      echo '2: 0x388'
      C=$C+1
   fi
fi
if [ "$BAS_3" != "DONE" ];then
   /etc/conf/bin/idcheck -a -l 390 -u 397
   if [ $? -eq 0 -o "$BAS_3" = "390" ]; then
      echo '3: 0x390'
      C=$C+1
   fi
fi
if [ "$BAS_4" != "DONE" ];then
   /etc/conf/bin/idcheck -a -l 398 -u 39F
   if [ $? -eq 0 -o "$BAS_4" = "398" ]; then
      echo '4: 0x398'
      C=$C+1
   fi
fi
if [ $TYP -ne 1 ]; then
   if [ "$BAS_5" != "DONE" ];then
      /etc/conf/bin/idcheck -a -l 3A0 -u 3A7
      if [ $? -eq 0 -o "$BAS_5" = "3A0" ]; then
         echo '5: 0x3A0'
         C=$C+1
      fi
   fi
   if [ "$BAS_6" != "DONE" ];then
      /etc/conf/bin/idcheck -a -l 3A8 -u 3AF
      if [ $? -eq 0 -o "$BAS_6" = "3A8" ]; then
        echo '6: 0x3A8\n'
        C=$C+1
      fi
   fi
   ENT=6
fi
if [ $C -eq 0 ]; then
   echo 'ERROR: No more free IO-Base'
   out_here
fi
while echo 'Please type a number : \c' 
do
read BASE
case $BASE in
     1)if [ "$BAS_1" != "DONE" ];then
          BAS_1='DONE'
          IOBASE=380
          IOEND=387
          SCRATCH=0
          break
       fi
       ;;
     2)if [ "$BAS_2" != "DONE" ];then
          BAS_2='DONE'
          IOBASE=388
          IOEND=38F
          SCRATCH=1
          break
       fi
       ;;
     3)if [ "$BAS_3" != "DONE" ];then
          BAS_3='DONE'
          IOBASE=390
          IOEND=397
          SCRATCH=2
          break
       fi
       ;;
     4)if [ "$BAS_4" != "DONE" ];then
          BAS_4='DONE'
          IOBASE=398
          IOEND=39F
          SCRATCH=3
          break
       fi
       ;;
     5)if [ "$BAS_5" != "DONE" ];then
          BAS_5='DONE'
          IOBASE=3A0
          IOEND=3A7
          SCRATCH=4
          break
       fi
       ;;
     6)if [ "$BAS_6" != "DONE" ];then
          BAS_6='DONE'
          IOBASE=3A8
          IOEND=3AF
          SCRATCH=5
          break
       fi
       ;;
     *);;
esac
done
}
#
#
#
#
print_typ()
{
open
if [ $CC -eq 0 ]; then
   echo 'For each ITK ix1-adapter you want to use in your system,'
   echo 'answer the following questions:\n'
fi
echo 'Which type of ITK ix1-adapter(s) do you want to use?:\n'
if [ $ISCHEME -eq 1 ]; then
echo '1: ITK ix1 basic S0/UP0 V1.1'                
fi
echo '2: ITK ix1 basic S0/UP0 V1.2'
echo '3: ITK ix1 octo  S0        '
echo '4: ITK ix1 multiModem V1.1'
if [ $ISCHEME -eq 2 ]; then
AW='2-6'
echo '5: ITK ix1 primary EISA V1.1'
echo '6: ITK ix1 primary EISA V1.2'
fi
}
#
#
typ()
{
AW='1-4'
print_typ
while echo '\nPlease type a number['$AW']: \c' 
do
read TYP
if [ $ISCHEME -eq 1 ]; then
  if [ $TYP -lt 1 -o $TYP -gt 4 ]; then
     TYP=8
  fi
elif [ $ISCHEME -eq 2 ]; then
  if [ $TYP -lt 2 -o $TYP -gt 6 ]; then
     TYP=8
  fi
fi
case $TYP in
    1)  if [ $ISCHEME = 1 ]; then
    #jumper
    ADAPTER='ITK ix1-Basic V1.1'
    REV=1
    HwTyp='HW_ISA_S0_25'
    vec
    SMSTART=0
    SMEND=0
    fi
    break;;
    2)# if [ $ISCHEME = 1 ]; then
      #    jumper
      # fi
    ADAPTER='ITK ix1-Basic V1.2'
    REV=2 
    HwTyp='HW_ISA_S0_25'
    vec
    SMSTART=0
    SMEND=0
    break;;
    3)# if [ $ISCHEME = 1 ]; then
      #    jumper
      # fi
    TYP=2
    ADAPTER='ITK ix1-Octo'
    REV=2 
    HwTyp='HW_ISA_S0_25'
    OCTO='0x1'
    vec
    SMSTART=0
    SMEND=0
    break;;
    4)# if [ $ISCHEME = 1 ]; then
      #    jumper
      # fi
    ADAPTER='ITK ix1-multi-Modem'
    REV=0
    HwTyp='HW_ISA_MODEM_25'
    vec
    SMSTART=0
    SMEND=0
    break;;
    5) ADAPTER='ITK ix1-Primary V1.1'
    REV=1
    HwTyp='HW_EISA_S2M_40'
    vec
    IOBASE=$SLOT'C80'
    IOEND=$SLOT'C8B'
    break;;
    6) ADAPTER='ITK ix1-Primary V1.2'
    REV=2
    HwTyp='HW_EISA_S2M_40'
    vec
    IOBASE=$SLOT'C80'
    IOEND=$SLOT'C8B'
    break;;
   *) print_typ
     ;;
esac
done
}
#
#
#
vec()
{
if [ $ISCHEME -eq 2 -a $IRQ -ne 0 ]; then
   SHOW=0
fi
open
echo 'Which irq-vector from the following list do you want to use?:\n'
case $TYP in
      [123])vec1
           break;;
      [456])vec2
           break;;
esac
}
#
#
#
#
capi_installed()
{
fgrep capi20 /etc/conf/cf.d/sdevice > /dev/null
if [ $? -eq 0 ]; then
   awk '($1 == "capi20") && ($2 == "Y") && ($6 == "5") { printf("%d", $6) }' /etc/conf/cf.d/sdevice |read ACT_VEC5
   awk '($1 == "capi20") && ($2 == "Y") && ($6 == "9") { printf("%d", $6) }' /etc/conf/cf.d/sdevice |read ACT_VEC9
   awk '($1 == "capi20") && ($2 == "Y") && ($6 == "10") { printf("%d", $6) }' /etc/conf/cf.d/sdevice |read ACT_VECa
   awk '($1 == "capi20") && ($2 == "Y") && ($6 == "11") { printf("%d", $6) }' /etc/conf/cf.d/sdevice |read ACT_VECb
   awk '($1 == "capi20") && ($2 == "Y") && ($6 == "12") { printf("%d", $6) }' /etc/conf/cf.d/sdevice |read ACT_VECc
   awk '($1 == "capi20") && ($2 == "Y") && ($6 == "15") { printf("%d", $6) }' /etc/conf/cf.d/sdevice |read ACT_VECf
   awk '($1 == "capi20") && ($2 == "Y") && ($7 == "380") { printf("%s", $7) }' /etc/conf/cf.d/sdevice |read BAS_1
   awk '($1 == "capi20") && ($2 == "Y") && ($7 == "388") { printf("%s", $7) }' /etc/conf/cf.d/sdevice |read BAS_2
   awk '($1 == "capi20") && ($2 == "Y") && ($7 == "390") { printf("%s", $7) }' /etc/conf/cf.d/sdevice |read BAS_3
   awk '($1 == "capi20") && ($2 == "Y") && ($7 == "398") { printf("%s", $7) }' /etc/conf/cf.d/sdevice |read BAS_4
   awk '($1 == "capi20") && ($2 == "Y") && ($7 == "3A0") { printf("%s", $7) }' /etc/conf/cf.d/sdevice |read BAS_5
   awk '($1 == "capi20") && ($2 == "Y") && ($7 == "3A8") { printf("%s", $7) }' /etc/conf/cf.d/sdevice |read BAS_6
fi
}
#
#
#
#
vec1()
{
typeset -i C=0
if [ $SHOW -ne 0 ]; then
echo '     \c'
if [ $ACT_VEC5 -ne 1 ]; then
   /etc/conf/bin/idcheck -v 5
   if [ $? -eq 0 -o $ACT_VEC5 -eq 5 ]; then
        echo '5\c' 
        C=$C+1
   fi
fi
if [ $ACT_VEC9 -ne 1 ]; then
   /etc/conf/bin/idcheck -v 9
   if [ $? -eq 0 -o $ACT_VEC9 -eq 9 ]; then
     echo ' 9\c' 
     C=$C+1
   fi
fi
if [ $ACT_VECa -ne 1 ]; then
   /etc/conf/bin/idcheck -v 10
   if [ $? -eq 0 -o $ACT_VECa -eq 10 ]; then
     echo ' 10\c' 
     C=$C+1
   fi
fi
if [ $ACT_VECb -ne 1 ]; then
   /etc/conf/bin/idcheck -v 11
   if [ $? -eq 0 -o $ACT_VECb -eq 11 ]; then
     echo ' 11' 
     C=$C+1
   fi
fi
if [ $C -eq 0 ]; then
   echo 'ERROR: No more free interrupts'
   out_here
fi
echo '\n'
while echo 'Please type a number: \c' 
do
read IRQ
case $IRQ in
     5)  if [ $ACT_VEC5 -ne 1 ]; then
              ACT_VEC5=1
              break
           fi
           ;;
     9)  if [ $ACT_VEC9 -ne 1 ]; then
              ACT_VEC9=1
              break
           fi
           ;;
     10)  if [ $ACT_VECa -ne 1 ]; then
              ACT_VECa=1
              break
           fi
           ;;
     11)  if [ $ACT_VECb -ne 1 ]; then
              ACT_VECb=1
              break
           fi
           ;;
     *) echo '\c'
           ;;
esac 
done
fi
iobase
return IRQ
}
#
#
#
vec2()
{
if [ $SHOW -ne 0 ]; then
echo '     \c'
#awk '($1 == "capi") && ($2 == "Y") { printf("%d\n", $6) }' /etc/conf/cf.d/sdevice |read ACT_VEC
typeset -i ACT_VEC5=0
typeset -i ACT_VEC9=0
typeset -i ACT_VECa=0
typeset -i ACT_VECb=0
typeset -i ACT_VECc=0
typeset -i ACT_VECf=0
capi_installed
/etc/conf/bin/idcheck -v 5
if [ $? -eq 0 -o $ACT_VEC5 -eq 5 ]; then
echo '5\c' 
fi
/etc/conf/bin/idcheck -v 9
if [ $? -eq 0 -o $ACT_VEC9 -eq 9 ]; then
echo ' 9\c' 
fi
/etc/conf/bin/idcheck -v 10
if [ $? -eq 0 -o $ACT_VECa -eq 10 ]; then
echo ' 10\c' 
fi
/etc/conf/bin/idcheck -v 11
if [ $? -eq 0 -o $ACT_VECb -eq 11 ]; then
echo ' 11\c' 
fi
/etc/conf/bin/idcheck -v 12
if [ $? -eq 0 -o $ACT_VECc -eq 12 ]; then
echo ' 12\c' 
fi
/etc/conf/bin/idcheck -v 15
if [ $? -eq 0 -o $ACT_VECf -eq 15 ]; then
echo ' 15' 
fi
while echo '\nPlease type a number: \c' 
do
read IRQ
case $IRQ in
     [579])break;;
     10)  break;;
     11)  break;;
     12)  break;;
     15)  break;;
     *);;
esac
done
fi
case $TYP in
      [4]) iobase
           break;;
      [56])slot
           smspace
           break;;
esac
}
#
#
#
#  Check the bustyp
#
bus_typ()
{
open
echo 'Do you want to configure the ix1-adapter(s) for:\n' 
echo '1: ISA  - bussystem'
echo '2: EISA - bussystem' 
echo '3: Only ITK ix1 adapter(s) for PCI - local bus are in use'
while echo '\nPlease type [1-3]:\c '
do
read BUS
case $BUS in
    1) ISCHEME=1 
       BUSTYPE=0
       capi_installed
       break;;
    2) ISCHEME=2 
       #if [ "$OSTYP" = "SINIX" ]; then
       SCHEME=4
       #fi
       break;;
    3) ISCHEME=0 
       BUSTYPE=1
       break;;
    *) open
       echo 'Do you want to configure the ix1-adapter(s) for:\n' 
       echo '1: ISA  - bussystem'
       echo '2: EISA - bussystem' 
       echo '3: Only ITK ix1 adapter(s) for PCI - local bus are in use'
esac
done
}
#
#
# Show the result of choosen parameters
#
result()
{
open
typeset -i HELP=0
HELP=$CC+1
echo 'Topical list of adapter(s) to install : \n\n'
echo 'No.\tADAPTERTYPE\t\tINT\tI/O addresses\tmemory addresses'
echo '\t\t\t\t\tstart\tend\tstart\tend'
echo '--------------------------------------------------------------------------'
#STR=$STRI$CC'\t'$ADAPTER'\t'$IRQ'\t'$IOBASE'\t'$IOEND'\t'$SMSTART'\t'$SMEND'\n' 
STR=$HELP'\t'$ADAPTER'\t'$IRQ'\t'$IOBASE'\t'$IOEND'\t'$SMSTART'\t'$SMEND'\n' 
echo $STRI
echo $STR
#echo ' 1\t'$ADAPTER'\t'$IRQ'\t'$IOBASE'\t'$IOEND'\t'$SMSTART'\t'$SMEND
echo '\n\n'

while echo '\nAre these parameters ok ? [y] or do you want to exit [q]\c'
do
read CHECK
case $CHECK in
    [yY]) STRI=$STRI$STR 
          return;;
    [qQ]) #setcolor -n
          out_here;;
    *) result 
       return
       ;;
esac
done
}
#
#
#
start_for_one_card()
{
open
if [ $BUSTYPE -eq 0 ]; then
   typ
   result
###if [ $ISCHEME -eq 1 ]; then
  ###     MORE=3
    ###   return
###fi
   while echo '\nDo you want to configure for one more card? [y/n]\c'
   do
   read OK
   case $OK in
      [yY])generate_files
           MORE=1
           break
           ;;
     [nN])  
           MORE=3
           break
           ;;
        *) open
           ;;
   esac
   done
else
   MORE=3
fi
}
#
# Check, if the nesessary files are installed
#
check_files()
{
open
if [ -f /tmp/capi20/Driver.o -a $NEWI -eq 1 ]; then
   return
fi
if [ -d /tmp/capi20 -a $NEWI -eq 0 ]; then
   rm -r /tmp/capi20
   mkdir /tmp/capi20
elif [ ! -d /tmp/capi20 ]; then
   mkdir /tmp/capi20
fi
if [ -f ./Driver.* ]; then
  cp Driver.* /tmp/capi20
  if [ -f itk.h ]; then
     NEWI=0
  else
     open
     echo 'Please type the path, where I can find the file itk.h:\c'
     read INPATH
  fi
else
  if [ -f /etc/conf/pack.d/capi20/Driver.o ]; then
     while echo 'Do you want to reconfigure your ITK-CAPI20-Driver installation\n\nusing the installed driver? [y/n]:\c'
     do
     read YN
     case $YN in
          [yY]) cp /etc/conf/pack.d/capi20/Driver.o /tmp/capi20
                open
                if [ -f /etc/conf/pack.d/capi20/space.c ]; then
                   grep itk\.h /etc/conf/pack.d/capi20/space.c | \
                   sed s/#include// | sed s/\"// | sed s/\"// |  \
                   sed s/\\/itk\.h// | read INPATH
                   RECON=1
                else
                   echo 'Please type the path, where I can find the file itk.h:\c'
                   read INPATH
                fi
                if [ ! -f $INPATH/itk.h ]; then
                     rm /tmp/capi20/*
                     out_here
                fi
                NEWI=1
                break
                ;;
          [nN]) out_here
                ;;
             *) open
     #           echo 'Do you want to reconfigure your ITK-CAPI20-Driver installation\n\nusing the installed driver? [y/n]:\c'
     esac
     done
  else
     echo 'ERROR: No Driver.o file found'
     out_here
  fi 
fi
 return OK
}
#####################################################
#                                                   #
# Generate new Node, System, space.c, Master-files  #
#                                                   #
#####################################################
generate_files()
{
cd /tmp/capi20
echo 'clone	capi20	c	capi20' > ./Node
echo 'capi20	ixl	c	0' >> ./Node
#
#
if [ $CC -eq 0 ]; then
   echo 'capi20	I	iHcS	capi20	0	0	1	8	-1' > ./Master
   echo '/*-----------------------------------------------------------+-------*/'> ./Space.c
   echo '/*                                                                   */'>> ./Space.c
   echo '/*   ITK Telekommunikations AG                                       */'>> ./Space.c
   echo '/*   Emil-Figge-Strasse 80                                           */'>> ./Space.c
   echo '/*   D-44247 Dortmund                                                */'>> ./Space.c
   echo '/*   Telefon 0231/9747-0                                             */'>> ./Space.c
   echo '/*                                                                   */'>> ./Space.c
   echo '/*   Copyright (C) 1995-1997 ITK AG, Dortmund                        */'>> ./Space.c
   echo '/*-------------------------------------------------------------------*/'>> ./Space.c
   echo '\n#include <sys/types.h>'>> ./Space.c
   echo '#include "'$INPATH'/itk.h"'>> ./Space.c
   echo '#include "config.h"\n'  >> ./Space.c
   if [ $BUSTYPE -eq 1 ]; then
       echo '\nitkparam itkparams[MAX_CARDS];'>> ./Space.c
   else
       echo '\nitkparam itkparams[MAX_CARDS]='>> ./Space.c
       echo '{' >> ./Space.c
   fi
fi
#
if [ $SCHEME -ne 4 ]; then
    SCHEME=$ISCHEME
fi
#echo 'capi20	Y	1	'$IPL'	'$SCHEME'	'$IRQ'	'$IOBASE'	'$IOEND'	'$SMSTART'	'$SMEND >> ./System
echo 'capi20	Y	1	'$IPL'	4	'$IRQ'	'$IOBASE'	'$IOEND'	'$SMSTART'	'$SMEND >> ./System

OCTO_STR=$OCTO_STR' '$OCTO
OCTO='0x0'

if [ $BUSTYPE -ne 1 ]; then
   echo '{'$REV', '$HwTyp', '$SLOT', CAPI20_'$CC'_SIOA, CAPI20_'$CC'_EIOA, CAPI20_'$CC'_VECT, CAPI20_'$CC'_IPL, '$SCRATCH', CAPI20_'$CC'_SCMA, CAPI20_'$CC'_ECMA}\c' >>  ./Space.c
fi

CC=$CC+1

if [ $MORE -eq 3 ]; then
   if [ $BUSTYPE -ne 1 ]; then
      echo '\n};' >>  ./Space.c
      echo '\nint s_cardcounter='$CC';'>> ./Space.c
   else
      echo '\nint s_cardcounter=0;'>> ./Space.c
   fi
#   if [ $BUSTYPE -eq 1]; then
#      CC=$CC+1
#   fi
   if [ $CC -ne 8 ]; then
      OCTO_STR=$OCTO_STR','
   fi
   while [ $CC -ne 8 ]
   do
        OCTO_STR=$OCTO_STR' '$OCTO
        if [ $CC -ne 7 ]; then
            OCTO_STR=$OCTO_STR','
        fi
        CC=$CC+1
   done 
   echo $OCTO_STR'};' >> ./Space.c
else 
   echo ', ' >> ./Space.c
   OCTO_STR=$OCTO_STR','
fi
}
############################################
#                                          #
# Configure and build new system           #
#                                          #
############################################
build_config()
{
cd /tmp/capi20
if [ -f /etc/conf/pack.d/capi20/Driver.o -o -f /etc/conf/pack.d/capi20/Driver.a ]; then
          /etc/conf/bin/idinstall -u capi20
          if [ $? -ne 0 ]; then
              echo 'ERROR: return value /etc/conf/bin/idinstall = '$?
            out_here
          fi
          if [ -f /etc/conf/pack.d/capi20/Driver.a ]; then
             mv /etc/conf/pack.d/capi20/Driver.a /etc/conf/pack.d/capi20/Driver.o
          fi
else
         /etc/conf/bin/idinstall -a capi20
          if [ $? -ne 0 ]; then
              echo 'ERROR: return value /etc/conf/bin/idinstall = '$?
              out_here
          fi
fi
open
echo '\nNow your system is new configured.\n'
echo 'If you want to use the CAPI20-Driver, the system must be rebuild\n\n'
while echo 'Do you want to rebuild the system now [y/n]?: \c'
do
read CHECK
case $CHECK in
    [yY]) /etc/conf/bin/idbuild
          if [ $? -ne 0 ]; then
              echo 'ERROR: return value /etc/conf/bin/idbuild = '$?
              out_here
           fi
          open    
          echo '\nNow your system is rebuild, to use the new installation, you have to reboot\n'
          break
          ;;
    [nN]) #setcolor -n 
          open 
          echo '\nThe configuration is finished. To rebuild your system type \n'
          echo '\n /etc/conf/bin/idbuild\n\n\n'
          out_here 
          ;;
     *);;
esac
done
}
#
#
#
move_initfile()
{


if [ -f /etc/rc2.d/S91IX ]; then
   return
else 
   echo ':' > ./S91IX
   echo '#  startup-file for ITK-ISDN-Adapter' >> ./S91IX
   echo '#' >> ./S91IX
   echo '#' >> ./S91IX
   echo '#' >> ./S91IX
   echo '#' >> ./S91IX
   echo 'cd '$IPATH'/ixload/bin' >> ./S91IX
   echo 'if [ -f ./*.bin ]; then' >> ./S91IX
   echo '   if [ -f ./ixload ]; then' >> ./S91IX
   echo '         echo 'Loading ITK-ISDN-Adapter........'' >> ./S91IX
   echo '         exec ./ixload -s' >> ./S91IX
   echo '   fi' >> ./S91IX
   echo 'fi' >> ./S91IX
   echo 'exit' >> ./S91IX
   chmod 744 ./S91IX 
   mv ./S91IX /etc/rc2.d/S91IX
fi
}
#
#
#
inst_initfile()
{
ANS=''
   open
   echo 'To use the ITK-ISDN-adapter(s), you have to load these adapter(s) first.\n'
   echo 'This can be done at boottime by your system automaticly.\n'
   while echo 'Do you want to use these automatical mechanism ? [y/n]\c'
   do 
   read ANS
   case $ANS in
        [yY]) move_initfile
              break;;
        [nN]) break;; 
           *) ;;
   esac
   done
}
#
#
#
out_here()
{
PATH=$OLDPATH
exit
}
#
#
#
check_for_tar()
{
if [ -f ./capi20.tar.Z ]; then
    open
    echo 'Where do you want to install this package ? (The relativ path is itk/*)'
    echo 'If you just type RETURN, I will take the relativ path for the absolute'
    echo 'otherwise please type an existing absolute path:\c'
    read  INPATH
    uncompress ./capi20.tar
    if [  "$NULL" = "$INPATH" ]; then
        mv capi20.tar  /capi20.tar
        cd /
        tar xvf /capi20.tar
    else
        if [ ! -d $INPATH ]; then
            echo 'ERROR: Path not existing'
            out_here
        fi
        cp capi20.tar $INPATH
        compress capi20.tar
        cd $INPATH
        tar xvf $INPATH/capi20.tar
    fi
    NEWI=1
else
    NEWI=0
fi
}
#
#
#
check_os()
{
case `uname -s` in
       SINIX-Z*) OSTYP=SINIX
                 break
                 ;;
               *) case `uname -r` in
                        3.2*) OSTYPE=SCO
                              break
                              ;;
                        5.42*) OSTYPE=SINIX
                              break
                              ;;
                            *) out_here
                   esac            
esac                            
}
#
#
#
install_path()
{
if  [ $NEWI -eq 1 ]; then
    return
fi
open
echo 'Do you want to use the default path (/usr/itk/*) to install the '
while echo 'include-files, etc. ? [y/n]\c:'
do
read IN
case $IN in
        [yY]) break
              ;;
        [nN]) open
              echo 'Please type the new installation path: (NOTE: Path must exist)\n'
              read INPATH
              break
              ;;
           *) open
              echo 'Do you want to use the default path (/usr/itk/*) to install the '
              ;;
esac
done
}
#
check_older_installation()
{
typeset -i OI=0
open
if [ -f  /etc/conf/sdevice.d/capi20 ]; then
   OI=1
fi
grep "capi20	" /etc/conf/cf.d/mdevice > /dev/null
if [ $? -eq 0 ]; then
   OI=1
fi
if [ -d /etc/conf/pack.d/capi20 ]; then
   if [ -f /etc/conf/pack.d/capi20/Driver* ]; then
      if [ $OI -eq 1 ];then
           cp /etc/conf/pack.d/capi20/Driver* /tmp
           echo '\nFound an old ITK capi driver and make a copy in /tmp'
      fi
   fi
fi
if [ OI -eq 1 ]; then
   echo 'Seaching for an older installation of an ITK capi driver.....'
   echo '\nFound an old installation'
   echo '\nTo use the new one, the old installation must be removed'
   while echo '\nRemove now ? [y/n]\c:'
   do
   read IN
   case $IN in
           [yY]) /etc/conf/bin/idinstall -d capi
                 break
                 ;;
           [nN]) open
                 echo 'You decide NOT to change your installation'
                 out_here
                 break
                 ;;
            *)   open
                 echo 'Seaching for an older installation of an ITK capi driver.....'
                 echo '\nFound an old installation'
                 echo '\nTo use the new one, the old installation must be removed'
                 ;;
    esac
    done   
fi
}
#
#
c_lean()
{
### thg
echo .
# rm INSTALL 
}
#
###############################################
#     main                                    #
###############################################
ACTPATH=$PWD
OLDPATH=$PATH
PATH=$PATH:/bin:/usr/bin:/etc/:/etc/conf/bin
check_os
#check_older_installation
check_for_tar
if [ $NEWI -eq 1 ]; then
   IPATH=$INPATH/itk
   INPATH=$INPATH/itk/inc
fi
check_files
bus_typ
install_path
while [ $MORE -ne 3 ]
do
  start_for_one_card
done
generate_files
inst_initfile
build_config
if [ $RECON -eq 0 ]; then
### thg
echo .
#   rm ./capi20.tar
fi
cd $ACTPATH
if [ $RECON -eq 0 ]; then
   echo '\n\nFor further modifications of your ITK-CAPI20-Driver installation use the'
   echo 'INSTALL script in '$IPATH'/bin\n\n'
### thg
#   rm ./INSTALL ./capi20.tar.Z
fi
out_here
exit
