#!/bin/bash
#
# Execute the utilities and scripts used to populate PDR database
# for component initial values

RETVAL=0
RC=0
LOGFILE=/var/log/nn_startup

. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/init_functions
. /etc/profile.d/SetNNEnvVar.sh

# See how we were called.

prog="Pdrd_db_init"

echo $"Checking dependencies for $prog."
testforpid $prog

if [ -f /nn/etc/scd/init_db ]; then
    # Execute the one-time only initialization utilities

    echo `date` Pdrd_db_init: populating the Pdrd database for initial values >> $LOGFILE

    # Published IP entries - /nn/bin/published_ip_pdr
    if [ -e /nn/bin/BCM_DataServicesMigrationTo_5_0 ]; then
        /nn/bin/BCM_DataServicesMigrationTo_5_0
    fi
    # DNS Pdrd entries - /nn/bin/dns_change_notification_pdr
    if [ -e /nn/bin/dns_change_notification_pdr ]; then
        /nn/bin/dns_change_notification_pdr
    fi
    # DSCP Marking - /nn/bin/dscp_marking_pdr
    if [ -e /nn/bin/dscp_marking_pdr ]; then
        /nn/bin/dscp_marking_pdr
    fi
    # CteInit - /nn/bin/CteInit
    if [ -e /nn/bin/CteInit ]; then
        /nn/bin/CteInit
    fi
    # fepspdr - /nn/sbin/fepspdr
    if [ -e /nn/sbin/fepspdr ]; then
        /nn/sbin/fepspdr
    fi
    # mgspdr - /nn/sbin/mgspdr
    if [ -e /nn/sbin/mgspdr ]; then
        /nn/sbin/mgspdr
    fi
    # JBSettingsMgr - /nn/sbin/JBSettingsMgr
    if [ -e /nn/sbin/JBSettingsMgr ]; then
        /nn/sbin/JBSettingsMgr
    fi
    # qmondpdr - /nn/sbin/qmondpdr
    if [ -e /nn/sbin/qmondpdr ]; then
        /nn/sbin/qmondpdr
    fi
    # BCM Amp - /nn/sbin/bcmAmpPdrInit
    if [ -e /nn/sbin/bcmAmpPdrInit ]; then
        /nn/sbin/bcmAmpPdrInit
    fi

    # Populate the high level product information.
    /nn/bin/pdrcmd create "Platform Services\Product Version" BCM50 STR
    /nn/bin/pdrcmd create "Platform Services\Rls ID" 5.0 STR

    # Populate the software release information.
    release=`/usr/local/bin/rpm -q --queryformat '%{VERSION}\n' system-release`
    if [ $? -eq 0 ] && [ -n "$release" ]; then
        /nn/bin/pdrcmd create "Platform Services\Release" "$release" STR
    else
        echo `date` Pdrd_db_init: could not determine the release software version from RPM >> $LOGFILE
    fi

    # Populate the available disk space for voice filesystem usage.
    /nn/bin/pdrcmd create "Platform Services\disk partition\max data space" 3390716 INT

    # Remove the flag indicating that this initialization is required
    rm -f /nn/etc/scd/init_db

elif [ -f /nn/etc/scd/Pdrd_upgrade_db_init.sh ]; then
    # Execute the one time upgrade db initialize script

    echo `date` Pdrd_db_init: populating the Pdrd database after upgrade >> $LOGFILE
    chmod u+x /nn/etc/scd/Pdrd_upgrade_db_init.sh
    /bin/bash -x /nn/etc/scd/Pdrd_upgrade_db_init.sh >> $LOGFILE

    # Published IP entries - /nn/bin/published_ip_pdr
    if [ -e /nn/bin/BCM_DataServicesMigrationTo_5_0 ]; then
        /nn/bin/BCM_DataServicesMigrationTo_5_0
    fi

    # Remove the script in order that initialization doesn't occur again
    rm -f /nn/etc/scd/Pdrd_upgrade_db_init.sh

else
    # Perform the recurring activities which occur on each reboot

    # Delete any contents in the inventory/hardware section of Pdrd.
    /nn/bin/pdrcmd delete "inventory\hardware"

    # Determine if the software release value currently in Pdrd matches the actual release value.
    currentPdrd=`/nn/bin/pdrcmd get "Platform Services\Release"`
    release=`/usr/local/bin/rpm -q --queryformat '%{VERSION}\n' system-release`

    if [ $? -eq 0 ] && [ -n "$release" ] && [ "$release" != "$currentPdrd" ]; then
        echo `date` Pdrd_db_init: updating the Pdrd software release version to $release >> $LOGFILE
        if [ -n "$currentPdrd" ] && [ "$currentPdrd" != "Retrieve Failed" ]; then
            /nn/bin/pdrcmd modify "Platform Services\Release" "$release"
        else
            /nn/bin/pdrcmd create "Platform Services\Release" "$release" STR
        fi
    fi 
fi

exit $?
