#!/bin/bash
#**********@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@**********************************
# Copyright (C) 2001-2006 Dialogic Corporation. All Rights Reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1.      Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2.      Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3.      Neither the name Dialogic Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#***********************************@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@**********/
######################################################################
#
# Script to stop the Dialogic(R) drivers
#
#######################################################################

sw_unload(){
    FOUND=0
    for driver in dvbm sctmr gpio gncfd dlgn 
    do
       /sbin/lsmod  | grep ${driver} 2>&1 >/dev/null
       RC=$?
       if [ $RC -eq 0 ]
       then
          FOUND=1
          /sbin/rmmod streams-${driver}Driver 2>&1 >/dev/null
       fi
    done
 
    if [ $FOUND -eq 1 ]
    then
       rm -f /dev/dlgn /dev/gncfd /dev/d_gn_cnfg /dev/gpioDRV 
       /sbin/rmmod streams 2>&1 >/dev/null
       echo "Springware Drivers Unloaded"
    fi
}

dm3_unload(){
    /sbin/lsmod | grep mercd 2>&1 >/dev/null
    RC=$?
    if [ $RC -eq 1 ]
    then 
       return
    fi
   
    /sbin/rmmod mercd 2>&1 >/dev/null
    RC=$?
    if [ $RC -eq 0 ]
    then
       rm -f /dev/mercd
       echo "DM3 Driver Unloaded"
    fi
}

pmac_unload(){
    /sbin/lsmod | grep pmac 2>&1 >/dev/null
    RC=$?
    if [ $RC -eq 1 ]
    then 
       return
    fi
    
    /sbin/rmmod pmac 2>&1 >/dev/null
    RC=$?
    if [ $RC -eq 0 ]
    then
       rm -f /dev/pmac_stream
       rm -f /dev/pmac_queue
       rm -f /dev/pmac_control
       echo "PMAC Driver Unloaded"
    fi
}

ctimod_unload(){
    /sbin/lsmod | grep ctimod 2>&1 >/dev/null
    RC=$?
    if [ $RC -eq 0 ]
    then
       /sbin/rmmod ctimod 2>&1 >/dev/null
    fi
}	
    

###
### START OF SCRIPT
###

# process command line arguments
case $1 in
   'hotswap')
      # There is nothing special to do at this point. 
      exit 0
      ;;

   'dm3')
      dm3_unload
      ctimod_unload
      ;;

   'pmac')
      pmac_unload
      ctimod_unload
      ;;

   'springware')
      sw_unload
      ;;

   *)
      dm3_unload
      pmac_unload
      sw_unload
      ctimod_unload
      ;;

esac

