#!/bin/ksh
#egan@us.ibm.com
#See $XCATROOT/README for license.
#

if [ -z "$XCATROOT" ]
then
	if [ -r /etc/sysconfig/xcat ]
	then
		. /etc/sysconfig/xcat
	else
		if [ -r /etc/rc.config ]
		then
			. /etc/rc.config
		fi
	fi
fi

if [ -z "$XCATROOT" ]
then
	if [ -r "$(dirname $0)/../lib/xcatroot" ]
	then
		. $(dirname $0)/../lib/xcatroot
	fi
fi

if [ -z "$XCATROOT" ]
then
	echo "$(basename $0): env XCATROOT not defined!" >&2
	exit 1
fi

if [ ! -d "$XCATROOT" ]
then
	echo "$(basename $0): XCATROOT $XCATROOT does not exist!" >&2
	exit 1
fi

. $XCATROOT/lib/functions

function usage {
	echo "\nUsage: ${MYNAME} [singlenode]\n"
}

needhelp $* && usage >&2 && exit 0

if [ "$#" != "1" ]
then
	usage >&2
	exit 1
fi

NODE=$1
NODENAME=$1

if ! isanode $1 2>/dev/null
then
	echo "${MYNAME}: $1 is not a node" >&2
	exit 1
fi

if NODETYPE=$(tabdb $NODETYPETAB $NODE 3)
then
	:
else
	echo "${MYNAME}: $NODE nodetype image not defined in $NODETYPETAB" >&2
	exit 1
fi

if OSVER=$(tabdb $NODETYPETAB $NODE 1)
then
	:
else
	echo "${MYNAME}: $NODE nodetype osver not defined in $NODETYPETAB" >&2
	exit 1
fi

if ARCH=$(tabdb $NODETYPETAB $NODE 2)
then
	:
else
	echo "${MYNAME}: $NODE nodetype arch not defined in $NODETYPETAB" >&2
	exit 1
fi

if NODERES=$(whatismyres2 $NODE)
then
	RESNAME=$NODERES
else
	echo "${MYNAME}: $NODE noderes not defined in $NODERESTAB" >&2
	exit 1
fi

MYOSVER=rhas3
MYARCH=x86

if [ "$OSVER" != "$MYOSVER" -o "$ARCH" != "$MYARCH" ]
then
	echo "${MYNAME}: Install directory does not match $NODE in $NODETYPETAB"
	exit 1
fi

if INSTALLDIR=$(tabdb $SITETAB installdir 1)
then
	:
else
	echo "$MYNAME: installdir not defined in $SITETAB"
	exit 1
fi

DISTDIR=$OSVER/$ARCH
CFDIR=scripts/$OSVER/$ARCH
PXET=install.pxe
GRUBT=install.grub
ELILOT=elilo.conf
KERNEL=installz
INITRD=install.gz

FILES="$INSTALLDIR/$DISTDIR/images/pxeboot/initrd.img $INSTALLDIR/$DISTDIR/images/pxeboot/vmlinuz /usr/share/terminfo/x/xterm $TFTPDIR/$TFTPXCATROOT $TFTPDIR"

if [ -r $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$KERNEL -a -r  $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$INITRD ]
then
	DOIMAGE=0
else
	DOIMAGE=1
fi

EXIT=0
for i in $FILES
do
	if [ ! -r $i ]
	then
		echo "${MYNAME}: $i missing"
		EXIT=1
	fi
	if [ "$EXIT" = "1" ]
	then
		exit 1
	fi
done

if [ -r $XCATPREFIX/tftp/pxelinux.0 ]
then
	mkdir $TFTPDIR >/dev/null 2>&1
	cp -f $XCATPREFIX/tftp/pxelinux.0 $TFTPDIR
else
	echo "$MYNAME: $XCATPREFIX/tftp/pxelinux.0 missing"
	exit 1
fi

if [ -r $XCATPREFIX/tftp/default ]
then
	mkdir $TFTPDIR/pxelinux.cfg >/dev/null 2>&1
	cp -f $XCATPREFIX/tftp/default $TFTPDIR/pxelinux.cfg/default
else
	echo "$MYNAME: $XCATPREFIX/tftp/default missing"
	exit 1
fi

MASTER=$(tabdb $SITETAB master 1) \
	|| echo "${MYNAME}: master not defined in $SITETAB"
MASTER_IP=$(nametoip $MASTER)

#MASTER_IPS=$(
#	IPS=""
#	for i in $(dnsnr "@$MASTER(-eth[0-9])?")
#	do
#		if IP=$(nametoip $i)
#		then
#			IPS="$IPS $IP"
#		fi
#	done
#	print $IPS
#)

