#!/bin/bash
#
#####################################################################
#    Copyright (C) 2001-2008 Dialogic Corporation.
#
#    All Rights Reserved.  All names, products,
#    and services mentioned herein are the trademarks
#    or registered trademarks of their respective organizations
#    and are the sole property of their respective owners.
#
#####################################################################


#####################################################################
#
#  Shell script: Start a single board
#
#####################################################################

# Check for file containing our environment variables.
# If it is not found, abort the execution.

if [ -z "${INTEL_DIALOGIC_DIR}" ]
then
   OURVARS=/etc/profile.d/ct_intel.sh
   if [ ! -x ${OURVARS} ]
   then
	   echo "$0: FATAL ERROR: Unable to find ${OURVARS}."
	   echo "$0: FATAL ERROR: Execution aborted due to invalid installation."
	   echo "$0: FATAL ERROR: Please reinstall the software by running install.sh."
	   exit
   fi

   # Get our environment variables
   . ${OURVARS}
fi

TECHSCRIPTS=${INTEL_DIALOGIC_DIR}/init.d/ctplatform

# Operating mode
MODE_NONE=0
MODE_BUS_SLOT=1
MODE_PHYS_SLOT=2
MODE_AUID=3
MODE=$MODE_NONE;
RESET=0

# Dummy values
PCI_BUS=9999
PCI_SLOT=9999
PHYS_SLOT=0
AUID=0
TECH=NONE

# Logging
LOG=0

######################################################################
# Verify startup script was called
######################################################################
verifystart() {
   
    # See if device mapper is running
    ps -ea | grep devmapserver > /dev/null
    RC=$?
    if [ $RC -eq 1 ] ; then
	echo  "System must be started first with dlstart"
	exit 1
    fi

}


######################################################################
# Get command line options
######################################################################
GetArgs() {

    NO_ARGS=0
    OPTERROR=1
    BOARDMODE=0
    SCRIPTNAME=`basename $0`

    if [ $SCRIPTNAME == "diagbrd" ]
    then
       USAGE="Usage: `basename $0` -p<physical slot> [-r]|-b<pcibus> -s<pcislot> [-r]|-a<auid> [-r]"
    else
       USAGE="Usage: `basename $0` -p<physical slot>|-b<pcibus> -s<pcislot>|-a<auid>"
    fi

    if [ $# -eq 0 ]
    then
      echo "No options specified"
      echo $USAGE
      exit $OPTERROR       
    fi

    if [ $SCRIPTNAME == "diagbrd" ]
    then
       while getopts "a:b:s:p:hr" Option
       do
         case $Option in
         b     )  MODE=$MODE_BUS_SLOT;
		   PCI_BUS=$OPTARG;
		   ;;
         s     )  MODE=$MODE_BUS_SLOT
		   PCI_SLOT=$OPTARG;
		   ;;
         p     )  MODE=$MODE_PHYS_SLOT
		   PHYS_SLOT=$OPTARG;
		   ;;
         a     )  MODE=$MODE_AUID
		   AUID=$OPTARG;
		   ;;
         r     )  RESET=1
         ;;
         h     ) 
               echo $USAGE;
               exit 1;; 
         *     ) echo "Unimplemented option chosen.";
               echo $USAGE;
               exit 1;; 
         esac
       done 
    else
       while getopts "a:b:s:p:h" Option
       do
         case $Option in
         b     )  MODE=$MODE_BUS_SLOT;
		   PCI_BUS=$OPTARG;
		   ;;
         s     )  MODE=$MODE_BUS_SLOT
		   PCI_SLOT=$OPTARG;
		   ;;
         p     )  MODE=$MODE_PHYS_SLOT
		   PHYS_SLOT=$OPTARG;
		   ;;
         a     )  MODE=$MODE_AUID
		   AUID=$OPTARG;
		   ;;
         h     ) 
               echo $USAGE;
               exit 1;; 
         *     ) echo "Unimplemented option chosen.";
               echo $USAGE;
               exit 1;; 
         esac
       done 
    fi

    shift $(($OPTIND - 1))

    # Bus/slot, make sure we have both args
    if [ $MODE -eq $MODE_BUS_SLOT ]
    then
	if [ $PCI_BUS -eq 9999 ]
	then
	    echo "Must set PCI bus number with -b option"
	    exit 1
	fi
	if [ $PCI_SLOT -eq 9999 ]
	then
	    echo "Must set PCI slot number with -s option"
	    exit 1
	fi
    fi
}

stop_log(){

   if [ $LOG -ne 0 ]
   then
	   # Stop logging
	   sleep 2
	   kill -1 $TAILPID
   fi
}

