#!/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 ios osx; do
    echo '***************' OpenSSL-64 $target
    

    if [ "$target" = "osx" ]; then
        TARGET=$target ARCH=amd64 OPENSSL_TARGET=darwin64-x86_64 $O3/core/deps/openssl/build-openssl
        TARGET=$target ARCH=arm64 OPENSSL_TARGET=darwin64-arm64 $O3/core/deps/openssl/build-openssl
    else
        TARGET=$target ARCH=arm64 OPENSSL_TARGET="ios64-cross no-dso no-engine" $O3/core/deps/openssl/build-openssl
    fi
done

pushd "$PWD"
cd openssl/openssl-ios
#mkdir -p fat/lib
#for l in libcrypto.a libssl.a ; do
#    lipo -create arm64/lib/$l armv7/lib/$l -output fat/lib/$l
#done
popd

pushd "$PWD"
cd openssl/openssl-osx
mkdir -p fat/lib
for l in libcrypto.a libssl.a ; do
    lipo -create arm64/lib/$l amd64/lib/$l -output fat/lib/$l
done
popd

exit 0
