#!/bin/sh

OSNAME=`uname`

for fn in build-smart-snmpd build-flags
do
    if [ -f "./${fn}.${OSNAME}" ]
    then
        . ./${fn}.${OSNAME}
    fi
done

pkgs="$@"
if [ -z "$pkgs" ]
then
    pkgs="snmp++ agent++ libconfuse log4cplus libstatgrab smart-snmpd"
fi

for pkg in ${pkgs}
do
    if [ "$pkg" = "snmp++" ]
    then
            cd snmp++
            ./configure --prefix=/sysmgmt/opt/smart-snmpd-gcc --with-libssl-prefix=/sysmgmt/opt/openssl --with-log-profile=quiet --enable-snmpv3 && gmake -j2 && gmake install
            rc=$?
            cd ..
    elif [ "$pkg" = "agent++" ]
    then
            cd agent++
            ./configure --prefix=/sysmgmt/opt/smart-snmpd-gcc --enable-threadpool && gmake -j2 && gmake install
            rc=$?
            cd ..
    elif [ "$pkg" = "log4cplus" ]
    then
            cd log4cplus
            ./configure --prefix=/sysmgmt/opt/smart-snmpd-gcc --without-iconv && gmake -j2 && gmake install
            rc=$?
            cd ..
    elif [ "$pkg" = "libstatgrab" ]
    then
            cd libstatgrab
            ./configure --prefix=/sysmgmt/opt/smart-snmpd-gcc && gmake -j2 && gmake install
            rc=$?
            cd ..
    elif [ "$pkg" = "libconfuse" ]
    then
            cd confuse
            ./configure --prefix=/sysmgmt/opt/smart-snmpd-gcc --disable-nls && gmake -j2 && gmake install
            rc=$?
            cd ..
    elif [ "$pkg" = "smart-snmpd" ]
    then
            cd smart-snmpd
            ./configure --prefix=/sysmgmt/opt/smart-snmpd-gcc --with-bundled-libjson --without-su-cmd && gmake -j2 && gmake install
            rc=$?
            cd ..
    else
            echo "Unknown pkg to build: $pkg" >&2
            rc=255
    fi

    if [ $rc -ne 0 ]
    then
        exit $rc
    fi
done
