#!/bin/sh
# $RootCheck: install ,v 1.0 2003/10/20, Gabriel$
# Installation script for RootCheck
# Read INSTALL for details. 

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

ME=`whoami`
PLAT=`uname`
FULLPLAT=`uname -a`
VERSION=`cat ./VERSION`

#####################################################################
# Procedures
checkr00t() {

  if [ ! "$ME" = "root" ]; then

     echo "Oops , you need to be root to install these perl modules."
     echo "Run this script as root or ask your sys admin to install it."
     echo ""
     exit 1
  fi
}

setVars() {

 case $PLAT in

     Linux)
       PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
       CFLAGS=""
     ;;
     
     SunOS)
       PATH=/usr/ucb:/usr/xpg4/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
       CFLAGS="-lnsl -lsocket"
     ;;
     
     *)
       PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
       CFLAGS=""
 esac

}

checkDependencies() {

  echo ""
  echo " ----- Cheking Dependencies:"
  echo ""
  echo -n "Checking for perl ..."
  which perl > /dev/null 2>&1
  if [ $? -ne "0" ]; then
    echo "not found in PATH"
    echo "You need to install Perl before using this programm"
    exit 1
  fi
  PERL_INFO=`perl -e "print $]"`
  echo " found: perl $PERL_INFO"

  echo -n "Checking for gcc ..."
  which gcc > /dev/null 2>&1
  if [ $? -ne "0" ]; then
    echo "not found in PATH"
    echo -n "Lets try for cc ..."
    which cc > /dev/null 2>&1
    if [ $? -ne "0" ]; then
       echo "not found in PATH"
       echo "You will need a compiler (like gcc) to be able to use RootCheck utilities"
       exit 1
    fi
      CC=cc  
  else 
      CC=gcc  
  fi
  echo " found: $CC"
  
  echo ""
  echo " -- Note: You will need perl version >= 5.6.0 (5.006)"
}

createMakefile() {

 touch Makefile.$PLAT && >Makefile.$PLAT
 IFS="
"
 for LINE in `cat Makefile.sample` ;
 do
                                                                                
    echo $LINE | grep '^CC' > /dev/null 2>&1
    RET=$?
    echo $LINE | grep '^CFLAGS'  > /dev/null 2>&1
    RRET=$?
    if [ $RET -eq "0" ]; then
       LINE="    CC = $CC "
    elif [ $RRET -eq "0" ]; then
       LINE="    CFLAGS = $CFLAGS "
    fi
                                                                                
    echo $LINE >> Makefile.$PLAT
 done
 cp Makefile.$PLAT Makefile
 IFS=" "

}

#####################################################################
# Main
#####################################################################

clear
case "$1" in
'all')
setVars
checkDependencies
installPerlPkg
createMakefile
make all
exit 0 
;;
*)
echo "
RootCheck v$VERSION Install script - http://www.ossec.net/rootcheck/

 - System: $FULLPLAT 

   -- Hit any key to continue installation (or Ctrl-C to abort) --
"

read POFT

####
# specific to platforms
setVars

OK=1;

### 
# check for perl and compiler (gcc / cc)
checkDependencies

echo ""
echo ""
echo " ----- Checking Perl Modules:"
echo ""
perl ./mods/installation/check $PKG
echo " "

#################################################
# Build Makefile based on current platform

echo "
   RootCheck uses a few tools written in C to help searching for
abnormal system activity. Thus we must compile this code for your platform,
so we can fully check your system.
"
echo -n "Do you want to compile theses tools ? [y/n] "
                                                                                
read POFT
                                                                                
                                                                                
case $POFT in
                                                                                
     [Nn])
         echo "Skipping ..."
	 OK=0
         break
     ;;
     *)
         echo ""
         echo -n "Generating Makefile for $PLAT ... "
         createMakefile
         echo " ok"
         echo "Compiling ... "
         make all 
         if [ $? -ne "0" ]; then
            echo "Oops, compilation had some problems.."
            exit 1 
         fi
         echo ""
         echo "Compilation sucessfull. Ready to go."

 esac
if [ ${OK} -eq 1 ] ;then
 	echo ""
 	echo "---------------------------------------------------------"
 	echo " That's it! If everything went ok, you should be ready"
 	echo " to run RootCheck. If you any doubts about installation, "
 	echo " please refer to INSTALL file."
 	echo " You can also find additional information at :"
 	echo " http://www.ossec.net/rootcheck/"
 	echo " Improves, patches, comments are very welcome. "
 	echo "---------------------------------------------------------"
	exit 0
else
	echo "---------------------------------------------------------"
	echo " You missed to install one or more of the necessary      "
	echo " packages. Make sure that you know what you are doing.   "
	echo " You can also find additional information at :"
        echo " http://www.ossec.net/rootcheck/"
        echo " Improves, patches, comments are very welcome. "
        echo "---------------------------------------------------------"
fi
;;
esac
exit 0