MASTER_IPS=$(
	IPS=""
	for i in $(ifconfig | egrep '^eth[0-9]' | awk '{print $1}')
	do
		IP=$(ifconfig $i | perl -pi -e 's/ +/:/g' | grep inet:addr: | awk -F: '{print $4}')
		IPS="$IPS $IP"
	done
	print $IPS
)

if CONFIG_SERVER=$(tabdb $NODERESTAB $RESNAME $noderes_config_server)
then
	CONFIG_IP=$(nametoip $CONFIG_SERVER)
else
	CONFIG_IP=$MASTER_IP
	CONFIG_SERVER=$MASTER
fi

RSHC=$(tabdb $SITETAB rsh 1) \
	|| echo "${MYNAME}: rsh not defined in $SITETAB"

if [ "$(basename $RSHC)" = "rsh" ]
then
	RSH="Y"
else
	RSH="N"
fi

SERIAL=$(tabdb $NODERESTAB $RESNAME $noderes_serial) \
	|| echo "${MYNAME}: serial defined in $NODERESTAB"

INSTALL_NFS=$(tabdb $NODERESTAB $RESNAME $noderes_nfs_install) \
	|| echo "${MYNAME}: nfs not defined in $NODERESTAB"
INSTALL_NFS_IP=$(nametoip $INSTALL_NFS)

if [ "$INSTALL_NFS_IP" = "noip" ]
then
	echo "Invalid Install NFS for $NODENAME"
fi

INSTALL_NIC=$(tabdb $NODERESTAB $RESNAME $noderes_netdevice) \
	|| echo "${MYNAME}: install_nic not defined in $NODERESTAB"

INSTALL_DIR=$(tabdb $NODERESTAB $RESNAME $noderes_install_dir) \
	|| echo "${MYNAME}: nfs dir not defined in $NODERESTAB"

INSTALL_SRC_DIR=$INSTALL_DIR/$DISTDIR
INSTALL_CF_DIR=$INSTALL_DIR/$CFDIR

mkdir -p $INSTALL_CF_DIR

FILE=$INSTALLDIR/$CFDIR/$NODETYPE-$NODERES
INSTALL_CF_FILE=$NODETYPE-$NODERES

if [ ! -r $NODETYPE.tmpl ]
then
	if [ ! -r base/$NODETYPE.tmpl ]
	then
		echo "${MYNAME}: cannot read template $NODETYPE.tmpl"
		exit 1
	else
		TMPLDIR="base"
	fi
else
	TMPLDIR="."
fi

export MASTER
export MASTER_IP
export MASTER_IPS
export CONFIG_IP CONFIG_SERVER
export RSH
export OSVER
export ARCH
export NODE
export NODERES
export NODENAME
export NODETYPE
export INSTALL_NFS
export INSTALL_NFS_IP
export INSTALL_SRC_DIR
export INSTALL_CF_DIR
export INSTALL_CF_FILE
export KERNEL
export INITRD

perl -pi -e 's/\\\n$//' <$TMPLDIR/$NODETYPE.tmpl | LANG=C subvars.pl >$FILE

mkdir -p $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH

if [ -r "$PXET" ]
then
	PXEF=$TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$NODETYPE-$NODERES.pxe
	perl -pi -e 's/\\\n$//' <$PXET | LANG=C subvars.pl >$PXEF
	if [ "$SERIAL" = "0" -o "$SERIAL" = "1" ]
	then
		:
	else
		perl -pi -e "s/^SERIAL/#SERIAL/g" $PXEF
#		perl -pi -e "s/(console[^ ]* )//" $PXEF
		perl -pi -e "s/(console[^ ]* ){2}//" $PXEF
	fi
fi

if [ -r "$GRUBT" ]
then
	GRUBF=$TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$NODETYPE-$NODERES.grub
	perl -pi -e 's/\\\n$//' <$GRUBT | LANG=C subvars.pl >$GRUBF
	if [ "$SERIAL" = "0" -o "$SERIAL" = "1" ]
	then
		:
	else
		perl -pi -e "s/^serial/#serial/g" $GRUBF
		perl -pi -e "s/^terminal/#terminal/g" $GRUBF
#		perl -pi -e "s/(console[^ ]* )//" $GRUBF
		perl -pi -e "s/(console[^ ]* ){2}//" $GRUBF
	fi
fi

if [ -r "$ELILOT" ]
then
	ELILOC=$TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$NODETYPE-$NODERES.elilo
	perl -pi -e 's/\\\n$//' <$ELILOT | LANG=C subvars.pl >$ELILOC
	if [ "$SERIAL" = "0" -o "$SERIAL" = "1" ]
	then
		:
	else
		perl -pi -e "s/^SERIAL/#SERIAL/g" $ELILOC
#		perl -pi -e "s/console[^ ]* //" $ELILOC
		perl -pi -e "s/(console[^ ]* ){2}//" $ELILOC
	fi
fi

if [ "$DOIMAGE" = "0" ]
then
	exit 0
