#!/bin/sh 

source ../Config.sh

PP_PUBLIC_INCLUDES="include/ucd-snmp"
# note: include/ucd-snmp contains some files that are generated at run-time

pp_clean_local () {
    pp_clean_default
    # force aclocal
    rm -f aclocal.m4
}

pp_build_local () {
    # force configure
    if [ ! -f aclocal.m4 -o "$PP_FORCE_FULL_BUILD" = "1" ]; then
	echo "Running aclocal ..."
	aclocal
	rm -f Makefile
    fi

    # create real mib2c script
    if [ `uname -m` != "x86_64" ]; then
       PERL5_10=$(perl -e 'printf "%d\n", ($^V and $^V ge v5.10);')
       if [ "$PERL5_10" = "1" ]; then
	   SNMP_DIR="${FW_TOPDIR}/ucd-snmp/build_stuff/SNMP-perl-5.10"
       else
	   SNMP_DIR="${FW_TOPDIR}/ucd-snmp/build_stuff/SNMP"
       fi
       sed -e "s,#MIB2CDIR#,${FW_TOPDIR}/ucd-snmp/build_stuff/," \
           -e "s,#PERLSNMPDIR#,$SNMP_DIR," \
         < build_stuff/mib2c.in \
         > build_stuff/mib2c
    else
       sed -e "s,#MIB2CDIR#,${FW_TOPDIR}/ucd-snmp/build_stuff/," \
           -e "s,#PERLSNMPDIR#,," \
         < build_stuff/mib2c.in \
         > build_stuff/mib2c
    fi
    chmod 755 build_stuff/mib2c

    mib="LARA-MIB"
    modules="peppercon/LARA"
    pushd agent/mibgroup/peppercon
	rm -f LARA.c
	make LARA.c
	patch -p0 < LARA.patch
    popd

    # configure
    if [ ! -f Makefile ]; then
	echo "Running configure ..."
	./configure \
		--prefix=/ \
		--host="${HOST_SYSTEM}" \
		--target="${HOST_SYSTEM}" \
		--with-endianness=big \
		--disable-debugging \
		--enable-static \
		--disable-shared \
		--enable-mini-agent \
		--with-sys-contact=nobody@nowhere \
		--with-sys-location=Unknown \
		--with-logfile=/var/log/snmpd.log \
		--with-persistent-directory=/var/ucd-snmp \
		--with-mibdirs="/lib/snmp/mibs" \
		--with-mib-modules=$modules \
		--with-out-mib-modules="ucd_snmp agentx host smux \
					misc/ipfwacc ipfwchains/ipfwchains \
					ucd-snmp/diskio target tunnel \
					agentx/client agentx/master \
					examples/ucdDemoPublic" \
		--with-mibs="SNMPv2-MIB:$mib" \
		--without-openssl \
		--with-cflags="$FW_CFLAGS_3RDPARTY -ffunction-sections -fdata-sections -DSYSV -D_REENTRANT"
	sed -i -e "s,^\(sys_lib_search_path_spec=\).*,\1\"$DESTDIR/lib $DESTDIR/usr/lib\"," libtool
    fi

    # make / install
    echo "Running make ..."
    make fw_install
}

pp_do "$@"
