#!/bin/sh
#
# Copyright(c) 2000 Broadcom Corporation, all rights reserved
#
# Name:     BCM5820 - Broadcom BCM5820 Cryptonet init script
#           This script is part of Broadcom Cryptonet
#           driver for Linux.
#
# Author:   Peter Wang 
#
# Created:  May 11, 2001
#

# Tags 
# chkconfig: - 30 70
# description: Hardware cryptographic accelerator support - BCM5820 Cryptonet driver 

PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Source function library
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

module=bcm5820

add() {
        awk '/^[:space:]*\[.*\][:space:]*$/ {section=$0;gsub(/^[\[[:space:]]*/,"",section);gsub(/[\][:space:]]*$/,"",section)} \
            /^default = openssl$/ {if(section=="engine") {print "default = ubsec";next}} \
            {print $0}' /usr/share/ssl/openssl.cnf > /usr/share/ssl/openssl.cnf.new && \
        cat /usr/share/ssl/openssl.cnf.new > /usr/share/ssl/openssl.cnf && rm /usr/share/ssl/openssl.cnf.new
}

remove() {
    awk '/^[:space:]*\[.*\][:space:]*$/ {section=$0;gsub(/^[\[[:space:]]*/,"",section);gsub(/[\][:space:]]*$/,"",section)} \
        /^default = ubsec$/ {if(section=="engine") {print "default = openssl";next}} \
        {print $0}' /usr/share/ssl/openssl.cnf > /usr/share/ssl/openssl.cnf.new && \
        cat /usr/share/ssl/openssl.cnf.new > /usr/share/ssl/openssl.cnf && rm /usr/share/ssl/openssl.cnf.new
}

start() {
    action $"Loading $module module" insmod ${module}
    RETVAL=$?
    if [ $RETVAL = 0 ] ; then
        rm -f /dev/cryptonet
        mknod -m666 /dev/cryptonet c `grep "cryptonet" /proc/devices | head -1 | awk '{print $1}'` 1
	add
        touch /var/lock/subsys/bcm5820
    else
        remove
    fi
}

stop() {
    remove
    action "Unloading $module module" rmmod ${module}
    RETVAL=$?
    if [ $RETVAL = 0 ] ; then
        rm -f /dev/cryptonet
    fi
    rm -f /var/lock/subsys/bcm5820
}

RETVAL=0
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
	if /usr/bin/b58diag > /dev/null ; then
		echo BCM5820 driver loaded.
		/usr/bin/b58stats -n
	else
		echo BCM5820 driver not loaded.
	fi
        ;;
    condrestart)
        if grep -q "cryptonet" /proc/devices ; then
                stop
                start
        fi
        ;;
    *)
        echo Broadcom BCM5820 init script
        echo Copyright \(c\) 2001 Broadcom Corporation
        echo usage: `basename $0` '[start|stop|restart|status|condrestart]'
        RETVAL=1
        ;;
esac

exit $RETVAL