#! /bin/sh
######################################################################
#
#  Shell script: DxxJCT_id
#                Display ID on D/XXJCT-U Board(s)
#                Also display on console
#
#
#      Copyright 2001 Intel Corp. All Rights Reserved
######################################################################
USAGE="Usage: $0"

DLGCROOT=/usr/dialogic
CFGFILE=$DLGCROOT/cfg/dialogic.cfg
CFGFILETMP=$DLGCROOT/cfg/dialogic.cfg.tmp


### Check for proper usage
if [ $# != 0 ]
then
    echo
    echo $USAGE
    echo
    exit 1
fi


### Check if user is root
if [ `id -u` != 0 ]
then
    echo This program requires you to be root.
    echo Please login as root and re-run $0.
    exit 1
fi


### Check if config file exists
if [ ! -f $CFGFILE ]
then
    echo
    echo $CFGFILE is missing
    echo Please run mkcfg first
    echo
    exit 1
fi


### Display message
echo
echo "Determining the D/xxJCT series board ID(s)"
echo Please standby...
echo


### Stop system
STOP=$DLGCROOT/bin/dlstop
if [ -x $STOP ]
then
    $STOP > /dev/null 2>&1
else
    echo Can not execute $STOP
    exit 1
fi


### Remove board entry file
BENTRY=$DLGCROOT/cfg/.bentry
if [ -f $BENTRY ]
then
    rm -f $BENTRY > /dev/null 2>&1
fi

if [ -f $BENTRY ]
then
    echo Could not delete $BENTRY
    exit 1
fi


### Comment out any D/XXJCT sections in dialogic.cfg
sed '/D\/XXJCT/,/^$/s/^/#TOKEN/' $CFGFILE > $CFGFILETMP
cp $CFGFILETMP $CFGFILE
rm $CFGFILETMP


### Start devmapserver if not running (should not if dlstop successful)
MAPSERVER=$DLGCROOT/bin/devmapserver
DMS=`ps -eaf | grep devmapserver | grep -v grep`
RC=$?
if [ ! -x $MAPSERVER ]
then
    echo Can not execute $MAPSERVER
    exit 1
fi

if [ $RC -ne 0 ]
then
    $MAPSERVER &
fi


### Execute genload to display board ID
GENLOAD=$DLGCROOT/bin/genload
RESULTS=/tmp/genload.jct
if [ -x $GENLOAD ]
then
    $GENLOAD -gr > $RESULTS
else
    echo Can not execute $GENLOAD
    exit 1
fi


### Restore config file
sed 's/#TOKEN//' $CFGFILE > $CFGFILETMP
cp $CFGFILETMP $CFGFILE
rm $CFGFILETMP


### Display ID's to user
grep 'D/82JCT' $RESULTS
grep 'D/42JCT' $RESULTS


### Ask user to alter config file
echo
echo "Please edit $DLGCROOT/cfg/dialogic.cfg"
echo "Set the respective IDs to those just displayed"
echo


# Clean up
rm $RESULTS


exit 0
