# Build the xCAT-genesis-base rpm on a host system.
# Option 1:  install the xCAT-genesis-builder rpm (that includes this file) on a system installed with
#            the distro you want to use to build xCAT-genesis-base.
# Option 2:  untar the root file system of the distro you want to use and then chroot into it and copy
#            this whole dir into it somewhere (like /tmp).
# Then run this script.  The optional 1st arg should be mcp if you are building against mcp.

# Currently, *Fedora 26* is the only OS supported to build genesis-base for ppc64, and Centos 6.5 for x86_64

HOSTOS="$1"
DIR=`dirname $0`
#DIR=`realpath $DIR`
DIR=`readlink -f $DIR`
BUILDARCH=`uname -m`

rpmdev-setuptree

#For Openpower
if [ -z $1 ]; then
    if [ $BUILDARCH = "ppc64le" ]; then
        HOSTOS="Pegas1.0"
        BUILDARCH="ppc64"
    elif [ $BUILDARCH = "ppc64" ]; then
        HOSTOS="fedora26"
    elif [ $BUILDARCH = "x86_64" ]; then
        yum install efibootmgr bc -y
        HOSTOS="centos7"
    fi
fi

# get the input files for dracut in the right place
# Fedora 20 ppc64 uses /usr/lib/dracut/modules.d
# CentOS 7 probably uses /usr/lib/dracut/modules.d also
if [ -e "/usr/share/dracut/modules.d" ]
then
    DRACUTMODDIR=/usr/share/dracut/modules.d/97xcat
else
    DRACUTMODDIR=/usr/lib/dracut/modules.d/97xcat
fi

# get all modules in the kernel
cd $DIR
KERVER=`uname -r`
if [ ! -e "./default_module_list" ]; then
    mv ./installkernel ./default_module_list
fi
echo "#!/bin/bash" > ./installkernel
for line in `cat /lib/modules/$KERVER/modules.dep | awk -F: '{print \$1}'`; do
    basename $line >> ./installkernel;
done
sed -i 's/\(.*\)\.ko.*/instmods \1/g' ./installkernel
chmod +x ./installkernel
cd -

