#!/bin/bash
#
# download and build opentracker

# read build environment
for i in build/conf.d/*; do source "$i" || exit 1; done

# check for binary
if [ -s "$OTBIN" -a -x "$OTBIN" ]; then
  echo "Opentracker binary exists, skipping build."

else
  # download opentracker
  echo "Cloning $OTNAME source ..."
  rm -rf "$OTSRCDIR"*
  cd "$SRC"
  sudo apt-get update
  apt-get source opentracker || exit 1
  rm -rf opentracker_*
  ln -sf "$(ls -d "$SRC/opentracker-"*)" "$OTSRCDIR"
  echo "Compiling $OTNAME ..."
  cd "$OTSRCDIR" || exit 1
  PREFIX=/usr STRIP=true make || exit 1
  [ -s "$OTBIN" -a -x "$OTBIN" ] || exit 1
fi

# install
echo "Installing $OTNAME ..."
mkdir -p "$OTINSTDIR"
install -m 755 "$OTBIN" "$OTINSTDIR" || exit 1
