#!/bin/sh

# Make clean?
ARGV=$1

# Checking argument
if [ "X${ARGV}" = "X" ]; then
    echo ""
    echo "No argument. Available options are:"
    echo "  $0 clean"
    echo "  $0 all"
    echo "  $0 build"
    echo "  $0 libs"
    echo "  $0 rootcheck"
    echo ""
    exit 1;
fi

# Increasing maximum number of agents
if [ "X$1" = "Xsetmaxagents" ]; then
    echo -n "Specify maximum number of agents: "; 
    read AGMAX
    echo "HEXTRA=-DMAX_AGENTS=$AGMAX" >> ./Config.OS
    echo "Maximum number of agents set to $AGMAX."
    exit 0;
fi


ZLIBV="external/zlib-1.2.3/"

# CPU information
#CPU=`uname -p`
MACH=`uname -m`
OS=`uname`
VERSION=`cat ./VERSION`

MSG=""
# Libraries. They need to be compiled before.
LIBS="os_xml os_regex os_net os_crypto"
# Shares sources
SOURCES="shared config"
# Binaries
BINARIES="os_maild os_dbd os_csyslogd agentlessd os_execd analysisd logcollector remoted client-agent addagent util rootcheck syscheckd monitord"
ROOTCHECKBIN="rootcheck"
DIRECTORIES=""      # Directories to make


# Setting SunOS path
if [ "X$OS" = "XSunOS" ]; then
    PATH=$PATH:/usr/ccs/bin:/usr/xpg4/bin:/opt/csw/gcc3/bin:/opt/csw/bin:/usr/sfw/bin
    export  PATH
fi
            

# Cleaning Config.OS
if [ "X${ARGV}" = "Xall" -o "X${ARGV}" = "Xrootcheck" -o "X${ARGV}" = "Xlibs" ]; then
    ls ./Config.OS >/dev/null 2>&1
    if [ ! $? = 0 ]; then
        echo "" > Config.OS
    fi

    # Checking for OpenSSLconf.h
    ls /usr/include/openssl/opensslconf.h > /dev/null 2>&1
    if [ $? = 0 ]; then
        echo "DEXTRA=-DUSE_OPENSSL" >> Config.OS
    fi    

    # Checking for inotify
    if [ "X$OS" = "XLinux" ]; then
        ls /usr/include/sys/inotify.h > /dev/null 2>&1
        if [ $? = 0 ]; then
            echo "EEXTRA=-DUSEINOTIFY" >> Config.OS
        fi    

    fi    

    if [ "X$OS" = "XAIX" ]; then
        echo "EEXTRA=-DAIX -DHIGHFIRST" >> Config.OS
        PATH=$PATH:/usr/vac/bin
        export  PATH

    elif [ "X$OS" = "XSunOS" ]; then
        # Set CPU optimization for Sun UltraSparc
        # by Eric Straavaldsen
        # Disabled by default. To enable it, just uncomment the following
        # lines and comment the EEXTRA below.
        #PROC=`isainfo -n`
        #if [ $PROC = sparcv9 ]; then
        #    echo "EEXTRA=-lsocket -lnsl -lresolv -DSOLARIS -DHIGHFIRST -O3 -mcpu=ultrasparc">>Config.OS
        #fi
        echo "EEXTRA=-DSOLARIS -DHIGHFIRST">>Config.OS
        echo "IEXTRA=-lsocket -lnsl -lresolv" >> Config.OS

    elif [ "X$OS" = "XHP-UX" ]; then
        echo "EEXTRA=-DHPUX -D_XOPEN_SOURCE_EXTENDED -DHIGHFIRST -D_REENTRANT" >> Config.OS

    elif [ "X$OS" = "XDarwin" ]; then
        echo "EEXTRA=-DDarwin -DHIGHFIRST" >> Config.OS
    
    else
        
        echo 'int main() { short one = 1; char *cp = (char*)&one; if ( *cp == 0 ) return(1); else return(0); }' > isbigendian.c
        gcc -o isbigendian isbigendian.c
        ./isbigendian
        if [ $? = 1 ]; then
            echo "INFO: Big endian set."
            echo "GEXTRA=-DHIGHFIRST" >> Config.OS
        else
            echo "INFO: Little endian set."
        fi
    fi
        

    if [ "X$OS" = "XFreeBSD" -o "X$OS" = "XDragonFly" ]; then
        echo "TEXTRA=-pthread" >> Config.OS
    else
        echo "TEXTRA=-lpthread" >> Config.OS
    fi    
fi


# Cleaning
if [ "X${ARGV}" = "Xclean" ]; then
    echo "" > ./Config.OS
fi
   
    
# Getting values for each action
if [ "X${ARGV}" = "Xall" ]; then
    DIRECTORIES="${LIBS} ${SOURCES} ${BINARIES}" 
    DOZLIB="x"
elif [ "X${ARGV}" = "Xlibs" ]; then
    DIRECTORIES="${LIBS} ${SOURCES}"
    DOZLIB="x"
elif [ "X${ARGV}" = "Xrootcheck" ]; then
    DIRECTORIES="os_xml os_regex os_net ${SOURCES} ${ROOTCHECKBIN}"    
elif [ "X${ARGV}" = "Xbuild" ]; then
    DIRECTORIES="${BINARIES}"   # Only binaries need to be built
else
    DIRECTORIES="${LIBS} ${SOURCES} ${BINARIES}"
fi


if [ "X${DOZLIB}" = "Xx" ]; then
    # Build zlib here
    echo ""
    echo " *** Making zlib (by Jean-loup Gailly and Mark Adler)  *** "
    cd ${ZLIBV}; make; make ossec;
    cd ../../
    echo ""
fi    


if [ "X${ARGV}" = "Xclean" ]; then
    # Cleaning zlib
    cd ${ZLIBV}; make clean;
    cd ../../
fi    


# Checking if the bin directory is present
if [ ! "X${ARGV}" = "Xrootcheck" ]; then
  ls ../bin >/dev/null 2>&1
  if [ $? != 0 ]; then
      mkdir -p ../bin
  fi
else
  echo "Compiling Rootcheck..."
  echo ""      
fi  


                    
# Making each directory
for i in ${DIRECTORIES}; do
    cd $i
    if [ $? != 0 ]; then
        echo ""
        echo "Error acessing directory $i"
        exit 1;
    fi    
    if [ "X${ARGV}" = "Xclean" ]; then
        echo "Entering $i"
        make clean
    elif [ "X${ARGV}" = "Xbuild" ]; then
        make build
        if [ $? != 0 ]; then
            echo ""
            echo "Error Making the binaries"
            exit 1;
        fi        
    elif [ "X${ARGV}" = "Xrootcheck" ]; then    
        echo "Making $i"
        if [ $i = "rootcheck" ]; then
            make binary
        else
            make
        fi        
        if [ $? != 0 ]; then
            echo ""
            echo "Error Making $i"
            exit 1;
        fi    
    else
        echo ""
        echo ""
        echo " *** Making $i *** "
        echo ""
        make
        if [ $? != 0 ]; then
            echo ""
            echo "Error Making $i"
            exit 1;
        fi
    fi    
    cd ../
done


if [ "X${ARGV}" = "Xrootcheck" ]; then
    cp -pr ./rootcheck/ossec-rootcheck ../
    cp -pr ./rootcheck/db ../
    cp -pr ./rootcheck/rootcheck.conf ../
    echo ""
    echo "Rootcheck compilation completed."
    echo "Just run './ossec-rootcheck' to execute it."
    echo ""
fi    


exit 0;
# EOF #
