#!/bin/sh
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#####################################################
#
#   Generic xCAT post script for diskless nodes
#   The syntax of this script
#      xcatdsklspost {mode} {-m|-M} [postscripts]
#   This script is called in the following different places:
#      updatenode -P ... --> xcatdsklspost 1 -m/-M ...
#      updatenode -S --> xcatdsklspost 2 -m/-M otherpkgs
#      moncfg rmcmon --> xcatdsklspost 3 configrmcnodes
#      node deployment  --> xcatdsklspost
#      statelite mode   --> xcatdsklspost 4
#      update security  --> xcatdsklspost 5 -m/-M ...
#      node reboot - xcatpostinit1  --> xcatdsklspost 6 
#
#####################################################

update_VPD()
{
    if [ -f /usr/sbin/vpdupdate ]; then
        vpdupdate
	logger -t xCAT -p local4.info "xcatdsklspost: updating VPD database"
    fi
}

# Run updatevpd only when necessary
if [ -f /usr/sbin/lsvpd ]; then
    /usr/sbin/lsvpd | grep -i cpu 2>&1 1>/dev/null
    if [ "$?" = "1" ]; then
      update_VPD
    fi
fi

download_postscripts()
{
    server=$1
    if [ -z $server ]; then
	return 1;
    fi

    if [ -f /opt/xcat/xcatinfo ]; then
       INSTALLDIR=`grep 'INSTALLDIR' /opt/xcat/xcatinfo |cut -d= -f2`
    fi
    if [ -z "$INSTALLDIR" ]; then
    INSTALLDIR="/install"
    fi
    max_retries=5
    retry=0
    rc=1 
    while [ 0 -eq 0 ]; do
	
        #wget -N --waitretry=10 --random-wait -T 60 http://$server$INSTALLDIR/postscripts.tgz  -P /xcatpost 2> /tmp/wget.log
        #rc=$?
        
	#if [ $rc -eq 0 ]; then
	#    return 0;
        #else 
            wget -l inf -nH -N -r --waitretry=10 --random-wait -e robots=off -T 60 -nH --cut-dirs=2 --reject "index.html*" --no-parent http://$server$INSTALLDIR/postscripts/ -P /xcatpost 2> /tmp/wget.log
            rc=$?
        
	   if [ $rc -eq 0 ]; then
	       return 0;
           fi
        #fi

	retry=$(($retry+1))
	if [ $retry -eq $max_retries ]; then
            break
	fi

	SLI=$(awk 'BEGIN{srand(); printf("%d\n",rand()*20)}')
        sleep $SLI
    done
    return $rc
}


download_mypostscript()
{
    server=$1
    node=$2
    postfix=$3
    max_retries=$4
    TFTPDIR=$5
    if [ -z $server ]; then
	return 1;
    fi
    if [ -z "$TFTPDIR" ]; then
        TFTPDIR="/tftpboot"
    fi
    #max_retries=2
    retry=0
    rc=1
 

    #node=`hostname`
    while [ 0 -eq 0 ]; do
        if [ $postfix -eq 0 ]; then	
            wget -N --waitretry=10 --random-wait -T 60 http://$server$TFTPDIR/mypostscripts/mypostscript.$node  -P /xcatpost 2>> /tmp/wget.log
            rc=$?
            if [ $rc -eq 0 ]; then
                mv /xcatpost/mypostscript.$node /xcatpost/mypostscript
	        return 0;
            fi

        elif [ $postfix -eq 1 ];then
            wget -N --waitretry=10 --random-wait -T 60 http://$server$TFTPDIR/mypostscripts/mypostscript.$node.tmp  -P /xcatpost 2>> /tmp/wget.log
            rc=$?
            if [ $rc -eq 0 ]; then
                mv /xcatpost/mypostscript.$node.tmp /xcatpost/mypostscript
	        return 0;
            fi
        fi
        
	retry=$(($retry+1))
	if [ $retry -eq $max_retries ]; then
            break
	fi

	#SLI=$(awk 'BEGIN{srand(); printf("%d\n",rand()*5)}')
        #sleep $SLI
    done
    return $rc
}



