#!/bin/bash

#########################################################
# Build script to build BarnOwl for the locker.
die() {
    echo "$@" 2>&1;
    if [ -n "$TMPDIR" ]; then
        if [ -n "$DRYRUN" ]; then
            echo "Dropping to a shell to investigate...";
            $SHELL
        fi
    fi
    exit 1;
}

usage () {
    cat >&2 <<EOF
Usage: $0 [-n] [-o OUTPUT-TGZ] SOURCE-TARBALL
 -n is a dry-run, and drops to a shell in the build directory
 -o does the install into a temporary directory and tars it into the
    specified tarball instead.

SOURCE-TARBALL is a source tarball, created by do-release.
EOF
    exit 2;
}

exittrap() { :; }
for sig in 1 2 13 15; do trap "exit $(($sig + 128))" $sig; done
trap 'exittrap' EXIT

SRC_TGZ=
OUT_TGZ=
DRYRUN=

set -- `getopt no: "$@"`
[ $? -eq 0 ] || usage

for opt
do
    case "$opt" in
        -n)
            DRYRUN=1; shift ;;
        -o)
            OUT_TGZ="$2"; shift 2;;
        --)
            shift; break ;;
    esac
done

SRC_TGZ="$1"

test -z "$SRC_TGZ"  && usage


ATHENA_SYS="${ATHENA_SYS:-$(machtype -S)}"

if [ "$(uname)" = "SunOS" ]; then
    MAKE=gmake
    TAR=gtar
else
    MAKE=make
    TAR=tar
fi

attach barnowl
aklog

TMPDIR=$(mktemp -d /tmp/barnowl-build-XXXXXX) || die "Unable to mktemp"

exittrap() { rm -rf "$TMPDIR"; }

$TAR -C "$TMPDIR" -xzf "$SRC_TGZ" || die "Unable to untar"

(
    cd "$TMPDIR"/* || die "Unable to cd to $TMPDIR"
    VERS=$(perl -ne 'print $1 if m{^AC_INIT\(\[[^\]]+\],\s*\[([^\]]+)\]}' configure.ac)
    test -z "$VERS" && die "Unable to detect barnowl version."

    echo "Building BarnOwl version $VERS"

    opt_rpath="-Wl,-R"
    [ $(uname) = "SunOS" ] && opt_rpath="-R"

    BARNOWL="/afs/sipb.mit.edu/project/barnowl/arch/$ATHENA_SYS"
    export PKG_CONFIG_PATH="$BARNOWL/lib/pkgconfig"
    eval $("$BARNOWL/bin/barnowl-perl-config")

    SUFFIX=
    case $ATHENA_SYS in
        *_deb50)
            # Both Debian Lenny and Ubuntu Karmic use the _deb50
            # sysname, but they have different zephyr versions (soname
            # 3 and 4, respectively). So for that sysname, we include
            # the zephyr soname into the build name, and the wrapper
            # script picks the right one.
            if /sbin/ldconfig -p | grep -qF "libzephyr.so.4"; then
                SUFFIX=.zephyr4
            else
                SUFFIX=.zephyr3
            fi
            ;;
    esac

    CPPFLAGS="-I$BARNOWL/include -I/usr/athena/include" \
        LDFLAGS="-L$BARNOWL/lib -L/usr/athena/lib $opt_rpath$BARNOWL/lib" \
        ./configure --exec-prefix="/mit/barnowl/arch/$ATHENA_SYS" \
        --prefix="/mit/barnowl/builds/barnowl-$VERS" --mandir=/mit/barnowl/man \
        --program-suffix="-$VERS$SUFFIX" \
        --with-zephyr --without-stack-protector \
        || die "configure failed"

    $MAKE clean  || die "make clean failed"

    CPUS=$(athinfo localhost cpuspeed | grep -c MHz)
    if [ "$CPUS" = 0 ]; then
        CPUS=1
    fi

    $MAKE -j$CPUS all || die "make failed"

    $MAKE check || die "Unit tests failed"

    if [ -n "$DRYRUN" ]; then
        echo "Build completed; Dropping to a shell..."
        $SHELL
    else
        if [ -n "$OUT_TGZ" ]; then
            mkdir tgz
            $MAKE DESTDIR=tgz install || die "Install failed"
        else
            $MAKE install || die "Install failed"
        fi
    fi
)

if [ "$?" -ne 0 ]; then
    exit "$?"
fi

if [ -n "$OUT_TGZ" ]; then
    $TAR -C "$TMPDIR/"*/tgz -czvf "$OUT_TGZ" . || die "Make tarball failed"
fi

rm -rf "$TMPDIR"
exittrap() { :; }
