#!/bin/sh  

#
# Sample script to accept license and install LoadLeveler resmgr package
# and perform additional customizations
# For AIX:
#    add LL path to profile
#    create directories for LL log, spool, etc.
# For Linux:
#    Add LL bin directory to PATH in default profile (modify "linux_loadl_bin"
#      if using a different LL bin location)
#    Create LL log directories (modify "logdir" if not using /var/loadl)
#    Change the owner of these directories (modify "loadl_admin" and
#           "loadl_group" if not using loadl:loadl)
#    If run from genimage, copy in /etc/LoadL.cfg from MN into image
#    


OS=`uname`
INSTALL_DIR='/install'
LOADL_DIR=$loadldir

#### 
#  Linux Note:  This script only installs the LoadL_resmgr_full rpm
#  To also install the LoadL_scheduler rpm, remove the "-c resmgr"
#  option from the line below
#  Also, change the bin path below to the correct directory
#### 
linux_loadl_license_script="/opt/ibmll/LoadL/sbin/install_ll -c resmgr"
#linux_loadl_license_script="/opt/ibmll/LoadL/sbin/install_ll"
linux_loadl_bin=/opt/ibmll/LoadL/resmgr/full/bin

####
# For AIX, the LoadLeveler lpps are listed in the loadl.bnd bundle file
# By default, it will only install the LoadL.resmgr package
# To install the full LoadLeveler product, edit that bundle file
# and change the bin path below to the correct directory
####
aix_loadl_bin=/usr/lpp/LoadL/resmgr/full/bin


logdir=/var/loadl
loadl_admin=loadl
loadl_group=loadl

if [ -z "$LOADL_DIR" ]; then
   # try to default
   LOADL_DIR=$INSTALL_DIR/post/otherpkgs/$OSVER/$ARCH/loadl
fi


if [ $OS != "AIX" ]; then
    if [ $NODESETSTATE == "install" ] || [ $NODESETSTATE == "boot" ]; then
    #  Being run from a stateful install postscript
    #  Copy rpms directly from the xCAT management node and install
        groupadd $loadl_group
        useradd -g $loadl_group $loadl_admin
        mkdir -p /tmp/loadl
        rm -f -R /tmp/loadl/*
        cd /tmp/loadl
        download_dir=`echo $LOADL_DIR | cut -d '/' -f3-`
        wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 10 -T 60 -nH --cut-dirs=5 ftp://$SITEMASTER/$download_dir/*.rpm 2> /tmp/wget.log
        rpm -Uvh LoadL-full-license*.rpm
        $linux_loadl_license_script -y -d .
        rm -Rf /tmp/loadl
        mkdir /dev/cpuset
        mount -t cpuset none /dev/cpuset
    fi

    if [ $NODESETSTATE == "genimage" ]; then
        # Being called from <image>.postinstall script
        # Assume we are on the same machine
        tmpdir=tmploadl
        mkdir $installroot/$tmpdir
        cp -p $LOADL_DIR/* $installroot/$tmpdir
        # The license rpm should already be installed, but just in case...
        rpm --root $installroot -Uvh $installroot/$tmpdir/LoadL-full-license*.rpm
        chroot $installroot mount -t proc none /proc
        chroot $installroot $linux_loadl_license_script -y -d  /$tmpdir
        umount -l $installroot/proc
        rm -rf $installroot/$tmpdir
    fi
fi

# Add LoadLeveler path to profile
if [ "$OS" == "AIX" ]; then
    # add to /etc/profile
    if ! grep 'LoadL' /etc/profile  > /dev/null 2>&1 ; then
        echo "export PATH=\$PATH:$aix_loadl_bin" >> /etc/profile
    fi
    # UNCOMMENT to add to /etc/inittab if not already in image
    #if ! grep 'loadl' /etc/inittab ; then
    #   echo "loadl:2:once:/usr/lpp/LoadL/resmgr/full/bin/llrctl start > /dev/console 2>&1" >> /etc/inittab
   #fi


else  # assume Linux
    loadlprofile=/etc/profile.d/loadl
    if [ $NODESETSTATE == "genimage" ]; then
       loadlprofile=$installroot$loadlprofile
    fi
    if [ ! -e $loadlprofile.sh ]; then
        echo "export PATH=\$PATH:$linux_loadl_bin" > $loadlprofile.sh
        echo "export PATH=\$PATH:$linux_loadl_bin" > $loadlprofile.csh
        # Turn off LANG support since we did not install other msg catalogs
        echo 'export LC_CTYPE=POSIX' $loadlprofile.sh
        echo 'export LC_CTYPE=POSIX' $loadlprofile.csh
        echo 'export LC_ALL=POSIX' $loadlprofile.sh
        echo 'export LC_ALL=POSIX' $loadlprofile.csh
        chmod 744 $loadlprofile.sh
        chmod 744 $loadlprofile.csh
    fi
fi



# Create LoadLeveler Directories:
# For stateless nodes, these directories need to be created in the image
# since they will be in memory only, and will be lost on node reboot. 
# You may choose to modify this approach and use a more 
# persistent location. 
# For statelite, make sure the directory is writable;  
# this will also be a memory-only copy of the files unless you set 
# the directory "persistent" in the statelite table.  
if [ $NODESETSTATE != "genimage" ]; then
   # running as a postscript in a full-disk install or AIX diskless install
   installroot=""
fi  
mkdir -p $installroot$logdir/execute
mkdir -p $installroot$logdir/spool
mkdir -p $installroot$logdir/log
mkdir -p $installroot$logdir/sockets
mkdir -p $installroot$logdir/core
chmod 700 $installroot$logdir/spool
chmod 1777 $installroot$logdir/execute
chmod 775 $installroot$logdir/log
chmod 777 $installroot$logdir/sockets
chmod 777 $installroot$logdir/core

# Owner set to 'loadl' userid, change if using a different userid
chown -R $loadl_admin:$loadl_group $installroot$logdir/

if [ "$NODESETSTATE" == "genimage" ] && [ -f /etc/LoadL.cfg ] ; then
    cp /etc/LoadL.cfg $installroot/etc/LoadL.cfg
fi


# UNCOMMENT to  Start loadleveler during postscripts 
#if [ $NODESETSTATE != "genimage" ]; then
#   /usr/lpp/LoadL/resmgr/full/bin/llrctl start
#fi

