#!/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 ""
    exit 1;
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_execd analysisd logcollector remoted client-agent addagent util rootcheck syscheckd monitord"
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" ]; 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    

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

    if [ "X$OS" = "XSunOS" ]; then
        echo "EEXTRA=-lsocket -lnsl -lresolv -DSOLARIS -DHIGHFIRST">>Config.OS
    fi

    if [ "X$OS" = "XHP-UX" ]; then
        echo "EEXTRA=-DHPUX -D_XOPEN_SOURCE_EXTENDED" >> Config.OS
    fi    
        
    if [ "X$OS" = "XFreeBSD" ]; then
        echo "TEXTRA=-pthread" >> Config.OS
    else
        echo "TEXTRA=-lpthread" >> Config.OS
    fi    

    if [ "X$OS" = "XDarwin" ]; then
        echo "EEXTRA=-DDarwin" >> Config.OS
    fi    
fi


# Cleaning
if [ "X${ARGV}" = "Xclean" ]; then
    #echo "" > ./Config.OS
    echo " "
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}" = "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
ls ../bin >/dev/null 2>&1
if [ $? != 0 ]; then
    mkdir -p ../bin
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        
    else
        echo ""
        echo ""
        echo " *** Making $i *** "
        echo ""
        make
        if [ $? != 0 ]; then
            echo ""
            echo "Error Making $i"
            exit 1;
        fi
    fi    
    cd ../
done

exit 0;
# EOF #
