#!/usr/bin/env bash

set -e
if [ -z "$O3" ]; then
    echo O3 var must point to ovpn3 tree
    exit 1
fi
if [ -z "$TARGET" ]; then
    echo TARGET var must be defined
    exit 1
fi

[ -z "$DL" ] && DL=~/Downloads

# source vars
. $O3/core/vars/vars-${TARGET}
. $O3/core/deps/lib-versions

[ "$GCC_CMD" ] && export CC=$GCC_CMD
[ "$LD_CMD" ] && export LD=$LD_CMD
[ "$AR_CMD" ] && export AR=$AR_CMD
[ "$RANLIB_CMD" ] && export RANLIB=$RANLIB_CMD

case $PLATFORM in
android*)
  echo PLATFORM android
  host=arm
  target=arm
  ;;
ios*)
  echo PLATFORM ios
  host="x86_64-apple-darwin"
  target=arm
  ;;
*)
  host=""
  target=""
  ;;
esac

if [ "$target" ]; then
    targ_opt="--target=$target"
fi

if [ "$host" ]; then
    host_opt="--host=$host"
fi

if [ "$NO_WIPE" != "1" ]; then
    rm -rf $LZO_VERSION
    tar xfz $DL/$LZO_VERSION.tar.gz
fi

DIST=$(pwd)/lzo/lzo-$PLATFORM
rm -rf $DIST
mkdir -p $DIST
cd $LZO_VERSION
echo 'OPTIONS' $CC $LD $AR $RANLIB $host_opt $targ_opt
CFLAGS="$PLATFORM_FLAGS $OTHER_COMPILER_FLAGS $LIB_OPT_LEVEL $LIB_FPIC" ./configure --prefix=$DIST $host_opt $targ_opt
make
make install
exit 0