# pmatch determines if 1st argument string is matched by 2nd argument pattern

pmatch ()
{
  case $1 in
    $2) return 0;;  # zero return code means string matched by pattern
  esac

  return 1          # non-zero return code means string not matched by pattern
}
# parse the arguments
ARGNUM=$#;
if [ -z $1 ]; then
  NODE_DEPLOYMENT=1
else
  NODE_DEPLOYMENT=0
  case $1 in
    1|2|5)
      MODE=$1
      if [ $ARGNUM -gt 1 ]; then
        if [ $2 = "-m" ]; then
          P_SIP=$3
        else
          if [ $2 = "-M" ]; then
            P_SIP=$3
            new_ms=$P_SIP
          fi
        fi
      fi
      if [ $ARGNUM -gt 3 ]; then
        POSTSCRIPTS=$4
      fi
      if [ $ARGNUM -gt 4 ]; then
        if [ $5 = "--tftp" ]; then
          TFTPDIR=$6
        fi 
      fi
      if [ $ARGNUM -gt 6 ]; then
        if [ $7 = "--installdir" ]; then
          INSTALLDIR=$8
        fi 
      fi
      if [ $ARGNUM -gt 8 ]; then
        if [ $9 = "--nfsv4" ]; then
          NFSV4=${10}
        fi 
      fi
      if [ $ARGNUM -gt 10 ]; then
        if [ $11 = "-c" ]; then
          CFLAG=${11}
        fi 
      fi
      ;;
    3|4|6) MODE=$1;;
  esac
fi

# If on AIX node
if [ ! `uname` = Linux ]; then
   #Get a new copy of xcataixpost on the node
   mkdir -p  /xcatpost;
   mkdir -p  /xcatmnt;
   if [ "$NFSV4" = "yes" ]; then
         mount -o vers=4 $P_SIP:$INSTALLDIR/postscripts /xcatmnt
   else
         mount $P_SIP:$INSTALLDIR/postscripts /xcatmnt 
   fi
   cp /xcatmnt/xcataixpost /xcatpost
   umount /xcatmnt
   rmdir /xcatmnt
  logger -t xCAT -p local4.err "Running xcataixpost $*"
  echo "/xcatpost/xcataixpost $1 $2 $3 '"$4"' $5 $6 $7 $8 $9 ${10} ${11}"
  #exec /xcatpost/xcataixpost $1 $2 $3 "'"$4"'" $5 $6 $7 $8 $9 ${10} ${11}
  exec /xcatpost/xcataixpost $1 $2 $3 "$4" $5 $6 $7 $8 $9 ${10} ${11}
  exit
fi

#SLI=$(awk 'BEGIN{srand(); printf("%d\n",rand()*10)}')
#sleep $SLI

if [ ! -d /xcatpost ]; then
    mkdir -p /xcatpost; 
fi

if [ ! -d /tmp/postage ]; then
    mkdir -p /tmp/postage
fi
#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


if [ "$MODE" = "4" ]; then # for statelite mode
    # We have written the xCATSERVER info into the kernel command line!!
    for i in `cat /proc/cmdline`; do
        KEY=`echo $i | awk -F= '{print $1}'`
        if [ "$KEY" =  "XCAT" ]; then
            TMP=`echo $i | awk -F= '{print $2}'`
            XCATSERVER=`echo $TMP | cut -d: -f1`
            echo "XCATSERVER=$XCATSERVER" > /opt/xcat/xcatinfo
            break
        fi
    done

    if [ -f /opt/xcat/xcatinfo ]; then
        SIP=`cut -d= -f2 /opt/xcat/xcatinfo`;
        if [ -n "$SIP" ]; then
            download_postscripts $SIP
            if [ $? -eq 0 ]; then
                downloaded=1
            fi
        fi
    else
        echo "xCAT management server IP can't be determined.";
	echo "exiting...";
        logger -t xCAT -p local4.err "xcatdsklspost:xCAT management server IP can't be determined.\nexiting...";
        exit;
    fi
