#!/bin/ksh
#
# ScienceLogic Agent
#

# The rc interface:

DAEMON=/usr/local/bin/scilogd
LIBNAME=libscilog.so.1.0.1
SHAREDIR=/usr/share/scilog
RAMDIR=/mnt/scilog_ramdisk

create_ram_disk() {

    RAMDISK=`mkramdisk 32M`
    if [ $? -gt 0 ]
    then
        echo "Warning: mkramdisk failed, preload has been disabled. Error: "$?
        exit 0
    fi

    RAMDISKNUM=`echo $RAMDISK | cut -c14-`
    echo "yes" | mkfs -V jfs2  /dev/ramdisk$RAMDISKNUM
    if [ $? -gt 0 ]
    then
        echo "Warning: mkfs failed, preload has been disabled. Error: "$?
        rmramdisk $RAMDISK
        exit 0
    fi

    mkdir -p $RAMDIR
    mount -V jfs2 -o log=NULL /dev/ramdisk$RAMDISKNUM $RAMDIR
    if [ $? -gt 0 ]
    then
        echo "Warning: mount of ramfs failed, preload has been disabled. Error: "$?
        rmramdisk $RAMDISK
        exit 0
    fi
}

ld_preload_chk () {
    export LDR_PRELOAD=/usr/share/scilog/lib/libscilog.so.1.0.1
    # test preload overlay
    $DAEMON -t
    if [ $? -gt 0 ]
    then
        unset LDR_PRELOAD
        echo "Warning: overlay issue, preload has been disabled"
        exit 0
    fi
    unset LDR_PRELOAD

    # create a ram disk to put the libraries.  I need a ram disk
    # because if the RPM is uninstalled there still needs to because
    # LDR_PRELOAD and LDR_PRELOAD64 may still be in the environment.
    # On reboot the ramdisk will go away and there will be no residual

    DF_OUT=`df | grep $RAMDIR`
    if [[ ! -n $DF_OUT ]]
    then
        create_ram_disk
    fi

    # atomically install/update library on ramfs
    mkdir -p $RAMDIR/lib
    cp $SHAREDIR/lib/$LIBNAME $RAMDIR/lib/$LIBNAME.tmp
    chmod 755 $RAMDIR/lib/$LIBNAME.tmp
    # atomic
    mv $RAMDIR/lib/$LIBNAME.tmp $RAMDIR/lib/$LIBNAME

    mkdir -p $RAMDIR/lib64
    cp $SHAREDIR/lib64/$LIBNAME $RAMDIR/lib64/$LIBNAME.tmp
    # atomic
    mv $RAMDIR/lib64/$LIBNAME.tmp $RAMDIR/lib64/$LIBNAME
    

    # backup /etc/profile
    if [[ ! -e /etc/profile.scilog.backup ]]
    then
        cp /etc/profile /etc/profile.scilog.backup
    fi   

    # set LDR_PRELOAD and LDR_PRELOAD64 to point to libs on the ramdisk

    if [[ -e $RAMDIR/lib/libscilog.so.1.0.1 ]]
    then
        LINE="if [[ -e $RAMDIR/lib/libscilog.so.1.0.1 ]]; then export LDR_PRELOAD=$RAMDIR/lib/libscilog.so.1.0.1; fi"
        grep -q -F "$LINE" /etc/profile || echo "$LINE" >> /etc/profile
    fi
    if [[ -e $RAMDIR/lib64/libscilog.so.1.0.1 ]]
    then
        LINE="if [[ -e $RAMDIR/lib64/libscilog.so.1.0.1 ]]; then export LDR_PRELOAD64=$RAMDIR/lib64/libscilog.so.1.0.1; fi"
        grep -q -F "$LINE" /etc/profile || echo "$LINE" >> /etc/profile
    fi

    # finally... test preload on the ramfs
    export LDR_PRELOAD=$RAMDIR/lib/libscilog.so.1.0.1 
    # test preload overlay
    $DAEMON -t
    if [ $? -gt 0 ]
    then
        unset LDR_PRELOAD
        echo "Warning: overlay issue, preload has been disabled"
        exit 0
    fi
    unset LDR_PRELOAD
}

case "$1" in
    start)
        if [[ -e /var/run/scilog/scilogd.pid ]] 
        then
            if ps -ef | grep $DAEMON | grep -q `cat /var/run/scilog/scilogd.pid`; then
                echo "ScienceLogic agent is already running"
                exit 0
            fi
        fi
        
        #ld_preload_chk
        # load global environment files
        . /etc/environment
        . /etc/profile
        $DAEMON
        RETVAL=$?

        echo "ScienceLogic agent has started"
    ;;
    stop)
        if [[ -e /var/run/scilog/scilogd.pid ]] 
        then
            # SIGUSR1 is signal 30.  This will safely stop the daemon
            kill -USR1 `cat /var/run/scilog/scilogd.pid` 2> /dev/null
        else
            echo "ScienceLogic agent is already stopped"
            exit 0
        fi
        echo "ScienceLogic agent is stopped"
        RETVAL=0
    ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        RETVAL=$?
    ;;
    status)
        ps -afT `cat /var/run/scilog/scilogd.pid`
        RETVAL=$?
    ;;
    *)
        echo "usage: scilogd [start|stop|restart|status]"
        exit 1
    ;;
esac
exit $RETVAL
