#!/usr/bin/env bash

set -e
[ "$DEP_DIR" ] && cd $DEP_DIR
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 openssl
mkdir openssl

for target in osx ; do
    echo '***************' OpenSSL-32 $target
    TARGET=$target OPENSSL_TARGET=darwin-i386-cc ARCH=i386 $O3/core/deps/openssl/build-openssl
    echo '***************' OpenSSL-64 $target
    TARGET=$target OPENSSL_TARGET=darwin64-x86_64-cc ARCH=x86_64 $O3/core/deps/openssl/build-openssl
    cd openssl/openssl-$target
    cp -a x86_64/include .
    rm include/openssl/opensslconf.h # contains 32/64-bit specific references
    for l in libcrypto.a libssl.a ; do
	lipo -create */lib/$l -output $l
    done
    mkdir lib
    mv *.a lib
    cd ../..
done
exit 0
