#!/bin/sh
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html

#-------------------------------------------------------------------------------
#=head1  otherpkgs
#=head2  It gets the extra rpms and install/update them.
#        The environment variable OTHERPKGS contains the rpms to be installed/updated.
#        On MN, You need to:
#        1. put rpms under /install/post/otherpkgs/os/arch directory where 'os' and 'arch'
#           can be found in the nodetype table.
#        2. put the name of the packages to /opt/xcat/share/xcat/netboot(install)/platform
#           directory. The file name is one of the following:
#              profile.os.arch.otherpkgs.pkglist  
#              profile.os.otherpkgs.pkglist   
#              profile.arch.otherpkgs.pkglist  
#              profile.otherpkgs.pkglist
#   The install/deployment process will pick up the rpms and install them on the nodes. 
#   However, if the nodes have already installed and up and running, you can run the following
#   command to have the extra rpms installed:
#       updatenode noderange otherpkgs 
#   
#=cut
#-------------------------------------------------------------------------------

#echo "OTHERPKGS=$OTHERPKGS"
if [[ -z "$OTHERPKGS" ]]; then 
  echo "$0: no extra rpms to install"
  exit 0
fi

#if [[ $OSTYPE = linux* ]]; then
#  #find out the server
#  SIP=`grep -h dhcp-server-identifier /var/lib/dhclient/dhclient-*.leases|tail -n 1|awk '{print $3}'|sed -e 's/;//'`
#  if [ -z "$SIP" ]; then
#    SIP=`grep -h DHCPSID /var/lib/dhcpcd/*.info|awk -F= '{print $2}'|tail -n 1`
#  fi
#update SIP for SLES11
#if [ "$OSVER" = "sles11" ]; then
#    SIP=`echo $SIP |awk -F\' '{print $2}'` #trim '
#fi

if [[ -z "$NFSSERVER" ]]; then
    NFSSERVER=$MASTER
fi

  if [[ $OTHERPKGS_HASREPO -eq 1 ]]; then
    rpm -q yum
    if [ $? -eq 0 ]; then 
      #use yum
      REPOFILE="/tmp/xCAT-otherpkgs.repo"
      echo "[xcat-otherpkgs]" > $REPOFILE
      echo "name=xcat-otherpkgs" >> $REPOFILE
      echo "baseurl=ftp://$NFSSERVER/post/otherpkgs/$OSVER/$ARCH" >> $REPOFILE
      echo "enabled=1" >> $REPOFILE
      echo "gpgcheck=0" >> $REPOFILE
      #clean meta data to make possible for update
      yum -c $REPOFILE clean metadata

      INSTALLPKGS=""
      UPDATEPKGS=""
      #check if the packges are installed already
      for x in `echo "$OTHERPKGS" | tr "," "\n"`
      do
        rpm -q $x
        if [ $? -eq 0 ]; then
          UPDATEPKGS="$UPDATEPKGS $x"  
        else
          INSTALLPKGS="$INSTALLPKGS $x"  
        fi  
      done      

      #update packages
      if [[ ! -z $UPDATEPKGS ]]; then
        echo "yum -c $REPOFILE -y update $UPDATEPKGS"
        result=`yum -c $REPOFILE -y update $UPDATEPKGS 2>&1`
        if [ $? -ne 0 ]; then 
          echo "$result"
          logger "otherpkgs: $result"
        fi
        exit 0
      fi      

      #install packages
      if [[ ! -z $INSTALLPKGS ]]; then
        echo "yum -c $REPOFILE -y install $INSTALLPKGS"
        result=`yum -c $REPOFILE -y install $INSTALLPKGS 2>&1`
        if [ $? -ne 0 ]; then 
          echo "$result"
          logger "otherpkgs: $result"
        fi
        exit 0
      fi      
    fi
  

    #try zypper
    rpm -q zypper
    if [ $? -eq 0 ]; then 
      #use zypper
      if [ "$OSVER" = "sles11" ]; then
	    zypper ar ftp://$NFSSERVER/post/otherpkgs/$OSVER/$ARCH/1 otherpkgs
      else
        zypper sa ftp://$NFSSERVER/post/otherpkgs/$OSVER/$ARCH/1
      fi

      #install
      PKGS=`echo "$OTHERPKGS" | tr "," " "`
      zypper install -y $PKGS
      exit 0
    fi
  fi

  #if it is not handled by the above code, we will use wget to handle it
  mkdir -p /xcatpost/post/otherpkgs/$OSVER/$ARCH; 
  rm -f -R /xcatpost/post/otherpkgs/*
  mkdir -p /tmp/postage/
  rm -f -R /tmp/postage/*
  cd /tmp/postage

  for x in `echo "$OTHERPKGS" | tr "," "\n"`
  do
    wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 0 -T 60 ftp://$NFSSERVER/post/otherpkgs/$OSVER/$ARCH/$x-* 2> /tmp/wget.log  
  done

  mv $NFSSERVER/post/otherpkgs/* /xcatpost/post/otherpkgs; 
  rm -rf $NFSSERVER

  cd /xcatpost/post/otherpkgs/$OSVER/$ARCH
  result=`rpm -Uvh * 2>&1`
  echo "$result"
  if [ $? -ne 0 ]; then 
    logger "otherpkgs $result"
  fi
  exit 0
else #AIX
  #mkdir -p /xcatpost/post/otherpkgs/$OSVER/$ARCH
  #rm -f -R /xcatpost/post/otherpkgs/$OSVER/$ARCH/*

  # get the name of my service node/NIM master from the /etc/niminfo file
  #if [ -f "/etc/niminfo" ]; then
  #  servnode=`grep NIM_MASTER_HOSTNAME /etc/niminfo|tr "=" " "|awk {'print $3'}`
  #  echo "servnode=$servnode"
  #else 
  #  echo "Could not find /etc/niminfo file"
  #  logger "otherpkgs: Could not find /etc/niminfo file" 
  #  exit 1
  #fi

  #for x in `echo "$OTHERPKGS" | tr "," "\n"`
  #do
  #  result=`rcp -r $servnode:/install/post/otherpkgs/$OSVER/$ARCH/$x* /xcatpost/post/otherpkgs/$OSVER/$ARCH/.`
  #  if [ $? -ne 0 ]; then 
  #    echo "$result"
  #    logger "otherpkgs: $result"
  #  fi
  #done

  #on AIX use geninstall
  #PKGS=`echo "$OTHERPKGS" | tr "," " "`
  #cd /xcatpost/post/otherpkgs/$OSVER/$ARCH
  #result=`geninstall -I aX -Y -d /xcatpost/post/otherpkgs/$OSVER/$ARCH $PKGS 2>&1`
  #rc=$?
  #if [ $rc -ne 0 ]; then
  #  echo "$result"
  #  logger "otherpkgs: $result"
  #fi
  #exit $rc

  echo "Please use nimnodecust command to add additional packages to AIX nodes."
  logger "xCAT otherpkgs: Please use nimnodecust command to add addition packages to AIX nodes."
fi

exit 0