start_log(){

   LOG=1

   # Remove old logfile
   rm -f ${INTEL_DIALOGIC_DIR}/log/${SCRIPTNAME}.log.* > /dev/null

   # Redirect IO to log file
   cat /dev/null > ${INTEL_DIALOGIC_DIR}/log/${SCRIPTNAME}.log.$$
   teelogger -s -f ${INTEL_DIALOGIC_DIR}/log/${SCRIPTNAME}.log.$$ &
   TAILPID=$!

   exec >> ${INTEL_DIALOGIC_DIR}/log/${SCRIPTNAME}.log.$$ 
   exec 2>&1

   # delete log file on control c
   trap "stop_log;exit 1" 2
}

# 
# Computes AUID based on command line args
# 
# RC set with 0 on success or non-zero on failure
# AUID set to correct AUID on return
#
GetAUID(){
    # Get AUID value
    if [ $MODE -eq $MODE_BUS_SLOT ]
    then
        AUID=`${INTEL_DIALOGIC_BIN}/devmapget -b $PCI_BUS -o $PCI_SLOT`
        RC=$?
    fi
    if [ $MODE -eq $MODE_PHYS_SLOT ]
    then
        AUID=`${INTEL_DIALOGIC_BIN}/devmapget -p $PHYS_SLOT`
        RC=$?
    fi
    if [ $MODE -eq $MODE_AUID ]
    then 
        # Verify AUID is blade
        AUIDTYPE=`${INTEL_DIALOGIC_BIN}/devmapget -a $AUID -kTYPE -L`    
        RC=$?
        if [ $RC -eq 0 ]
        then
            # Devmap call succeeded verify it is a blade
            if [ $AUIDTYPE -eq 200 ]
            then
                RC=0
            else
                RC=1
            fi
        fi
    fi


}

#
# Compute technology based on AUID
#
# RC set with 0 on success or non-zero on failure
# TECH set to correct technology on return
#
GetTechnology() {
    # Get Technology
    TECH=`${INTEL_DIALOGIC_BIN}/devmapget -a $AUID -kCTPLATFORM -s`
    RC=$?
    if [ $RC -ne 0 ]
    then
        echo "Board not found at location specified"
        return
    fi

    # Verify technology script
    if [ ! -f $TECHSCRIPTS/$TECH ]
    then
        echo "Script for this technology not found.  Is installation correct?" 
        RC=1
        return
    fi
    
    # Good exit
    RC=0
}



######################################################################
# Beginning of the script
######################################################################

RC=0
SCRIPTNAME=`basename $0`

# Parse Command Line
GetArgs $*

# Verify system is up and running
verifystart

# Start logging
start_log

# Get the AUID
GetAUID
if [ $RC -ne 0 ]
then
    echo "Board not found at location specified"
    stop_log
    exit 1
fi

# Get the technology of the AUID
GetTechnology
if [ $RC -ne 0 ]
then
    stop_log
    exit 1
fi

case $SCRIPTNAME in
    "stopbrd" )

        echo "Stopping board"

        ${INTEL_DIALOGIC_BIN}/evtgen -a $AUID -eASTOP
        # Call sub-script
        $TECHSCRIPTS/$TECH stopauid $AUID
        RC=$?
        if [ $RC -eq 0 ]
        then
            ${INTEL_DIALOGIC_BIN}/evtgen -a $AUID -eSTOP
        fi

        ;;

    "startbrd")
        EVT_POST="START"
        echo "Starting board"

        ${INTEL_DIALOGIC_BIN}/evtgen -a $AUID -eASTART
        # Call sub-script
        $TECHSCRIPTS/$TECH startauid $AUID
        RC=$?
        if [ $RC -eq 0 ] ; then
            CLK_MODE=`${INTEL_DIALOGIC_BIN}/scdget -t TDM --key ClockDaemonMode`
            case $CLK_MODE in 
            DISABLED)
                echo "Clocking daemon disabled"
                ${INTEL_DIALOGIC_BIN}/evtgen -a $AUID -eSTART
                ;;
            *)
                # Add board to bus
                cdaemon_ctl -a $AUID
                RC=$?
                if [ $RC -eq 0 ] ; then
                    ${INTEL_DIALOGIC_BIN}/evtgen -a $AUID -eSTART
                fi
                ;;
            esac
        fi

        ;;

    "diagbrd")
        EVT_POST="NONE"
        # Call sub-script
        $TECHSCRIPTS/$TECH post $AUID $RESET

        RC=$?
        ;;

    "removebrd" )
        EVT_POST="NONE"
        echo "Removing board"
        $TECHSCRIPTS/$TECH removeauid $AUID
        RC=$?
        ;;

    * )
        echo "$SCRIPTNAME is named incorrectly"
        RC=1;;
esac


stop_log; 
exit $RC

