#!/bin/bash
#
# Setup hostname
#
echo "post scripts" >/root/post.log
export PRINIC=#TABLE:noderes:THISNODE:primarynic#
if [ "$PRINIC" == "mac" ]
then
    export PRINIC='#GETPRINICMAC:THISNODE#'
fi
if [ -z "$PRINIC" ]
then
    export PRINIC=eth0
elif [[ `echo "$PRINIC" | grep -sqE ^[A-Fa-f0-9]+:[A-Fa-f0-9]+:[A-Fa-f0-9]+:[A-Fa-f0-9]+:[A-Fa-f0-9]+:[A-Fa-f0-9]+$ ;echo $?` == "0" ]]; then
    export PRINIC=`ifconfig -a | grep -i "HWaddr $PRINIC" | awk '{print $1}'`
fi
IP=$(ifconfig $PRINIC | grep inet | awk '{print $2}' | awk -F: '{print $2}')
if [ -z $IP ]
then
	dhclient $PRINIC
	IP=$(ifconfig $PRINIC | grep inet | awk '{print $2}' | awk -F: '{print $2}')
fi
echo "search #TABLE:site:key=domain:value#" >/etc/resolv.conf
for i in $(echo #TABLE:site:key=nameservers:value# | tr ',' ' ')
do
	echo "nameserver $i"
done >>/etc/resolv.conf
#change the soft link /bin/sh to /bin/bash
ln -sf /bin/bash /bin/sh
#
# Run xCAT post install
#
export MASTER_IP="#XCATVAR:XCATMASTER#"
export MASTER_IPS="#XCATVAR:XCATMASTER#"
export MASTER="#XCATVAR:XCATMASTER#"
export INSTALLDIR=#TABLE:site:key=installdir:value#
export HTTPPORT=#TABLEBLANKOKAY:site:key=httpport:value#
if [ -z "$INSTALLDIR" ]; then
    INSTALLDIR="/install"
fi
if [ -z "$HTTPPORT" ]; then
    HTTPPORT="80"
fi
cd /tmp
RAND=$(perl -e 'print int(rand(50)). "\n"')
sleep $RAND
for j in $(seq 1 20)
do
	GOTIT=0
	for i in $MASTER_IPS
	do
        wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused  -e robots=off -nH --cut-dirs=2 --reject "index.html*" --no-parent -t 0 -T 60 http://${i}:${HTTPPORT}${INSTALLDIR}/postscripts/ -P /xcatpost
		#wget --wait=10 --random-wait --waitretry=10 --retry-connrefused -t 0 -T 60 http://$i/install/autoinst/xcatpost.tar.gz
		if [ "$?" = "0" ]
		then
         if [ ! -x /usr/bin/openssl ]; then #Stop if no openssl to help the next bit
            exit 1
         fi
       	 USEOPENSSLFORXCAT=1 #Though this is the only method going forward, flag to allow backward compatibility with 2.2 generated netboot images
     	 export USEOPENSSLFORXCAT
         XCATSERVER=$i:3001
         export XCATSERVER
         mv $i/postscripts /xcatpost
         rm -rf $i
         chmod +x /xcatpost/*
         /xcatpost/getpostscript.awk |egrep '<data>' |sed -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' | sed -e 's/&lt;/</g' -e 's/&gt;/>/g' -e 's/&amp;/\&/g' -e 's/&quot;/"/g' -e "s/&apos;/'/g" > /xcatpost/mypostscript
         MYCONT=`grep MASTER /xcatpost/mypostscript`
         MAX_RETRIES=10
         RETRY=0
         while [ -z "$MYCONT" ]; do
            RETRY=$(($RETRY+1))
            if [ $RETRY -eq $MAX_RETRIES ]
            then
                break
            fi

            let SLI=$RANDOM%10+10
            sleep $SLI
            /xcatpost/getpostscript.awk |egrep '<data>'|sed -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' | sed -e 's/&lt;/</g' -e 's/&gt;/>/g' -e 's/&amp;/\&/g' -e 's/&quot;/"/g' -e "s/&apos;/'/g" > /xcatpost/mypostscript
            MYCONT=`grep MASTER /xcatpost/mypostscript`
         done


         chmod 700 /xcatpost/mypostscript
			GOTIT=1
			break
		fi
	done
	if [ "$GOTIT" = "1" ]
	then
            #save the master to /opt/xcat/xcatinfo file
            if [ ! -f /opt/xcat/xcatinfo ]; then
                mkdir -p /opt/xcat
                touch /opt/xcat/xcatinfo
            fi
            grep 'XCATSERVER' /opt/xcat/xcatinfo > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                sed -i "s/XCATSERVER=.*/XCATSERVER=$i/" /opt/xcat/xcatinfo
            else
                echo "XCATSERVER=$i" >> /opt/xcat/xcatinfo
            fi

            grep 'HTTPPORT' /opt/xcat/xcatinfo > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                sed -i "s/HTTPPORT=.*/HTTPPORT=$HTTPPORT/" /opt/xcat/xcatinfo
            else
                echo "HTTPPORT=$HTTPPORT" >> /opt/xcat/xcatinfo
            fi
            
	    break
	fi
	RAND=$(perl -e 'print int(rand(5)). "\n"')
	sleep $RAND
done
cd /xcatpost
#gunzip xcatpost.tar.gz
#tar -xvf xcatpost.tar
#/xcatpost/#TABLE:nodelist:THISNODE:node#
export PATH=$PATH:/xcatpost

#save the postboot scripts to /var/tmp/mypostscript.post
#
#  mypostscript.post must survive a reboot.  Traditionally, xCAT has put mypostscript.post in /tmp.
#  However, Ubuntu cleans /tmp on reboot.  So, for Ubuntu, /var/tmp is used instead.

# use the run_ps subroutine to run the postscripts
TMP=`sed "/postscripts-start-here/,/postscripts-end-here/ s/\(.*\)/run_ps postscript \1/;s/run_ps postscript\s*#/#/;s/run_ps postscript\s*$//" /xcatpost/mypostscript`
echo "
# subroutine used to run postscripts
# \$1 argument is the script type
# rest argument is the script name and arguments
run_ps () {
 logdir=\"/var/log/xcat\"
 mkdir -p \$logdir
 logfile=\"/var/log/xcat/xcat.log\"
 scriptype=\$1
 shift;

 if [ -z \"\$scriptype\" ]; then
  scriptype=\"postscript\"
 fi

 if [ -f \$1 ]; then
  echo "\"\`date\` Running \$scriptype: \$1\"" | tee -a \$logfile
  #./\$@ 2>&1 1> /tmp/tmp4xcatlog
  #cat /tmp/tmp4xcatlog | tee -a \$logfile
  ./\$@ 2>&1 | tee -a \$logfile
 else
   echo "\"\`date\` \$scriptype \$1 does NOT exist.\"" | tee -a \$logfile
 fi
}
# subroutine end

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



TMP=`sed "/postscripts-start-here/,/postscripts-end-here/ d" /xcatpost/mypostscript`
echo "$TMP" > /xcatpost/mypostscript.post
chmod 755 /xcatpost/mypostscript.post

#create the post init
cat >/etc/init.d/xcatpostinit1 << 'EOF'
#INCLUDE:#TABLE:site:key=installdir:value#/postscripts/xcatpostinit1#
EOF
chmod 755 /etc/init.d/xcatpostinit1
ln -s /etc/init.d/xcatpostinit1 /etc/rc2.d/S84xcatpostinit1
mkdir -p /opt/xcat
cat >/opt/xcat/xcatinstallpost << 'EOF'
#INCLUDE:#TABLE:site:key=installdir:value#/postscripts/xcatinstallpost#
if [ -f /xcatpost/mypostscript.post ]; then
    RUNBOOTSCRIPTS=`grep 'RUNBOOTSCRIPTS=' /xcatpost/mypostscript.post |cut -d= -f2 | tr -d \'\" | tr A-Z a-z`
fi
if [[ ! "$RUNBOOTSCRIPTS" =~ ^(1|yes|y)$ ]]; then
    chkconfig xcatpostinit1 off
fi
echo "REBOOT=TRUE" >> /opt/xcat/xcatinfo
EOF
chmod 755 /opt/xcat/xcatinstallpost

chkconfig --add xcatpostinit1

#create the dskls post
cat >/opt/xcat/xcatdsklspost << 'EOF'
#INCLUDE:#TABLE:site:key=installdir:value#/postscripts/xcatdsklspost#
EOF
chmod 755 /opt/xcat/xcatdsklspost

#only run the prebooot scripts here
TMP=`sed "/postbootscripts-start-here/,/postbootscripts-end-here/ d" /xcatpost/mypostscript`
echo "$TMP" > /xcatpost/mypostscript

/xcatpost/mypostscript
export NODE=#TABLE:nodelist:THISNODE:node#
export OSVER=#TABLE:nodetype:THISNODE:os#
export ARCH=#TABLE:nodetype:THISNODE:arch#
#addsiteyum
if [[ -r /boot/grub/menu.lst ]]; then
sed -i 's/^serial/#serial/' /boot/grub/menu.lst
sed -i 's/^terminal/#terminal/' /boot/grub/menu.lst
elif [[ -r /boot/grub/grub.cfg ]] ; then
sed -i 's/\(GRUB_CMDLINE_LINUX=\).*/\1\"console=tty0\ console=ttyS#TABLE:nodehm:THISNODE:serialport#,#TABLE:nodehm:THISNODE:serialspeed#\"/' /etc/default/grub
sed -i 's/\(GRUB_CMDLINE_LINUX_DEFAULT=\).*/\1""/g' /etc/default/grub
update-grub
fi
sed -i 's/\(deb.*security.*\)/#\1/' /etc/apt/sources.list
updateflag.awk $MASTER 3002
cd /
#rm -Rf /xcatpost
#rm -f /tmp/mypostscript
exit 0

