#!/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
    echo "Nothng to do for stateless and statelite node."
    exit 0
fi

#create /opt/xcat directory if not exist
if [ ! -d "/opt/xcat" ]; then
    mkdir -p /opt/xcat
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
infofile="/opt/xcat/xcatinfo"
if [ ! -f $infofile ]; then
    echo "XCATSERVER=$MASTER" > $infofile
    echo "REBOOT=TRUE" >> $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
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