mkdir -p $DRACUTMODDIR
cp $DIR/* $DRACUTMODDIR
# Remove the ipr(IBM Power RAID) stuff when building on x86_64
if [ $BUILDARCH = "x86_64" ]; then
    sed -i 's/dracut_install \/lib64\/libform.so.5//' $DRACUTMODDIR/install
    sed -i 's/\/lib\/libtinfo.so.5.7/\/lib64\/libtinfo.so.5.7/' $DRACUTMODDIR/install
    sed -i 's/\/lib64\/libpanel.so.5//' $DRACUTMODDIR/install
    sed -i 's/\/lib64\/libmenu.so.5//' $DRACUTMODDIR/install
    sed -i 's/\/lib64\/libsysfs.so.2//' $DRACUTMODDIR/install
    sed -i '/\/usr\/sbin\/iprconfig/ d' $DRACUTMODDIR/install
    sed -i '/hwdb.bin/ d' $DRACUTMODDIR/install
    sed -i 's/instmods ipr//' $DRACUTMODDIR/installkernel
    sed -i 's/ mkreiserfs//' $DRACUTMODDIR/install
    sed -i 's/ reiserfstune//' $DRACUTMODDIR/install
    sed -i 's/ vconfig//' $DRACUTMODDIR/install
    sed -i 's/\/lib\/terminfo\/l\/linux/\/usr\/share\/terminfo\/l\/linux/g' $DRACUTMODDIR/install
    sed -i 's/\/lib\/terminfo\/v\/vt100/\/usr\/share\/terminfo\/v\/vt100/g' $DRACUTMODDIR/install
fi
if [ "$HOSTOS" = "mcp" ]; then
        #Special handlings for MCP PPC64 platform building.
        if [ $BUILDARCH = "ppc64" ]; then
            sed -i 's/dracut_install efibootmgr//' $DRACUTMODDIR/install
            sed -i 's/dracut_install dmidecode \/usr\/lib64\/libstdc++.so.5//' $DRACUTMODDIR/install
            sed -i 's/dmidecode//' $DRACUTMODDIR/install
            sed -i 's/\/lib\/ld-linux.so.2/\/usr\/lib64\/ld-2.17.so/' $DRACUTMODDIR/install
            sed -i 's/\/lib64\/libsysfs.so.2//' $DRACUTMODDIR/install
            sed -i '/\/usr\/sbin\/iprconfig/ d' $DRACUTMODDIR/install
        else
            sed -i 's/\/lib\/ld-linux.so.2/\/usr\/lib64\/ld-linux-x86-64.so.2/' $DRACUTMODDIR/install
        fi
	sed -i 's/mkfs.btrfs//' $DRACUTMODDIR/install
	sed -i 's/\/etc\/redhat-release/\/etc\/base-release \/etc\/system-release/' $DRACUTMODDIR/install
	sed -i 's/btrfs//' $DRACUTMODDIR/installkernel

	sed -i 's/\/usr\/bin\/basename/\/bin\/basename/' $DRACUTMODDIR/install

	sed -i 's/\/lib64\/libnss_dns-2.12.so/\/usr\/lib64\/libnss_dns-2.17.so/' $DRACUTMODDIR/install
	sed -i 's/\/lib64\/libnss_dns.so.2/\/usr\/lib64\/libnss_dns.so.2/' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libc.so.6/\/usr\/lib64\/libc.so.6/' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libdl.so.2/\/usr\/lib64\/libdl.so.2/' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libm.so.6/\/usr\/lib64\/libm.so.6/' $DRACUTMODDIR/install
        sed -i 's/\/usr\/lib\/libstdc++.so.6.0.13/\/usr\/lib64\/libstdc++.so.6.0.19/' $DRACUTMODDIR/install
        sed -i 's/\/usr\/lib64\/libstdc++.so.5//' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libpthread.so.0/\/usr\/lib64\/libpthread.so.0/' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libncurses.so.5.7/\/usr\/lib64\/libncurses.so.5.9/' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libgcc_s.so.1/\/usr\/lib64\/libgcc_s.so.1/' $DRACUTMODDIR/install
        sed -i 's/\/lib\/libtinfo.so.5.7/\/usr\/lib64\/libtinfo.so.5.9/' $DRACUTMODDIR/install
        sed -i 's/\/usr\/lib64\/libsasl2.so.2/\/usr\/lib64\/libsasl2.so.3/' $DRACUTMODDIR/install

        sed -i 's/\/lib\/terminfo\/l\/linux/\/usr\/share\/terminfo\/l\/linux/g' $DRACUTMODDIR/install
        sed -i 's/\/lib\/terminfo\/v\/vt100/\/usr\/share\/terminfo\/v\/vt100/g' $DRACUTMODDIR/install

	# these are needed for sysclone, but are not yet in mcp
	sed -i 's/ bc//' $DRACUTMODDIR/install
	sed -i 's/ mkdosfs//' $DRACUTMODDIR/install
	sed -i 's/ rsync//' $DRACUTMODDIR/install
	sed -i 's/ kexec//' $DRACUTMODDIR/install
	sed -i 's/ klogd//' $DRACUTMODDIR/install
	sed -i 's/ mdadm//' $DRACUTMODDIR/install
	sed -i 's/ mkfs\.xfs//' $DRACUTMODDIR/install
	sed -i 's/ xfs_db//' $DRACUTMODDIR/install
	sed -i 's/ mkreiserfs//' $DRACUTMODDIR/install
	sed -i 's/ reiserfstune//' $DRACUTMODDIR/install
	sed -i 's/ vconfig//' $DRACUTMODDIR/install
	sed -i 's/ killall//' $DRACUTMODDIR/install

        # These timezone files are not available in the latest mcp build
        sed -i 's/dracut_install \/usr\/share\/zoneinfo\/posix\/Asia\/Riyadh87//' $DRACUTMODDIR/install
        sed -i 's/dracut_install \/usr\/share\/zoneinfo\/posix\/Asia\/Riyadh88//' $DRACUTMODDIR/install
        sed -i 's/dracut_install \/usr\/share\/zoneinfo\/posix\/Asia\/Riyadh89//' $DRACUTMODDIR/install
        sed -i 's/dracut_install \/usr\/share\/zoneinfo\/posix\/Mideast\/Riyadh87//' $DRACUTMODDIR/install
        sed -i 's/dracut_install \/usr\/share\/zoneinfo\/posix\/Mideast\/Riyadh88//' $DRACUTMODDIR/install
        sed -i 's/dracut_install \/usr\/share\/zoneinfo\/posix\/Mideast\/Riyadh89//' $DRACUTMODDIR/install

# For ppc64 platform, needs to remove some files,
# # and some files are in different directories
elif [ $BUILDARCH = "ppc64" ]; then
        if [ "$HOSTOS" = "Pegas1.0" ]; then
           sed -i 's/ mkreiserfs//' $DRACUTMODDIR/install
           sed -i 's/ reiserfstune//' $DRACUTMODDIR/install
           sed -i 's/ vconfig//' $DRACUTMODDIR/install
        fi
        sed -i 's/ efibootmgr//' $DRACUTMODDIR/install
        sed -i 's/ dmidecode//' $DRACUTMODDIR/install
        sed -i 's/\/lib\/terminfo\/l\/linux/\/usr\/share\/terminfo\/l\/linux/g' $DRACUTMODDIR/install
        sed -i 's/\/lib\/terminfo\/v\/vt100/\/usr\/share\/terminfo\/v\/vt100/g' $DRACUTMODDIR/install
fi

libnss_pkgname=`find /usr/lib64/ -name libnss_dns-2*.so | xargs basename`
sed -i "s/\/lib64\/libnss_dns-2.12.so/\/usr\/lib64\/$libnss_pkgname/g" $DRACUTMODDIR/install
sed -i 's/\/lib64\/libnss_dns.so.2/\/usr\/lib64\/libnss_dns.so.2/' $DRACUTMODDIR/install

# Based on cmdlist_check, makesure the commands are included in $DRACUTMODDIR/install
if [ -e "${DRACUTMODDIR}/cmdlist_check" ]; then
    miss_cmd=0
    for cmd in `cat ${DRACUTMODDIR}/cmdlist_check`; do
        if ! grep $cmd $DRACUTMODDIR/install; then
            echo "ERROR: The required $cmd is not included in the install file to build the Genesis base package"
            miss_cmd=1
        fi
    done
    if [ "$miss_cmd" = 1 ]; then
        exit 1
    fi
fi

mkdir -p /tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/fs

# run dracut
if [ "$HOSTOS" = "mcp" ]; then
	KPATH=`/bin/ls -d /lib/modules/*`
	KERNELVERSION=`basename $KPATH`
	echo Creating the initramfs in /tmp/xcatgenesis.$$.rfs using dracut and kernel $KERNELVERSION ...
else
	echo Creating the initramfs in /tmp/xcatgenesis.$$.rfs using dracut ...
fi
# On Fedora 20 ppc64, dracut uses host-only mode by default
if [ $BUILDARCH = "ppc64" ]; then
    dracut -m "xcat base" -N -f /tmp/xcatgenesis.$$.rfs $KERNELVERSION
else
    dracut -m "xcat base" --no-early-microcode -N -f /tmp/xcatgenesis.$$.rfs $KERNELVERSION
fi

if [ $? -ne 0 ]; then
    echo "ERROR - creating the initramfs, please correct the issues and try again"
    exit 1
fi

echo Expanding the initramfs into /tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/fs ...
cd /tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/fs

zcat /tmp/xcatgenesis.$$.rfs|cpio -dumi

# add the perl library
# add /usr/share/ntp/lib for Fedora26 ppc64, the ntp-perl will installed libraries under it
PERL_LIB_DIR="/usr/share/perl5 /usr/lib64/perl5 /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/share/ntp/lib"
for d in `echo $PERL_LIB_DIR`; do
    if [ -e $d ]; then
        echo Adding perl libary "$d"
        TEMP_DIR=/tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/fs/"$d"
        mkdir -p $TEMP_DIR
        cp -a -t $TEMP_DIR $d/.
    fi
done


# create the predictable naming for nics
LIB_UDEV_RULES="/lib/udev/rules.d/"
if [ ! -e "$LIB_UDEV_RULES/80-net-name-slot.rules" ]; then
    cp $DRACUTMODDIR/80-net-name-slot.rules /tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/fs/lib/udev/rules.d/
    if [ $? -ne 0 ]; then
        echo "ERROR - expanding the initramfs, please correct the issues and try again"
        exit 1
    fi
fi

# add the kernel
if [ "$HOSTOS" = "mcp" ]; then
	echo Adding kernel /boot/vmlinuz-* ...
	cp /boot/vmlinuz-* /tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/kernel
else
	echo Adding kernel /boot/vmlinuz-$BUILDARCH ...
	cp /boot/vmlinuz-`uname -r` /tmp/xcatgenesis.$$/opt/xcat/share/xcat/netboot/genesis/$BUILDARCH/kernel
fi
cd -

# create tar file
echo Tarring /tmp/xcatgenesis.$$/opt into ~/rpmbuild/SOURCES/xCAT-genesis-base-$BUILDARCH.tar.bz2 ...
cd /tmp/xcatgenesis.$$
tar jcf ~/rpmbuild/SOURCES/xCAT-genesis-base-$BUILDARCH.tar.bz2 opt

# build the rpm
echo Building xCAT-genesis-base rpm from ~/rpmbuild/SOURCES/xCAT-genesis-base-$BUILDARCH.tar.bz2 and $DIR/xCAT-genesis-base.spec ...
rpmbuild -ba $DIR/xCAT-genesis-base.spec
rm -rf $DRACUTMODDIR
