#!/bin/sh
# cd Autorun for System Mangement Software
#

# detect system configuration and dynamically write HTML
#
set -x

ROOTdir=`pwd` 
ARCH="ia64"
if [ $HOSTTYPE != $ARCH ]; then
  ARCH="i386"
fi;
RVERSION=`uname -r`
ULANG=`echo $LANG | sed "s/\..*$//;s/_.*$//;" `

RHRELEASE=`rpm -q redhat-release`
if [ $? = 0 ]; then
    NVERSION="RedHat"
else 
  NVERSION=`uname -s`
  if [ -f "/etc/SuSE-release" ]; then
    NVERSION="SUSE"
  fi;
fi;

if [ "$UID" -eq "0" ]; then
  ADMIN=true
else
  ADMIN=false
fi;

#determine browser
BROWSER=`which firefox`
if [ $? != 0 ]; then
  BROWSER=`which mozilla`
  if [ $? != 0 ]; then
     BROWSER=`which htmlview`
     if [ $? != 0 ]; then
        BROWSER=`which konqueror`
        if [ $? != 0 ]; then
           BROWSER=`which opera`
           if [ $? != 0 ]; then
             BROWSER="unknown"
           fi;
	fi;
     fi;
  fi;
fi;

FREESPACE=`df /usr/local -m --total |grep total|sed "s/[a-Z]* *\([0-9]*\) *\([0-9]*\) *\([0-9]*\).*/\3/"`
MEMAVAIL=`free -m | grep "Mem:"|sed "s/[a-Z]*: *\([0-9]*\).*/\1/"`

#detect installed applications
CACHERIVER=`rpm -qa | grep CacheRiver`
if [ $? != 0 ]; then 
  CACHERIVER="MISSING"
fi;
XERCES=`rpm -qa | grep XERCES`
if [ $? != 0 ]; then 
   XERCES="MISSING"
fi;
ASC=`rpm -qa | grep ASC`
if [ $? != 0 ]; then 
   ASC="MISSING"
fi;

#check everything, exit on failure otherwise install and launch ASC
if [ $ADMIN == false ]; then
   echo "You must be root to install"
   exit
else
   if [ $FREESPACE <= 1000 ]; then
	echo "insufficient space, it is recommended to have 1G available"
        exit
   else
      if [ $MEMAVAIL <= 250 ]; then
	echo "insufficient Memory Available, it is recommend to have > 250 MB"
	exit
      fi;
   fi;   
fi;


#install RPMs
if [ $NVERSION == "RedHat" ]; then
   if [ $ARCH == "i386" ]; then

      #install XERCES 3.0.1
      if [ $XERCES != "MISSING" ]; then
         rpm -Uhv XERCES-*.i386.rpm
      else
	 rpm -ihv XERCES-*.i386.rpm
      fi;

     #InStall Cache River 2.0
      if [ $ASC != "MISSING" ]; then
	 rpm -Uhv CacheRiver-*.i386.rpm
      else
	 rpm -ihv CacheRiver-*.i386.rpm
      fi;

     #Install ASC
      if [ $ASC != "MISSING" ]; then
	 rpm -Uhv ASC-*.i386.rpm
      else
	 rpm -ihv ASC-*.i386.rpm
      fi;

   else
     echo "$ARCH not supported"
     exit
   fi;
else
   echo "%NVERSION not supported"
   exit
fi;



#Launch Application
cd /
defaddr=http://127.0.0.1:2000
`$BROWSER $deffile`
