#!/bin/bash
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#################################################################
#
#   xCAT script for running postboot scripts for full install case.
#################################################################

. /xcatpost/xcatlib.sh
if [ -f /xcatpost/mypostscript.post ]; then
    XCATDEBUGMODE=`grep 'XCATDEBUGMODE=' /xcatpost/mypostscript.post |cut -d= -f2|sed s/\'//g`
    MASTER_IP=`grep '^MASTER_IP=' /xcatpost/mypostscript.post |cut -d= -f2|sed s/\'//g`
fi


if [ ! `uname` = Linux ]; then
   msgutil_r "$MASTER_IP" "err" "$0: the OS name is not Linux" "/var/log/xcat/xcat.log"
   exit
fi
SLI=$(awk 'BEGIN{srand(); printf("%d\n",rand()*10)}')
sleep $SLI

MACADDR=`grep MACADDRESS= /xcatpost/mypostscript.post |awk -F = '{print $2}'|sed s/\'//g`
INSTALLNIC=`ip -o link|grep -i $MACADDR|awk  '{print $2}'|sed s/://`

#the nics have not been configured when running the PBS sometimes, need to make sure... 
RETRY=0
until ip -4 --oneline addr show dev $INSTALLNIC|grep inet >/dev/null
do
    sleep 2
    RETRY=$[ $RETRY + 1 ]
    if [ $RETRY -eq 90  ];then
          msgutil_r "$MASTER_IP" "err" `date`" xcatinstallpost: Network not configured, please check..." "/var/log/xcat/xcat.log"
          exit 1
    fi

done



cd /xcatpost; 
PATH=/xcatpost:$PATH
export PATH
# To support the postscripts in the subdirectories under /install/postscripts
#chmod +x /xcatpost/*;
chmod -R +x `find /xcatpost/ -maxdepth 1 -print | grep -E -v '^(/xcatpost/|/xcatpost/_xcat|/xcatpost/_ssh|/xcatpost/ca|/xcatpost/hostkeys)$'`

POST_IN_DIR="/xcatpost"

if [ -x /usr/bin/openssl ]; then
     SIP=`grep "^MASTER=" ${POST_IN_DIR}/mypostscript.post  |cut -d= -f2`
     XCATSERVER="$SIP:3001"
     export XCATSERVER
     USEOPENSSLFORXCAT=1 #Though this is the only method going forward, flag to allow backward compatibility with 2.2 generated netboot images
     export USEOPENSSLFORXCAT
fi

#  Regardless of where mypostscript.post was found, modify and run it in /xcatpost.

DHCP_TMP=`sed 's/\(DHCPINTERFACES=\)\(.*\)$/\1"\2"/' /xcatpost/mypostscript.post`
echo "$DHCP_TMP" > /xcatpost/mypostscript.post

#mark that the first reboot after installation is finished
echo "REBOOT=TRUE" >> /opt/xcat/xcatinfo

CNS=`grep NODESTATUS= /xcatpost/mypostscript.post |awk -F = '{print $2}'`
if [ -z "$CNS" ] || [ "$CNS" != "'0'" -a   "$CNS" != "'N'"  -a  "$CNS" != "'n'" ]; then
#update the node status during the postbootscript running
#TMP=`sed "/postbootscripts-start-here/ i\updateflag.awk \\$MASTER 3002 \\"installstatus configuring\\"" /xcatpost/mypostscript.post`
#echo "$TMP"> /xcatpost/mypostscript.post

echo "

if [ \"\$return_value\" -eq \"0\" ]; then
   if [ \"\$XCATDEBUGMODE\" > \"0\" ]; then
      msgutil_r \"\$MASTER_IP\" \"info\" \"node booted, reporting status...\" \"/var/log/xcat/xcat.log\"
   fi
   updateflag.awk \$MASTER 3002 \"installstatus booted\"
else
   if [ \"\$XCATDEBUGMODE\" > \"0\" ]; then
      msgutil_r \"\$MASTER_IP\" \"info\" \"node boot failed, reporting status...\" \"/var/log/xcat/xcat.log\"
   fi
   updateflag.awk \$MASTER 3002 \"installstatus failed\"
fi
" >> /xcatpost/mypostscript.post
fi

stopservice NetworkManager
stopservice NetworkManager-dispatcher
stopservice NetworkManager-wait-online

disableservice NetworkManager
disableservice NetworkManager-dispatcher 
disableservice NetworkManager-wait-online
chmod +x /xcatpost/mypostscript.post
if [ -x /xcatpost/mypostscript.post ];then
   msgutil_r "$MASTER_IP" "info" "running /xcatpost/mypostscript.post" "/var/log/xcat/xcat.log"
   /xcatpost/mypostscript.post
   msgutil_r "$MASTER_IP" "info" "/xcatpost/mypostscript.post return" "/var/log/xcat/xcat.log"
fi
