#!/bin/sh
# postinst script for xCAT-rmc
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
      needCopyFiles=0
      if [ -f /etc/xCATMN ]; then
         #on MN
         needCopyFiles=1;
      else
         #om SN
         mounted=0
         result=`mount |grep /install`
         if [ $? -eq 0 ]; then
	   mounted=1
         fi
         if [ $mounted -eq 0 ]; then
	   needCopyFiles=1;
         fi
      fi
    
      if [ $needCopyFiles -eq 1 ]; then
         echo "Copying files to /install/postscripts directory..."
         mkdir -p /install/postscripts
         mkdir -p /install/postscripts/rmcmon/resources/node
         mkdir -p /install/postscripts/rmcmon/scripts
         cp /opt/xcat/sbin/rmcmon/configrmcnode /install/postscripts
         chmod 755 /install/postscripts/configrmcnode
    
         FILES_TO_COPY=`cat /opt/xcat/sbin/rmcmon/scripts_to_node|tr '\n' ' '` 
         for file in $FILES_TO_COPY
         do
	    #echo "file=$file"
	    cp /opt/xcat/sbin/rmcmon/$file /install/postscripts/rmcmon/scripts
         done
         chmod 755 /install/postscripts/rmcmon/scripts/*
    
         cp -r /opt/xcat/lib/perl/xCAT_monitoring/rmc/resources/node/* /install/postscripts/rmcmon/resources/node
      fi  
    

      if [[ `uname -s` = "Linux" ]] ; then
         if [ -f "/proc/cmdline" ]; then   # prevent running it during install into chroot image
           if [ -f /opt/xcat/sbin/xcatd  ]; then
              /etc/init.d/xcatd reload
           fi
         fi
      else
      #restart the xcatd on if xCAT or xCATsn is installed already
         if [ -f $RPM_INSTALL_PREFIX0/sbin/xcatd  ]; then
           if [ -n "$INUCLIENTS" ] && [ $INUCLIENTS -eq 1 ]; then
             #Do nothing in not running system
             echo "Do not restartxcatd in not running system"
           else
             XCATROOT=/opt/xcat /opt/xcat/sbin/restartxcatd -r
           fi     
         fi
      fi
      exit 0
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
