#!/bin/sh
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#####################################################
#
#   Generic xCAT post script for diskless nodes
#   This script is called in the following different palces:
#      updatenode -P ... --> xcatdsklspost 1 ...
#      updatenode -S --> xcatdsklspost 2 otherpkgs
#      moncfg rmcmon --> xcatdsklspost 3 configrmcnodes
#      node deployment  --> xcatdsklspost
#
#####################################################
if [ ! `uname` == Linux ]; then
   MYDIR=`dirname $0`
   exec $MYDIR/xcatdsklspost.aix
   exit
fi
let SLI=$RANDOM%10
sleep $SLI

#setup $OSVER ,for SLES11
if [ -e '/etc/SuSE-release' ]; then
   OSVER=`grep -h VERSION /etc/SuSE-release |awk '{print $3}'`
fi

SIP=`grep -h dhcp-server-identifier /var/lib/dhclient/dhclient*.leases 2> /dev/null|tail -n 1|awk '{print $3}'|sed -e 's/;//'`
if [ -z "$SIP" ]; then
    SIP=`grep -h DHCPSID /var/lib/dhcpcd/*.info 2> /dev/null|awk -F= '{print $2}'|tail -n 1`
fi

#echo "SIP=$SIP"
if [ "$OSVER" = "11" ]; then
    SIP=`echo $SIP |awk -F\' '{print $2}'` #trim '   
fi

#open the xcatinfo file to look for the master if it is not set
frominfofile=0
if [ -z $SIP ]; then
    if [ -f /opt/xcat/xcatinfo ]; then
	SIP=`grep 'XCATSERVER' /opt/xcat/xcatinfo |cut -d= -f2`
	frominfofile=1
    fi   
fi

#no hope, now let's get out here.
if [ -z $SIP ]; then
    hn=`hostname`
    echo "Cannot find the xCAT server for node $hn"
    exit  
fi

#save the SIP into the xcatinfo file
if [ $frominfofile -eq 0 ]; then
    if [ ! -f /opt/xcat/xcatinfo ]; then
	mkdir -p /opt/xcat
        touch /opt/xcat/xcatinfo
    fi
    grep 'XCATSERVER' /opt/xcat/xcatinfo 2>&1 > /dev/null
    if [ $? -eq 0 ]; then
	sed -i "s/XCATSERVER=.*/XCATSERVER=$SIP/" /opt/xcat/xcatinfo
    else
	echo "XCATSERVER=$SIP" >> /opt/xcat/xcatinfo
    fi
fi    

if grep 'rw /rw tmpfs ' /proc/mounts  >& /dev/null; then
    touch /var/lock/subsys/xcatmounts
    echo '#!/bin/bash' > /etc/rc6.d/K10xcatmounts
    echo umount -l /ro >> /etc/rc6.d/K10xcatmounts
    echo umount -l /rw >> /etc/rc6.d/K10xcatmounts
    chmod 755 /etc/rc6.d/K10xcatmounts
    ln -sf /etc/rc6.d/K10xcatmounts /etc/rc0.d/K10xcatmounts
fi


if [ -x /usr/bin/openssl ]; then
     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
mkdir -p /xcatpost; 
mkdir -p /tmp/postage
rm -R -f /xcatpost/*
rm -R -f /tmp/postage/*

#here we get all the postscripts.  Please do not change this behaviour because some scripts depend on others
cd /tmp/postage
#wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 0 -T 60 ftp://$SIP/install/postscripts 2> /tmp/wget.log
wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 0 -T 60 ftp://$SIP/postscripts 2> /tmp/wget.log
#mv $SIP/install/postscripts/* /xcatpost; 
mv $SIP/postscripts/* /xcatpost; 
rm -rf $SIP
cd /xcatpost; 
PATH=/xcatpost:$PATH
export PATH
chmod +x /xcatpost/*;
#echo "PATH=$PATH" 
/xcatpost/getpostscript.awk | sed  -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' >  /tmp/mypostscript; 
MYCONT=`grep MASTER /tmp/mypostscript`
#echo "MYCONT=$MYCONT"
#if getpostscript.awk fails, the postscript will fall into infinit loop
#so one retry_number is added to avoid sunc a condition
MAX_RETRIES=10
RETRY=0
while [ -z "$MYCONT" ]; do
    RETRY=$(($RETRY+1))
    if [ $RETRY -eq $MAX_RETRIES ]
    then
        break
    fi

    let SLI=$RANDOM%10
    let SLI=10+$SLI
    sleep $SLI
    /xcatpost/getpostscript.awk | sed  -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' >  /tmp/mypostscript; 
    MYCONT=`grep MASTER /tmp/mypostscript`
#    echo "MYCONT=$MYCONT"
done

# when called by the updatenode command
#modify the UPDATENODE flag to 1 
if [ $# -gt 0 ]; then
  TMP=`sed -e 's/UPDATENODE=0/UPDATENODE=1/g' /tmp/mypostscript`;
  echo "$TMP" > /tmp/mypostscript;
fi

if [ $# -gt 1 ]; then
  POSTS=$2
  #remove all the postscripts
  TMP=`sed "/postscripts-start-here/,/postscripts-end-here/ d" /tmp/mypostscript`
  echo "$TMP" > /tmp/mypostscript
  #add requested postscripts in
  echo "$POSTS" | tr "," "\n" >> /tmp/mypostscript
fi

#ADDSITEYUM is set by post.rh and post.rh.iscsi for full installtion
if [[ "$ADDSITEYUM" = "1" ]]; then
  TMP=`sed "/postscripts-start-here/ a addsiteyum" /tmp/mypostscript`
  echo "$TMP" > /tmp/mypostscript
fi

#MYCONT=`cat /tmp/mypostscript`
#echo "$MYCONT"

if [ $# -eq 0 ]; then
  #notify the server that we are done with netbooting
  CNS=`grep NODESTATUS= /tmp/mypostscript |awk -F = '{print $2}'`
  if [ -z "$CNS" ] || [ "$CNS" != "'0'" -a   "$CNS" != "'N'"  -a  "$CNS" != "'n'" ]; then
      echo "updateflag.awk \$MASTER 3002 \"installstatus booted\"" >> /tmp/mypostscript
  fi
fi

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

chmod +x /tmp/mypostscript
if [ -x /tmp/mypostscript ];then
   /tmp/mypostscript
fi
#rm -f /tmp/mypostscript

#tell user it is done when this is called by updatenode command
if [ $# -gt 0 ]; then
  echo "returned from postscript"
fi