fi

cp -f $INSTALLDIR/$DISTDIR/images/pxeboot/vmlinuz $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$KERNEL
cp -f $INSTALLDIR/$DISTDIR/images/pxeboot/initrd.img $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$INITRD

TMPDIR=/tmp/.mkinstall-$OSVER-$ARCH

if getlock mkinstall-$OSVER-$ARCH 60
then
	:
else
	echo "${MYNAME} Failed to get $TMPDIR lock"
	exit 1
fi

rm -rf $TMPDIR
mkdir -p $TMPDIR

gunzip -c $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$INITRD >$TMPDIR/initrd 2>/dev/null
mkdir -p $TMPDIR/root
mount -o loop $TMPDIR/initrd $TMPDIR/root
#mkdir -p $TMPDIR/root/etc/terminfo/x
#mkdir -p $TMPDIR/root/etc/terminfo/v
#mkdir -p $TMPDIR/root/etc/terminfo/b
#cp -f /usr/share/terminfo/x/xterm $TMPDIR/root/etc/terminfo/v/vt100
#cp -f /usr/share/terminfo/x/xterm $TMPDIR/root/etc/terminfo/b/bterm
#cp -f /usr/share/terminfo/x/xterm $TMPDIR/root/etc/terminfo/x/xterm

###
#rm -f $TMPDIR/root/etc/terminfo/v/vt100-nav

perl -pi -e 's/^.*qla2.00.*\n$//' $TMPDIR/root/modules/pcitable

#MYDIR=$(pwd)
#cd $TMPDIR

#gunzip -c root/modules/modules.cgz | cpio -id 2>/dev/null

#if [ -r $MYDIR/data/e100.o ]
#then
#	rm -f 2.4.21-3.ELBOOT/eepro100.o
#	cp -f $MYDIR/data/e100.o 2.4.21-3.ELBOOT/eepro100.o
#	cat $MYDIR/data/e100.pcitable >> root/modules/pcitable
#fi

#if [ -r $MYDIR/data/bcm5700.o ]
#then
#	cp -f $MYDIR/data/bcm5700.o 2.4.21-3.ELBOOT/bcm5700.o
#	cat $MYDIR/data/bcm5700.pcitable >> root/modules/pcitable
#	cat $MYDIR/data/bcm5700.module-info >> root/modules/module-info
#fi

#if [ -r $MYDIR/data/e1000.o ]
#then
#	cp -f $MYDIR/data/e1000.o 2.4.21-3.ELBOOT/e1000.o
#	cat $MYDIR/data/e1000.pcitable >> root/modules/pcitable
#	cat $MYDIR/data/e1000.module-info >> root/modules/module-info
#fi

#find 2.4.21-3.ELBOOT -type f -print | cpio -o -H crc 2>/dev/null | gzip -c -9 >root/modules/modules.cgz

#cd $MYDIR


# default kernel version that ships with RHEL3-U4
KERNELVER=2.4.21-27
#
# try and detect if we're actually installing CentOS instead of RHEL
# ( tests cut&paste'd from XCATROOT/sbin/copycds )
#
if [ -f $INSTALLDIR/$DISTDIR/.discinfo ]
then
	if egrep "1104951135.38" $INSTALLDIR/$DISTDIR/.discinfo >/dev/null 2>&1
	then
		if grep "i386" $INSTALLDIR/$DISTDIR/.discinfo >/dev/null 2>&1
		then
			# kernel version in the CentOS-3.4 distro
			KERNELVER=2.4.21-27.0.1
		fi

	fi
fi

#
# fix the broken tg3 driver in RHEL3-U4/CentOS-3.4
#
if [ -f $INSTALLDIR/$DISTDIR/RedHat/RPMS/kernel-${KERNELVER}.EL.i686.rpm ]
then
	MYDIR=$(pwd)
	cd $TMPDIR
	gunzip -c root/modules/modules.cgz | cpio -id 2>/dev/null

	if [ -r $MYDIR/data/27.EL/bcm5700.o ]
	then
		cp -f $MYDIR/data/27.EL/bcm5700.o ${KERNELVER}.ELBOOT/i386/tg3.o
	fi

	find ${KERNELVER}.ELBOOT -type f -print | cpio -o -H crc 2>/dev/null | gzip -c -9 >root/modules/modules.cgz

	cd $MYDIR
fi

umount $TMPDIR/root
gzip -9 -f $TMPDIR/initrd
cp -f $TMPDIR/initrd.gz $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH/$INITRD

rm -rf $TMPDIR

freelock mkinstall-$OSVER-$ARCH 0

cd $TFTPDIR/$TFTPXCATROOT/$OSVER/$ARCH

chown -f root.root *.gz *.pxe *.nbi *.eb
chmod -f 444 *.gz *.pxe *.nbi *.eb

exit 0

