#!/bin/sh

ANDROID_NDK=~/Android/Sdk/ndk/23.1.7779620
OPENSSL_VERSION=3.1.4

API_LEVEL=24

BUILD_DIR=out/openssl_android_build

BUILD_TARGETS="x86_64 x86 arm64-v8a armeabi-v7a"

rm -rfv openssl-${OPENSSL_VERSION}

if [ ! -d openssl-${OPENSSL_VERSION} ]
then
    if [ ! -f openssl-${OPENSSL_VERSION}.tar.gz ]
    then
        wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
    fi
    tar xzf openssl-${OPENSSL_VERSION}.tar.gz || exit 128
fi
WORK_DIR=openssl-${OPENSSL_VERSION}

##### export ndk directory. Required by openssl-build-scripts #####
export ANDROID_NDK

##### build-function #####
build_the_thing() {
    TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
    export PATH=$TOOLCHAIN/$TRIBLE/bin:$TOOLCHAIN/bin:$PATH
    echo $PATH
    export GLOBAL_OPTIONS="no-trace no-asm no-threads no-md2 no-md4 no-dso no-async no-multiblock no-dgram no-filenames no-shared no-ssl3 no-engine no-dynamic-engine no-zlib no-comp no-psk no-idea no-srp no-weak-ssl-ciphers no-dtls no-egd no-tests -Os"
    echo "./Configure $SSL_TARGET $OPTIONS $GLOBAL_OPTIONS -fuse-ld=$TOOLCHAIN/bin/ld" && \
    ./Configure $SSL_TARGET $OPTIONS $GLOBAL_OPTIONS -fuse-ld=$TOOLCHAIN/bin/ld no-tests && \
    make clean && \
    make build_generated && \
    make libcrypto.a libssl.a || exit 128
}

##### set variables according to build-tagret #####
for build_target in $BUILD_TARGETS
do
    echo "Building $build_target"
    cd $WORK_DIR || exit 128

    case $build_target in
    armeabi-v7a)
        TRIBLE="arm-linux-androideabi"
        OPTIONS="--target=armv7a-linux-androideabi -ffunction-sections -fdata-sections -Wl,--fix-cortex-a8 -fPIC -D__ANDROID_API__=$API_LEVEL -Wno-macro-redefined"
        DESTDIR="/tmp/$BUILD_DIR/armeabi-v7a"
        SSL_TARGET="android-arm"
        CC=clang
    ;;
    x86)
        TRIBLE="i686-linux-android"
        OPTIONS="-fPIC -ffunction-sections -fdata-sections -D__ANDROID_API__=${API_LEVEL} -Wno-macro-redefined"
        DESTDIR="/tmp/$BUILD_DIR/x86"
        SSL_TARGET="android-x86"
        CC=clang
    ;;
    x86_64)
        TRIBLE="x86_64-linux-android"
        OPTIONS="--static -static -ffunction-sections -fdata-sections -D__ANDROID_API__=${API_LEVEL} -Wno-macro-redefined"
        DESTDIR="/tmp/$BUILD_DIR/x86_64"
        SSL_TARGET="android-x86_64"
        CC=clang
    ;;
    arm64-v8a)
        TRIBLE="aarch64-linux-android"
        OPTIONS="--static -static -ffunction-sections -fdata-sections -fPIC -D__ANDROID_API__=${API_LEVEL} -Wno-macro-redefined"
        DESTDIR="/tmp/$BUILD_DIR/arm64-v8a"
        SSL_TARGET="android-arm64"
        CC=clang
    ;;
    esac

    rm -rf $DESTDIR
    build_the_thing
    cd ..
#### copy libraries and includes to output-directory #####
    echo WORK_DIR=$WORK_DIR
    rm -rf deps/openssl/android/$build_target/
    mkdir -p deps/openssl/android/$build_target/usr/local/include
    mkdir -p deps/openssl/android/$build_target/usr/local/lib/
    cp -R $WORK_DIR/include/* deps/openssl/android/$build_target/usr/local/include/
    cp $WORK_DIR/*.a deps/openssl/android/$build_target/usr/local/lib/
done

echo Success