#!/bin/sh
# Kiran Kamity, 05/11/2004
# Sreen Tallam, 04/04/2008
# Sreen Tallam, 08/07/2008
# Any Netapp additions to the initial sequence of events that must run on
# the RLM go in this file
# Example: Adding user 'admin', etc...

case "$1" in
    start|"")
        #Change Serial port baudrate
        stty -F /dev/ttyS1 115200

        # create NetApp proc fs entries
        insmod /usr/local/module/procfsfile.ko

        # Change permisions of these directories
        chmod 755 /usr/bin
        chmod 755 /usr/lib
        chmod 755 /usr/sbin

        chmod +t /tmp
	chmod 777 /mnt/logs
        chmod 755 /etc/
        chmod 755 /bin/
        chmod 755 /sbin/
        chmod 755 /var/
        chmod 755 /var/log/messages
        chmod 777 /mnt/logs/fpga_events
        chmod 777 /mnt/logs/fpga_events.1
        chmod 4111 /usr/bin/sudo
        chmod 0440 /etc/sudoers
        chmod 4755 /usr/bin/ssh

        # Choosing correct watcher config file
        if [ -f "/var/run/RazorS" ] || [ -f "/var/run/RazorM" ]  ; then
            cp /etc/watcher_sp_M.conf /etc/watcher.conf
        else
            cp /etc/watcher_sp_L.conf /etc/watcher.conf
        fi

        # Create these two files for logging users
        touch /var/run/utmp
        touch /var/log/wtmp

        # Create this file to keep notification sent to OnTap
        touch /var/run/notification
        # Create file for saving expander version
        touch /tmp/sas_version; chmod 666 /tmp/sas_version

        # Extract CLI help files from hlp.tgz.
        /bin/tar zxf /usr/local/hlp/hlp.tgz -C /usr/local/hlp
        if [ $? -ne 0 ]; then
            echo "Error extracting help files for CLI" >> /var/log/messages
        fi
        # Starting the seld
        /usr/local/bin/seld

        # Starting the pktd_reader and pktd_writer
        /usr/local/bin/pktd_reader
        /usr/local/bin/pktd_writer

        # Starting the selpktd
        /usr/local/bin/selpktd

        # Start all of the ACP daemons
        if [ -f "/var/run/RazorS" ] || [ -f "/var/run/RazorM" ]  ; then
            /usr/local/acp/etc/init.d/rcS
        fi

        if [ -f "/var/run/RazorS" ] || [ -f "/var/run/RazorM" ]  ; then
            # Starting the isvcd
            /usr/local/bin/isvcd
        fi

        # Starting the console logger
        /usr/local/bin/loggerd

        touch /var/run/ncsi/ports/0/operstate
        chmod 666 /var/run/ncsi/ports/0/operstate
        touch /var/run/ncsi/ports/1/operstate
        chmod 666 /var/run/ncsi/ports/1/operstate

        # Starting the eventfifod
        /usr/local/bin/eventfifod

        # Starting the orftp daemons
        /usr/local/bin/orftp_sender
        /usr/local/bin/orftp_rcvr
        /usr/local/bin/fud_sp

        # Starting the trap daemon
        /usr/local/bin/trapd

        /usr/local/bin/configd

        # Starting the loopd
        /usr/local/bin/loopd

        # Starting the downbeat
        /usr/local/bin/f2rbeat

        # Starting the upbeat
        /usr/local/bin/r2fbeat

        # Starting the eventd
        /usr/local/bin/eventd

        # Starting the linkd
        /usr/local/bin/linkd

        # Starting the asupd
        /usr/local/bin/asupd

        # Starting passwd_auth
        /usr/local/bin/passwd_auth

        # Starting notification daemon
        /usr/local/bin/notifd

        # Starting resource monitor daemon
        /usr/local/bin/resource_monitor
        # Starting Switch API daemon
        /usr/local/bin/swapi_server

        # Starting WrenchPort link status monitor daemon
        /usr/local/bin/wrench_port_mon

        # Starting Forensics API daemon
        /usr/local/bin/ontap_forensics

        # Starting splogd daemon
        /usr/local/bin/splogd

        # Starting Service Processor Command Server
        /usr/local/bin/spcs

#REVISIT
        # Try to fix the battery FC-MTO flag
    #   if [ -f "/var/run/RazorL" ]  || [ -f "/var/run/RazorS" ] || [ -f "/var/run/RazorM" ]  ; then
    #       /usr/local/bin/batTool fixFCMTO
    #   fi

        # Initiating Auto update of battery firmware for Carnegie
    #   if [ -f "/var/run/Carnegie" ] ; then
    #       /etc/init.d/bat_update_bg.sh&
    #   fi

       (/usr/local/bin/bat_wakeup.sh 2>&1 | /usr/bin/logger) &

        #Make sure we have an initialized battery EEPROM
        #and run an initial learning cycle if necessary
        #REVISIT the SLEEP 600, using this to protect us
        #from conflicts with the bat_wakeup.sh being backgrounded
        (sleep 120; /usr/local/bin/batTool learning_cycle 2>&1 | /usr/bin/logger) &

        if [ ! -f "/mnt/logs/etc/auth_keys" ] ; then
            touch /mnt/logs/etc/auth_keys
            chmod 644 /mnt/logs/etc/auth_keys
        fi

        # Start the environmental manager
        /usr/local/bin/env_mgr &

	# Start Switch FW update daemon
	/usr/local/bin/switch_fud

        touch /var/run/sp_up
        /usr/local/bin/log_start_time.sh "SP_Ready_at "
        # Notify Ontap that the SP is up
        /usr/local/bin/run_cmd_when_ipmi_ready.sh "/usr/local/bin/notify 4" 30 &
        /usr/local/bin/log_fpgaevent.sh "SP_Ready"

        current_boot_image=`/usr/local/bin/fw_printenv | grep image= | sed 's/image=//g'`;
        echo "Booting from" $current_boot_image
        version=$(/usr/local/bin/version.sh -e -r)
        echo "Current version: " $version
        sel_entry="Running $current_boot_image version $version"
        sel_entry=${sel_entry:0:45}
        /usr/local/bin/sel_logger "SP.notice" "$sel_entry"
        ;;
    stop)
        ;;
    *)
        echo "Usage: local {start|stop}" >&2
        exit 1
        ;;
esac
