#! /bin/sh
### BEGIN INIT INFO
# Provides:          Broadcom_OpenNSL_Library
# Required-Start:    $syslog platform-baseconf
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Broadcom OpenNSL
### END INIT INFO

# Author: Rob Sherwood <rob.sherwood@bigswitch.com>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=" Broadcom OpenNSL Library"
NAME=BRCM_OpenNSL
PLAT_FILE=/etc/onl/platform
PLATFORM=`cat $PLAT_FILE`
if [ -z $PLATFORM ] ; then
    echo "Failed: no platform in $PLAT_FILE"
    exit 0
elif [ $PLATFORM != x86-64-accton-wedge-16x-r0 ] ; then
    echo "Failed: OpenNSL not supported for platform $PLATFORM"
    exit 0
fi

    if [ ! -f /usr/local/lib/libopennsl.so.1 ] ; then
    wget --no-check-certificate "https://raw.githubusercontent.com/Broadcom-Switch/OpenNSL/8e0b499f02dcef751a3703c9a18600901374b28a/bin/wedge-trident/libopennsl.so.1" -O /usr/local/lib/libopennsl.so.1 
    fi
    if [ ! -f /usr/local/lib/libopennsl.so.1 ] ; then
        echo "Error: Still unable to download module, please check that the management ethernet interface (ma1) is up and has internet connectivity then run /etc/init.d/OpenNSL_download"
        exit 0
    fi

# check MD5 of downloaded opennsl binary

MD5_1=$(md5sum '/usr/local/lib/libopennsl.so.1' | cut -d ' ' -f 1)
MD5_2=5243031055ac7d591917967691c20f0b

    if [ $MD5_1 != $MD5_2 ] ; then

        echo "OpenNSL binary corrupt"
	rm /usr/local/lib/libopennsl.so.1
	echo "Please run the program again"
        exit 0
    else
        echo "OpenNSL library succesfully installed"
    fi
:
