#!/bin/sh

# This script is used to install the localized packages of the Sun ONE Instant Messaging
# components
# The usage is 
# install.sh [locale/messenger] [locale/identity] [locale/portal]
# with no arguments it will install the localized packages for all the components

SED=/bin/sed
GREP=/bin/grep
CUT=/bin/cut
LS=/bin/ls
ECHO=/bin/echo
RM=/bin/rm

OS="`uname -s`"
if [ "$OS" = "SunOS" ]; then
   PKGADD=/usr/sbin/pkgadd
   PKGRM=/usr/sbin/pkgrm
   PKGPARAM=/usr/bin/pkgparam
elif [ "$OS" = "Linux" ]; then
   RPM=/bin/rpm
else
   echo "$OS not supported, sorry."
   exit 1
fi

install_localized_packages(){

   component=$1 
   localePkgDir="${localeDir}/${component}"
   list_of_packages=`${LS} $localePkgDir`

   if [ "$OS" = "SunOS" ]; then

     ${ECHO} "action=nocheck" >> /tmp/pkgaddAdmin.$$ 
     ${ECHO} "conflict=nocheck" >> /tmp/pkgaddAdmin.$$ 
     ${ECHO} "action=nocheck" >> /tmp/pkgrmAdmin.$$ 
     ${ECHO} "conflict=nocheck" >> /tmp/pkgrmAdmin.$$ 

     if [ "$component" = "messenger" ]; then
           corePackage=SUNWiimc
     elif [ "$component" = "identity" ]; then
           corePackage=SUNWiimid
     elif [ "$component" = "portal" ]; then
           corePackage=SUNWiimps 
     else 
           ${ECHO} " ${component is not a valid component"
     fi

     BASEDIR=`${PKGPARAM} -v ${corePackage} | ${GREP} BASEDIR= | ${CUT} -f2 -d= | ${SED} -e s/\'//g`
     IM_HOME=${BASEDIR}/SUNWiim

   elif [ "$OS" =  "Linux" ]; then

     if [ "$component" = "messenger" ]; then
          corePackage=soimc
     else 
          ${ECHO} " ${component is not a valid component"
     fi
     BASEDIR=`rpm -q --queryformat '%{INSTALLPREFIX}' ${corePackage}`
     IM_HOME=${BASEDIR}/soim

   else 
      ${ECHO} "UnSupported OS + ${OS}"
   fi

   for pkg in $list_of_packages
   do
       ${ECHO} "Installing ${pkg} \n"
       if [ "$OS" = "SunOS" ]; then
         ${PKGRM}  -n  -a /tmp/pkgrmAdmin.$$ ${pkg} > /dev/null 2>&1
         ${PKGADD} -d ./$localePkgDir  -a /tmp/pkgaddAdmin.$$ ${pkg} > /dev/null 2>&1
       else
         ${RPM} -e ${pkg}
         ${RPM} -i ./$localePkgDir ${pkg}
       fi

       if [ $? -ne 0 ]; then
            ${ECHO} "Failed to install the localized package $pkg" 
       else 
           ${ECHO} ${component}:${pkg} >> ${IM_HOME}/${localizedPkgListFile} 
       fi

   done

   if [ "$OS" = "SunOS" ]; then
     ${RM} /tmp/pkgaddAdmin.$$
     ${RM} /tmp/pkgrmAdmin.$$
   fi
}

install_localized_pkgs_all_comps(){

     if [ -d ${localeDir}/${messengerComp} ]; then
            ${ECHO} "Installing localized packages of Sun ONE Instant Messenger component"
            install_localized_packages  ${messengerComp}
     else
            ${ECHO} "${localeDir}/${messengerComp} dir does not exist"
            ${ECHO} "The localized packages of SUNWiimc and SUNWiimd packages will not be installed"
     fi

     if [ -d ${localeDir}/${identityComp} ]; then
            ${ECHO} "Installing localized packages of Sun ONE Identity Server Instant Messaging Service Definition component"
            install_localized_packages ${identityComp}
     else
            ${ECHO} "${localeDir}/${identityComp} dir does not exist"
            ${ECHO} "The localized packages of SUNWiimid package will not be installed"
     fi

     if [ -d ${localeDir}/${portalComp} ]; then
            ${ECHO} "Installing localized packages of Sun ONE Portal Server IM Channel and Archive component"
            install_localized_packages  ${portalComp}
     else
            ${ECHO} "${localeDir}/${portalComp} dir does not exist"
            ${ECHO} "The localized packages of SUNWiimps package will not be installed"
     fi

}


installDir=`dirname $0`
localeDir="$installDir/locale"
messengerComp="messenger"
identityComp="identity"
portalComp="portal"
localizedPkgListFile="l10n_pkgs"

if [ $# -ne 1 ]; then
      install_localized_pkgs_all_comps  
      
else
     if [ -d $localeDir/$1 ]; then
          install_localized_packages $1
          ${ECHO} "All the localized packages have been installed successfully"
     else
          ${ECHO} "${localeDir}/$1 does not exist \n"
          exit 1
     fi
fi



