#!/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 OPENSSL_TARGET=darwin64-x86_64-cc $O3/core/deps/openssl/build-openssl
    else
        ARCH=arm64 CROSS_TOP=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer \
                 CROSS_SDK=iPhoneOS.sdk TARGET=$target OPENSSL_TARGET="ios64-cross no-dso no-engine" \
                 $O3/core/deps/openssl/build-openssl

        ARCH=armv7 CROSS_TOP=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer \
                 CROSS_SDK=iPhoneOS.sdk TARGET=$target OPENSSL_TARGET="ios-cross no-dso no-engine" \
                 $O3/core/deps/openssl/build-openssl
    fi
done

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

exit 0
