#!/bin/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

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

# put build targets here
DIST=$(pwd)/mbedtls/mbedtls-$PLATFORM
rm -rf $DIST
mkdir -p $DIST

if [ "$NO_WIPE" = "1" ]; then
    echo RETAIN existing source
    cd $MBEDTLS_VERSION
else
    echo WIPE and reunzip source
    rm -rf $MBEDTLS_VERSION
    [ -z "$DL" ] && DL=~/Downloads
    tar xfz $DL/$MBEDTLS_VERSION-apache.tgz
    cd $MBEDTLS_VERSION

    # enable MD4 (needed for NTLM auth)
    perl -pi -e 's/^\/\/// if /#define MBEDTLS_MD4_C/' include/mbedtls/config.h
fi

# compiler vars
CC=cc
LD=ld
AR=ar
RANLIB=ranlib
[ "$GCC_CMD" ] && CC=$GCC_CMD
[ "$LD_CMD" ] && LD=$LD_CMD
[ "$AR_CMD" ] && AR=$AR_CMD
[ "$RANLIB_CMD" ] && RANLIB=$RANLIB_CMD

# build it
SRC=$(pwd)
cd library
rm -f *.o
for c in *.c ; do
    CMD="$CC -I../include $PLATFORM_FLAGS $OTHER_COMPILER_FLAGS $LIB_OPT_LEVEL $LIB_FPIC -c $c"
    echo $CMD
    $CMD
done

# create archive
cd $DIST
mkdir library
$AR rc library/libmbedtls.a $SRC/library/*.o
$RANLIB library/libmbedtls.a 2>&1 | grep -v "has no symbols" || true

# copy headers
mkdir -p include/mbedtls
cp $SRC/include/mbedtls/*.h include/mbedtls/
exit 0
