 #!/bin/bash

cd "$( dirname "${BASH_SOURCE[0]}" )"

############
## Config ##
############

ndk_version=23.1.7779620
if [[ -d "/opt/android-sdk/ndk/${ndk_version}" ]]
then
    NDK="/opt/android-sdk/ndk/${ndk_version}"
fi
export ANDROID_NDK_HOME=$NDK
export PATH="$PATH:$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin"

if [[ $# == 1 && $1 == "arm64" ]]
then
    ABI=arm64-v8a
    echo "compile arm64-v8a things..."
else
    ABI=armeabi-v7a
    echo "compile armeabi-v7a things..."
fi

LIBS_ROOT=`pwd`

if [[ $ABI == arm64-v8a ]]
then
    export CC="aarch64-linux-android29-clang"
    export CCX="aarch64-linux-android29-clang++"
    export GOARCH=arm64
else
    export CC="armv7a-linux-androideabi16-clang"
    export CCX="armv7a-linux-androideabi16-clang++"
    export GOARCH=arm
fi

export CGO_ENABLED=1
export GOOS="android"

##############
##############

# Clean
rm -r -f arm64-v8a armeabi-v7a
mkdir arm64-v8a armeabi-v7a

#################
# libobfs4proxy #
#################

pushd lyrebird/cmd/lyrebird/
go build -ldflags="-s -w -checklinkname=0" -o libobfs4proxy.so
mv libobfs4proxy.so ${LIBS_ROOT}/${ABI}/libobfs4proxy.so || exit 1
popd

#####################
# libdnscrypt-proxy #
#####################

pushd dnscrypt-proxy/dnscrypt-proxy/
go build -ldflags="-s -w" -o libdnscrypt-proxy.so
mv libdnscrypt-proxy.so ${LIBS_ROOT}/${ABI}/libdnscrypt-proxy.so || exit 1
popd

################
# libsnowflake #
################

pushd snowflake/client/
go build -ldflags="-s -w -checklinkname=0" -o libsnowflake.so
mv libsnowflake.so ${LIBS_ROOT}/${ABI}/libsnowflake.so || exit 1
popd

################
# libconjure #
################

pushd libzmq/builds/android/
export NDK_VERSION="android-ndk-r23b"
export ANDROID_NDK_ROOT=$NDK
if [[ $ABI == arm64-v8a ]]
then
    #compile arm64-v8a things...
    export MIN_SDK_VERSION=21
    ./build.sh arm64
else
    #compile armeabi-v7a things...
    export MIN_SDK_VERSION=19
    ./build.sh arm
fi
popd

pushd conjure/client/
if [[ $ABI == arm64-v8a ]]
then
    #compile arm64-v8a things...
    export CGO_LDFLAGS="-L${LIBS_ROOT}/libzmq/builds/android/prefix/arm64/lib -l:libzmq.a"
    export PKG_CONFIG_PATH="${LIBS_ROOT}/libzmq/builds/android/prefix/arm64/lib/pkgconfig"
else
    #compile armeabi-v7a things...
    export CGO_LDFLAGS="-L${LIBS_ROOT}/libzmq/builds/android/prefix/arm/lib -l:libzmq.a"
    export PKG_CONFIG_PATH="${LIBS_ROOT}/libzmq/builds/android/prefix/arm/lib/pkgconfig"
fi
go build -ldflags="-s -w -checklinkname=0 -linkmode=external -extldflags '$CGO_LDFLAGS'" -o libconjure.so
mv libconjure.so ${LIBS_ROOT}/${ABI}/libconjure.so || exit 1
popd

################
# libdnstt #
################

pushd dnstt/dnstt-client/
go build -ldflags="-s -w -checklinkname=0" -o libdnstt.so
mv libdnstt.so ${LIBS_ROOT}/${ABI}/libdnstt.so || exit 1
popd

################
# libnflog #
################

pushd Nflog-android/nflog/
go build -ldflags="-s -w" -o libnflog.so
mv libnflog.so ${LIBS_ROOT}/${ABI}/libnflog.so || exit 1
popd

##########
# libtor #
##########

pushd ../../TorBuildScript/external/

export EXTERNAL_ROOT=`pwd`

if [[ $ABI == arm64-v8a ]]
then
    #compile arm64-v8a things...
    export APP_ABI=arm64
    NDK_PLATFORM_LEVEL=21 NDK_BIT=64 make clean
    NDK_PLATFORM_LEVEL=21 NDK_BIT=64 make
    NDK_PLATFORM_LEVEL=21 NDK_BIT=64 make showsetup
else
    #compile armeabi-v7a things...
    export APP_ABI=armeabi-v7a
    make clean
    make
    make showsetup
fi

mv ../tor-android-binary/src/main/libs/${APP_ABI}/libtor.so ${LIBS_ROOT}/${ABI}/libtor.so || exit 1

popd

###########
# libi2pd #
###########

pushd ../../PurpleI2PBuildScript/external/

export EXTERNAL_ROOT=`pwd`
export TARGET_I2P_ABI=$ABI
export APP_ABI=$ABI

if [[ $ABI == arm64-v8a ]]
then
    #compile arm64-v8a things...
    export TARGET_I2P_PLATFORM=21
    NDK_PLATFORM_LEVEL=21 NDK_BIT=64 make clean
    NDK_PLATFORM_LEVEL=21 NDK_BIT=64 make
    NDK_PLATFORM_LEVEL=21 NDK_BIT=64 make showsetup
else
    #compile armeabi-v7a things...
    export TARGET_I2P_PLATFORM=16
    make clean
    make
    make showsetup
fi

mv ../i2pd-android-binary/src/main/libs/${APP_ABI}/libi2pd.so ${LIBS_ROOT}/${ABI}/libi2pd.so || exit 1

popd
