#!/bin/sh
#<copyright>
# ----------------------------------------------------------
# Sun Proprietary/Confidential Code
# Copyright 2001, Sun Microsystems, Inc. All rights reserved.
# ----------------------------------------------------------
#</copyright>

if test "$1" = "-h"
then
   cat /opt/SUNWstade/System/ras_install.help
   exit 1;

fi


found=N
while test "$found" = "N" ; do
  perl1=""
  found=N
  remove_db=0

  # MOVE EXISTING CORE
  if test -f core ;then
    mv core core.storade
  fi

  for p in "/usr/bin/storade_perl" "/usr/bin/perl" "/usr/perl5/5.6.1/bin/perl" "/usr/local/bin/perl" "/bin/perl"  "/opt/bin/perl" 
  do
      if test -x $p; then
        perl=$p
        v=`$p -e '$x=$]; print $x*1000'`
        echo "Trying perl $perl ($v)"
  
        if test "0$v" -ge "05005" ; then
             rm -f /opt/SUNWstade/bin/perl
             ln -s $perl /opt/SUNWstade/bin/perl
             $perl /opt/SUNWstade/sbin/try_libraries
             if test "$?" = "0"; then
               echo "            -> This version works"
               perl1=$perl
               break
             else if test "`cat /tmp/ras_bad_modules.info`" = "Storable"; then
               remove_db=1
             fi
             fi
        fi
      fi
  done
  
  # REMOVE NEW CORES, PUT EXISTING CORE BACK
  if test -f core; then
    rm core
  fi
  if test -f core.storade; then
    mv core.storade core
  fi
  
  if test "0$perl1" != "0"; then
     found=Y
  fi
  if test $found = "N"; then
    if test "$remove_db" = "1"; then
       echo 
       echo " ******************* WARNING ***********************************"
       echo " A good version of perl was found but is not binary compatible "
       echo " with the current databases used by the agent."
       echo " The files /opt/SUNWstade/DATA/*.db will now be removed."
       echo " Warning: This will reset the agent and erase the current alarms."
       echo " ***************************************************************"
       echo 
       rm /opt/SUNWstade/DATA/*.db
    else
      echo " ******************* ERROR *************************************"
      echo " No supported version of perl could be found. "
      echo " The following perl module(s) failed: `/usr/bin/cat /tmp/ras_bad_modules.info` "
      echo " ******************* HELP **************************************"
      echo " The following URL can assist with getting a supported version. "
      echo " http://www.perl.com/pub/language/info/software.html"
      echo " Perl Solaris packages can be found at http://www.sunfreeware.com."
      echo
      echo " If you have a supported version of perl in a different directory"
      echo " link it to /usr/bin/storade_perl and run this command again:"
      echo " > ln -s <good_perl_path> /usr/bin/storade_perl "
      echo
      echo " You can also try removing the database files"
      echo " ie: rm /opt/SUNWstade/DATA/*.db"
      echo " and try rerunning ras_install"
      echo " Warning: This will reset the agent and erase the current alarms." 
      echo " ***************************************************************"
      exit 1;
    fi
  fi
done

perl=$perl1

echo 
echo "*** USING PERL: $perl ***"
echo 

rm -f /opt/SUNWstade/bin/perl /opt/SUNWstadh/bin/perl

ln -s $perl /opt/SUNWstade/bin/perl >/dev/null 2>&1
ln -s $perl /opt/SUNWstadh/bin/perl >/dev/null 2>&1

pkginfo SUNWstadh > /dev/null 2>&1

if [ $? -eq 0 ]
then
  cd /opt/SUNWstadh/dhbin
  $perl /opt/SUNWstadh/dhbin/ras_install.exe $*

elif test -d "/opt/SUNWstade/svebin"
then
  cd /opt/SUNWstade/svebin
  $perl /opt/SUNWstade/svebin/ras_install.exe $*

else
  $perl /opt/SUNWstade/bin/fix_perl_path $perl
  cd /opt/SUNWstade/bin
  $perl /opt/SUNWstade/bin/ras_install.exe $*
fi
