#!/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

if [ ! -d /usr/local ]
then
      /usr/bin/mkdir /usr/local > /dev/null
fi
if [ ! -d /usr/local/lib ]
then
      /usr/bin/mkdir /usr/local/lib > /dev/null
fi



if [ ! -f /usr/local/lib/libgcc_s.s0.1 ]
then
      /usr/bin/cp /opt/SUNWstade/libxml2/lib/libgcc_s.so.1 /usr/local/lib/libgcc_s.so.1 > /dev/null
fi

if [ ! -f /usr/local/lib/libiconv.so.2 ]
then
      /usr/bin/cp /opt/SUNWstade/libxml2/lib/libiconv.so.2 /usr/local/lib/libiconv.so.2 > /dev/null
fi

if [ ! -f /usr/local/lib/libxml2.so.2 ]
then
      /usr/bin/cp /opt/SUNWstade/libxml2/lib/libxml2.so.2  /usr/local/lib/libxml2.so.2  > /dev/null
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
             /usr/bin/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
    /usr/bin/rm core
  fi
  if test -f core.storade; then
    /usr/bin/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 
       /usr/bin/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 

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

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


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