#!/usr/bin/env bash

set -e
if [ -z "$O3" ]; then
    echo O3 var must point to ovpn3 tree ; exit 1
fi
if [ -z "$DEP_DIR" ]; then
    echo DEP_DIR var must point to ovpn3 dependency tree
    exit 1
fi
cd $DEP_DIR

rm -rf polarssl
mkdir polarssl

if [ "$OSX_ONLY" != "1" ]; then
    # for ios, build with minicrypto
    for target in ios-dbg ios ; do
	echo '***************' TARGET $target
	VERBOSE=1 TARGET=$target USE_MINICRYPTO=0 $O3/core/deps/polarssl/build-polarssl
	mv polarssl-$target polarssl
	[ "$IOS_DBG_ONLY" = "1" ] && exit
    done

    # ios simulators
    for target in iossim iossim-dbg ; do
	echo '***************' TARGET $target
	VERBOSE=1 TARGET=$target $O3/core/deps/polarssl/build-polarssl
	mv polarssl-$target polarssl/
    done
fi

# osx
[ -z "$OSX_MINICRYPTO" ] && OSX_MINICRYPTO=0
[ -z "$OSX_SERVER" ] && OSX_SERVER=1
for target in osx osx-dbg ; do
    echo '***************' TARGET $target
    VERBOSE=1 TARGET=$target USE_MINICRYPTO=$OSX_MINICRYPTO MINICRYPTO_NO_AES=1 ENABLE_SERVER=$OSX_SERVER $O3/core/deps/polarssl/build-polarssl
    mv polarssl-$target polarssl/
done
exit 0