else # for common mode
    downloaded=0;
    #try the -m if it is specified, -m is passed in the updatenode command
    if [ $downloaded -eq 0 ]; then
        if [ "XX$P_SIP" != "XX" ]; then
            SIP=$P_SIP
    	     download_postscripts $SIP
            if [ $? -eq 0 ]; then 
                downloaded=1
            fi
        fi
    fi 

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

    # Try the host in XCAT= kernel parameter
    if [ $downloaded -eq 0 ]; then
         for i in `cat /proc/cmdline`; do
             KEY=`echo $i | awk -F= '{print $1}'`
             if [ "$KEY" =  "XCAT" ]; then
                 TMP=`echo $i | awk -F= '{print $2}'`
                 SIP=`echo $TMP | cut -d: -f1`
                 if [ -n "$SIP" ]; then
                     download_postscripts $SIP
                     if [ $? -eq 0 ]; then
                         downloaded=1
                         break
                     fi
                 fi
            fi
          done
    fi

    
    #try the dhcp server, this is used for initial boot for the node.
    if [ $downloaded -eq 0 ]; then
        #setup $OSVER ,for SLES11
        if [ -e '/etc/SuSE-release' ]; then
            OSVER=`grep -h VERSION /etc/SuSE-release |awk '{print $3}'`
        fi
        SIPS=`grep -h dhcp-server-identifier /var/lib/dhclient/dhclient*eth*.leases 2> /dev/null|awk '{print $3}'|sed -e 's/;//'`
        if [ -z "$SIPS" ]; then
            SIPS=`grep -h dhcp-server-identifier /var/lib/dhclient/dhclient*hf*.leases 2> /dev/null|awk '{print $3}'|sed -e 's/;//'`
            if [ -z "$SIPS" ]; then
                SIPS=`grep -h DHCPSID /var/lib/dhcpcd/*.info 2> /dev/null|awk -F= '{print $2}'|sed -e s/\'//g`
            fi
        fi
        SIP=`echo $SIPS|awk '{printf $NF}' | tail -n 1` #Pick one for wget
        if [ -n "$SIP" ]; then
            download_postscripts $SIP
            if [ $? -eq 0 ]; then 
                downloaded=1
            fi
        elif [ -x "/sbin/dhcpcd" ]; then
            # New dhcpcd doesn't creates *.info files.
            for lease in $(ls "/var/lib/dhcpcd/"); do
                iface="$(echo "$lease" | sed -n -e 's/^dhcpcd-\(.*\)\.lease$/\1/p')"
                if [ -n "$iface" ]; then
                    SIP="$(dhcpcd -q -T "$iface" | sed -n -e '/new_dhcp_server_identifier/ s/.*=//p')"
                    if [ -n "$SIP" ]; then
                        download_postscripts $SIP
                        if [ $? -eq 0 ]; then
                            downloaded=1
                            break
                        fi
                    fi
                fi
            done
        fi
    fi
    
    #no hope, now let's get out of here.
    if [ $downloaded -eq 0 ]; then
        hn=`hostname`
        echo "Cannot download the postscripts from the xCAT server for node $hn"
        logger -t xCAT -p local4.err "xcatdsklspost:Cannot download the postscripts from the xCAT server for node $hn"
        exit  
    fi

fi # finish the postscripts download


rm -rf /xcatpost/mypostscript

#get node name and download the mypostscript.$node file
#try to get the node ip address that connects to the server.
#then resolve the name of the ip
real_SIP=`getent hosts $SIP |awk {'print $1'}` 
if [ $? -ne 0 ]; then 
    real_SIP=$SIP
fi

