#!/bin/sh
 
# $Id$

# Copyright 2008 Free Software Foundation.
#
# DO NOT RUN THIS SCRIPT UNTIL YOU UNDERSTAND IT!!!  IT WILL REMOVE
# FILES ON YOUR SYSTEM.
#
# Read the script thoroughly before running it; it will *remove*
# /usr/local/gnuradio and repopulate it.
#
# This script provides a way to build GNU Radio modules individually,
# and both serves as an example of using the component build system
# and provides a way to test the build system.  When run, it will
# build and install each GNU Radio module in turn, using the
# just-installed modules as prerequisites.  It places the output of
# each build in a separate file BUILD.NNN.options, so that one can do
# 'tail -f BUILD.*' to see which components were successfully built.

# This script is intended to be broadly portable; be careful when
# modifying not to cause problems on systems that place dependencies
# in other than /usr
#
# Besides GNU Radio dependencies, this program requires sudo, with a
# timer long enough to build each module (or no password requirement).
#

set -x

# Do not use /opt, because many systems do not have /opt and that
# risks running out of space in /.  /usr/local/gnuradio is believed to
# be reasonable on all of *BSD and GNU/Linux.  Probably this needs
# OS-specific overrides.
PREFIX=/usr/local/gnuradio

echo "ABOUT TO COMPLETELY REMOVE $PREFIX in 10 SECONDS!"
sleep 10

echo -n "README.components START "; date

# This file provides an example of how to build GNU Radio under pkgsrc.

# Avoid using rm -rf with $PREFIX, which could be /.  Make a backup of
# the old prefix.
sudo rm -rf $PREFIX.old
if [ -d $PREFIX ]; then
    sudo mv $PREFIX $PREFIX.old
fi
rm -rf BUILD.*

# Bootstrap just once, rather than once per module.
./bootstrap

# Determine where prereqs come from.
export LDFLAGS=
export CPPFLAGS=
export PKG_CONFIG_PATH=
if [ -d /usr/pkg ]; then
    # pkgsrc
    LDFLAGS="$LDFLAGS -L/usr/pkg/lib -R/usr/pkg/lib"
    CPPFLAGS="$CPPFLAGS -I/usr/pkg/include"

    # pkg-config is from pkgsrc, so already knows about /usr/pkg/lib/pkgconfig
    PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PREFIX/lib/pkgconfig"
fi

echo LDFLAGS ,$LDFLAGS,
echo CPPFLAGS ,$CPPFLAGS,
echo PKG_CONFIG_PATH ,$PKG_CONFIG_PATH,

# Determine number of cpus and thus how many jobs to run.
ncpus=1
case x`uname` in
    xNetBSD)
	ncpus=`sysctl hw.ncpu|awk '{print $3}'`
	;;
esac
jflag=-j`expr $ncpus \* 2`

# These are currently ignored.
CONF_DOC_ARGS="
--enable-doxygen
--enable-dot
--enable-latex-docs
"
CONF_DISABLE_ALL="--disable-all-components"

# We use % instead of ' ' to be able to iterate with /bin/sh's for.
# This variable should list all possible arguments, in tsorted order.
CONF_ENABLE_ARGS="
--enable-omnithread
--with-omnithread%--enable-gnuradio-core
--with-omnithread%--enable-pmt
--with-omnithread%--with-pmt%--enable-mblock
--with-omnithread%--with-pmt%--with-mblock%--enable-usrp
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--with-usrp%--enable-gr-usrp
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-msdd6000
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-audio-alsa
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-audio-jack
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-audio-oss
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-audio-osx
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-audio-portaudio
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-audio-windows
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-atsc
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-comedi
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-cvsd-vocoder
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--with-usrp%--enable-gr-gpio
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-gsm-fr-vocoder
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-pager
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--with-usrp%--enable-gr-radar-mono
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-radio-astronomy
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-trellis
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-video-sdl
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gr-wxgui
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--with-usrp%--enable-gr-sounder
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--with-usrp%--with-gr-usrp%--with-gr-wxgui%--enable-gr-utils
--with-omnithread%--with-gnuradio-core%--with-pmt%--with-mblock%--enable-gnuradio-examples
"

seq=0
for arg in $CONF_ENABLE_ARGS; do

    # Convert sequence numbers and arguments to usable values.
    seqprint=`printf "%03d" $seq`
    argspace=`echo $arg | sed -e 's/%/ /g'`

    echo "BUILDING WITH $argspace"

    (
    # configure with just one module
    ./configure --prefix=$PREFIX $CONF_DISABLE_ALL $argspace &&

    # remove all prior objects
    make clean &&

    # build
    make $jflag &&

    # install
    sudo make install &&

    echo "SUCCEEDED $argspace"

    ) > BUILD.$seqprint.$arg 2>&1

    seq=`expr $seq + 1`
done

echo -n "README.components FINISH "; date