#!/bin/sh
# IBM(c) 2012 EPL license http://www.eclipse.org/legal/epl-v10.html

#-------------------------------------------------------------------------------
#=head1  setuppostbootscripts 
#=head2  This command setup the node so that when the node reboots,
#        the postbootscripts will be run or not depending on the   
#        site.runbootscripts setting. 
#        If site.runbootscripts is 'yes', then the scripts defined by
#        postscripts.postbootscripts will be run when the node reboots. 
#=cut
#-------------------------------------------------------------------------------

#only works for diskful nodes
if [ "$NODESETSTATE" = "netboot" -o \
     "$NODESETSTATE" = "statelite" -o \
     "$NODESETSTATE" = "diskless" -o \
     "$NODESETSTATE" = "dataless" ]; then
    logger -t xCAT -p local4.info "setuppostbootscripts: Nothing to do for stateless and statelite nodes."
    exit 0
fi

#create /opt/xcat directory if not exist
if [ ! -d "/opt/xcat" ]; then
    mkdir -p /opt/xcat
fi
infofile="/opt/xcat/xcatinfo"
if [ "$RUNBOOTSCRIPTS" = "yes" ] || [ "$RUNBOOTSCRIPTS" = "YES" ]; then
       RUNBOOTSCRIPTS=YES
else
       RUNBOOTSCRIPTS=NO
fi 
#  check to see if current setting is already in the file, if so nothing to do
if [ -f $infofile ]; then
    value=`grep "RUNBOOTSCRIPTS=$RUNBOOTSCRIPTS" $infofile`
    if [[ -n $value ]]; then  # match
      logger -t xCAT -p local4.info "setuppostbootscripts: xcatinfo uptodate, nothing to do."
      exit 0
    fi
fi
#copy the necessary files
rsync /xcatpost/xcatdsklspost /opt/xcat/xcatdsklspost
rsync /xcatpost/xcatinstallpost /opt/xcat/xcatinstallpost
rsync /xcatpost/xcatpostinit1 /etc/init.d/xcatpostinit1
chmod 755 /etc/init.d/xcatpostinit1

if [ ! -f "/etc/rc.d/rc3.d/S84xcatpostinit1" ]; then 
    ln -s /etc/init.d/xcatpostinit1 /etc/rc.d/rc3.d/S84xcatpostinit1
fi
if [ ! -f "/etc/rc.d/rc4.d/S84xcatpostinit1" ]; then
    ln -s /etc/init.d/xcatpostinit1 /etc/rc.d/rc4.d/S84xcatpostinit1
fi
if [ ! -f "/etc/rc.d/rc5.d/S84xcatpostinit1" ]; then
    ln -s /etc/init.d/xcatpostinit1 /etc/rc.d/rc5.d/S84xcatpostinit1
fi

#put correct info in /opt/xcat/xcatinfo

if [ ! -f $infofile ]; then
    echo "XCATSERVER=$MASTER" > $infofile
    echo "REBOOT=TRUE" >> $infofile
    echo "RUNBOOTSCRIPTS=$RUNBOOTSCRIPTS" >> $infofile
else
    value=`grep XCATSERVER $infofile`
    if [[ -n $value ]]; then
        sed -i "s/^XCATSERVER=.*$/XCATSERVER=$MASTER/" $infofile
    else 
        echo "XCATSERVER=$MASTER" >> $infofile
    fi
    value=`grep REBOOT $infofile`
    if [[ -n $value ]]; then
        sed -i "s/^REBOOT=.*$/REBOOT=TRUE/" $infofile
    else 
        echo REBOOT=TRUE >> $infofile
    fi
    value=`grep RUNBOOTSCRIPTS $infofile`
    if [[ -n $value ]]; then
        sed -i "s/^RUNBOOTSCRIPTS=.*$/RUNBOOTSCRIPTS=$RUNBOOTSCRIPTS/" $infofile
    else 
        echo "RUNBOOTSCRIPTS=$RUNBOOTSCRIPTS" >> $infofile
    fi
fi
#enable/disable the running of postscripts according to site.runbootscripts
if [[ "$RUNBOOTSCRIPTS" = "yes" ]]; then
    output=`chkconfig xcatpostinit1 on 2>&1  > /dev/null`
else
    output=`chkconfig xcatpostinit1 off 2>&1  > /dev/null`
fi

exit 0