NIP=`ip route get $real_SIP | head -n 1 | sed 's/^.*src//g' | awk {'print $1'}`
if [ $? -eq 0 ] && [ -n "$NIP" ]; then
    #relsove the name of the node from ip address
    result=`getent hosts $NIP`
    if [ $? -eq 0 ]; then
	node1=`echo $result | awk {'print $2'}`
	node2=`echo $result | awk {'print $3'}`
	if [ ${#node1} -gt ${#node2} ]; then 
	    node=$node1 
	    node_short=$node2
	else
	    node=$node2 
	    node_short=$node1
	fi	
	if [ -z "$node_short" ]; then
	    node_short=`echo $node |awk -F. {'print $1'}`
	fi
    else 
	if [ -z "$node" ]; then
	    node=`hostname`
	    node_short=`hostname -s`
	fi
    fi
else
    node=`hostname`
    node_short=`hostname -s`
fi
    
max_retries=2
postfix=0
if [ -n "$node_short" ]; then 
    download_mypostscript $SIP $node_short $postfix $max_retries $TFTPDIR
    #disable trying the long node name for now 
    #if [ $? -ne 0 ]; then
    #	if [ "$node" != "$node_short" ]; then
    #	    download_mypostscript $SIP $node $postfix $max_retries $TFTPDIR
    #	fi
    #fi
fi



if grep 'rw /rw tmpfs ' /proc/mounts  >/dev/null 2>&1; 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 [ -f /xcatpost/postscripts.tgz ]; then
#    tar xzf /xcatpost/postscripts.tgz -C /xcatpost/ 2>/dev/null 
#fi

chmod +x /xcatpost/*; 

cd /xcatpost; 
PATH=/xcatpost:$PATH
export PATH

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

# if the mypostscript isn't got by download_mypostscript, we assume that it wasn't generated.
# So generate it firstly.
if [ ! -x /xcatpost/mypostscript ]; then

#/xcatpost/getpostscript.awk | egrep  '<data>' | sed  -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' >  /xcatpost/mypostscript; 
/xcatpost/getpostscript.awk version2 > /dev/null 
max_retries=1
postfix=1
if [ -n "$node_short" ]; then
    download_mypostscript $SIP $node_short $postfix $max_retries $TFTPDIR 
    if [ $? -ne 0 ]; then
	if [ "$node" != "node_short" ]; then
	    download_mypostscript $SIP $node $postfix $max_retries $TFTPDIR
	fi
    fi
fi

MYCONT=`grep MASTER /xcatpost/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
    	 echo "xcatdsklspost: failed to getpostscript from $XCATSERVER"
	 logger -t xcat -p local4.err "xcatdsklspost: failed to getpostscript from $XCATSERVER"
         exit 1
    fi

    #SLI=$(awk 'BEGIN{srand(); printf("%d\n",rand()*10)}')
    #SLI=$((10 + $SLI))
    #sleep $SLI
    
    #/xcatpost/getpostscript.awk | egrep  '<data>'  | sed  -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' >  /xcatpost/mypostscript;
    /xcatpost/getpostscript.awk version2 > /dev/null
    max_retries=1
    postfix=1
    download_mypostscript $SIP $node $postfix $max_retries $TFTPDIR 
    if [ $? -ne 0 ]; then
	if [ "$node" != "$node_short" ]; then
	    download_mypostscript $SIP $node $postfix $max_retries $TFTPDIR
	fi
    fi 
    MYCONT=`grep MASTER /xcatpost/mypostscript`
    if [ ! -z "$MYCONT" ]; then
        break;
    fi
done

fi

#save the MASTER into the xcatinfo file for node deployment case, 
#for updatenode case, only save it when -M is specified
if [ $NODE_DEPLOYMENT -eq 1 ] || [ "$MODE" = "4" ]; then
    new_ms=`grep '^MASTER' /xcatpost/mypostscript |cut -d= -f2`
fi
if [ -n "$new_ms" ]; 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=$new_ms/" /opt/xcat/xcatinfo
    else
	echo "XCATSERVER=$new_ms" >> /opt/xcat/xcatinfo
    fi
fi


# when called by the updatenode command
#modify the UPDATENODE flag to 1 
if [ "$MODE" = "1" ] || [ "$MODE" = "2" ]; then
  TMP=`sed -e 's/UPDATENODE=0/UPDATENODE=1/g' /xcatpost/mypostscript`;
  echo "$TMP" > /xcatpost/mypostscript;
fi
if [ "$MODE" = "5" ]; then
  TMP=`sed -e 's/UPDATENODE=0/UPDATENODE=1\nUPDATESECURITY=1\nexport UPDATESECURITY/g' /xcatpost/mypostscript`;
  echo "$TMP" > /xcatpost/mypostscript;
fi

#when a diskfull reboot mode 
if [ "$MODE" = "6" ]; then
  # remove the post scripts so that they are not run on reboot
  TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ d" /xcatpost/mypostscript`
  echo "$TMP" > /xcatpost/mypostscript
  # get the RUNBOOTSCRIPTS site variable
  if [ -f /xcatpost/mypostscript ]; then
    RUNBOOTSCRIPTS=`grep 'RUNBOOTSCRIPTS=' /xcatpost/mypostscript |cut -d= -f2`
  fi

  # if admin did not requested running of post boot scripts - then remove PBS
  if [ "$RUNBOOTSCRIPTS" != "'yes'" ]; then
  #remove all the postscripts
  TMP=`sed "/# postbootscripts-start-here/,/# postbootscripts-end-here/ d" /xcatpost/mypostscript`
  echo "$TMP" > /xcatpost/mypostscript
 
  #TMP=`sed "/postbootscripts-start-here/,/postbootscripts-end-here/ d" /xcatpost/mypostscript.post`
  #echo "$TMP" > /xcatpost/mypostscript.post
  fi
fi

# postscript name is specified with the updatenode
#cp /xcatpost/mypostscript /xcatpost/mypostscript.backup
# if the list has a postscript named *start-here* then we must rebuild the
# mypostscript file with only the matching *start-here stanza. 


if ( pmatch $POSTSCRIPTS  "*start-here*" ); then
  if ( pmatch $POSTSCRIPTS  "*osimage-postbootscripts-start-here" ); then
  # remove all sections but the osimage-postbootscripts section
    TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# defaults-postbootscripts-start-here/,/# defaults-postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# node-postbootscripts-start-here/,/# node-postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
  fi
  if ( pmatch $POSTSCRIPTS  "*postscripts-start-here" ); then
    #remove all the postbootscripts 
    TMP=`sed "/# postbootscripts-start-here/,/# postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
  fi
  if ( pmatch $POSTSCRIPTS  "*postbootscripts-start-here" ); then
    #remove all the postscripts 
    TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    
  fi
  if ( pmatch $POSTSCRIPTS  "*defaults-postscripts-start-here" ); then
  # remove all sections but the defaults-postscripts section
    TMP=`sed "/# osimage-postscripts-start-here/,/# osimage-postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# node-postscripts-start-here/,/# node-postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# postbootscripts-start-here/,/# postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
  fi
  
  if ( pmatch $POSTSCRIPTS  "*node-postscripts-start-here" ); then
  # remove all sections but the node-postscripts section
    TMP=`sed "/# osimage-postscripts-start-here/,/# osimage-postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# defaults-postscripts-start-here/,/# defaults-postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# postbootscripts-start-here/,/# postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
  fi

  if ( pmatch $POSTSCRIPTS  "*defaults-postbootscripts-start-here" ); then
  # remove all sections but the defaults-postbootscripts section
    TMP=`sed "/# osimage-postbootscripts-start-here/,/# osimage-postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# node-postbootscripts-start-here/,/# node-postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
  fi
  
  
  if ( pmatch $POSTSCRIPTS  "*node-postbootscripts-start-here" ); then
  # remove all sections but the node-postbootscripts section
    TMP=`sed "/# osimage-postbootscripts-start-here/,/# osimage-postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# defaults-postbootscripts-start-here/,/# defaults-postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
  fi
   
   
# check to see if input postscript list is not empty. If there is a list
# remove the built postscripts and only add the ones for the list.
else
   if [ "XX$POSTSCRIPTS" != "XX" ]; then
    #remove all the postbootscripts, and replace with list provided 
    TMP=`sed "/# postbootscripts-start-here/,/# postbootscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    #remove all the postscripts
    TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ d" /xcatpost/mypostscript`
    echo "$TMP" > /xcatpost/mypostscript
    echo "# postscripts-start-here" >> /xcatpost/mypostscript
    #add requested postscripts in
    echo "$POSTSCRIPTS" | tr "," "\n" >> /xcatpost/mypostscript
    echo "# postscripts-end-here" >> /xcatpost/mypostscript
  
  fi
fi

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

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

# use the run_ps subroutine to run the postscripts
TMP=`sed "/# postscripts-start-here/,/# postscripts-end-here/ s/\(.*\)/run_ps \1/;s/run_ps\s*#/#/;s/run_ps\s*$//" /xcatpost/mypostscript`
echo "
# global value to store the running status of the postbootscripts,the value is non-zero if one postbootscript failed
return_value=0
# subroutine used to run postscripts
run_ps () {
 local ret_local=0
 logdir=\"/var/log/xcat\"
 mkdir -p \$logdir
 logfile=\"/var/log/xcat/xcat.log\"
 
 if [ -f \$1 ]; then 
  echo \"\`date\` Running postscript: \$@\" | tee -a \$logfile
  #./\$@ 2>&1 1> /tmp/tmp4xcatlog
  #cat /tmp/tmp4xcatlog | tee -a \$logfile
  ./\$@ 2>&1 | tee -a \$logfile
  ret_local=\${PIPESTATUS[0]}
  if [ \"\$ret_local\" -ne \"0\" ]; then
     return_value=\$ret_local
  fi
  echo \"Postscript: \$@ exited with code \$ret_local\"
 else
  echo \"\`date\` Postscript \$1 does NOT exist.\" | tee -a \$logfile
  return_value=-1
 fi

 return 0
}
# subroutine end

" > /xcatpost/mypostscript
echo "$TMP" >> /xcatpost/mypostscript
TMP=`sed "/# postbootscripts-start-here/,/# postbootscripts-end-here/ s/\(.*\)/run_ps \1/;s/run_ps\s*#/#/;s/run_ps\s*$//" /xcatpost/mypostscript`
echo "$TMP" > /xcatpost/mypostscript 

if [ $NODE_DEPLOYMENT -eq 1 ] || [ "$MODE" = "4" ]; then
  #notify the server that we are done with netbooting
  CNS=`grep NODESTATUS= /xcatpost/mypostscript |awk -F = '{print $2}'`
  if [ -z "$CNS" ] || [ "$CNS" != "'0'" -a   "$CNS" != "'N'"  -a  "$CNS" != "'n'" ]; then
# TMP=`sed "/postscripts-start-here/ i\updateflag.awk \\$MASTER 3002 \\"installstatus configuring\\"" /xcatpost/mypostscript`
#       echo "$TMP"> /xcatpost/mypostscript
        echo "
if [ \"\$return_value\" -eq \"0\" ]; then
   updateflag.awk \$MASTER 3002 \"installstatus booted\"
else
   updateflag.awk \$MASTER 3002 \"installstatus failed\"
fi
        " >> /xcatpost/mypostscript
  fi
fi

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

CLEANUPXCATPOST=`grep CLEANUPXCATPOST= /xcatpost/mypostscript |awk -F = '{print $2}'` 
if [ "$CLEANUPXCATPOST" = "'1'" ] || [ "$CLEANUPXCATPOST" = "'yes'" ]; then
  echo "cd /" >> /xcatpost/mypostscript
  # /xcatpost might be read-only for statelite nodes
  echo "rm -rf /xcatpost/*" >> /xcatpost/mypostscript
fi



if [ "$MODE" = "1" ] || [ "$MODE" = "2" ] || [ "$MODE" = "5" ]; then
  echo "exit \$return_value" >> /xcatpost/mypostscript
fi

chmod +x /xcatpost/mypostscript
if [ -x /xcatpost/mypostscript ];then
   /xcatpost/mypostscript
   VRET_POST=$?   
fi
#rm -f /xcatpost/mypostscript

#tell user it is done when this is called by updatenode command
if [ "$MODE" = "1" ] || [ "$MODE" = "2" ] || [ "$MODE" = "5" ]; then
  echo "returned from postscript"
fi

exit $VRET_POST
