#!/bin/bash -x
# see https://chromium.googlesource.com/chromium/src/+/master/components/cronet/build_instructions.md

CHROMIUM_SRC_ROOT=${CHROMIUM_SRC_ROOT:-/root/chromium/src}
DEPOT_TOOLS_ROOT=${DEPOT_TOOLS_ROOT:-/root/depot_tools}
export PATH="$DEPOT_TOOLS_ROOT:$PATH"
BUILD_VARIANT=${1:-release}

set -euo pipefail

cd "$(dirname "$0")" || exit 1
PATCH_DIR="$(pwd)"

# newer chromium assumes `python` is avaiable and is python3
python3 -m venv venv
export PATH="$PATCH_DIR/venv/bin:$PATH"

cd "$CHROMIUM_SRC_ROOT" || exit 2
# patches are generated by `git ls-files -m | xargs git cl format`, `git ls-files -m | grep -F '.gn' | xargs gn format` and `git diff --patch-with-stat`.
source $CHROMIUM_SRC_ROOT/chrome/VERSION
CHROME_VERSION=$MAJOR.$MINOR.$BUILD.$PATCH
git clean -ffd
git checkout .

patch --fuzz=0 --no-backup-if-mismatch --forward --strip=1 --reject-file=- <"$PATCH_DIR/01-proxy_support.patch"
patch --fuzz=0 --no-backup-if-mismatch --forward --strip=1 --reject-file=- <"$PATCH_DIR/02-dns_resolver_rules.patch"
patch --fuzz=0 --no-backup-if-mismatch --forward --strip=1 --reject-file=- <"$PATCH_DIR/02.5-update_api_01_02.patch"
# the tls_options patch needs some fixes? cronet requests fail with it applied
#patch --fuzz=0 --no-backup-if-mismatch --forward --strip=1 --reject-file=- <"$PATCH_DIR/03-tls_options.patch"
#patch --fuzz=0 --no-backup-if-mismatch --forward --strip=1 --reject-file=- <"$PATCH_DIR/04-dns_over_https_config.patch"
#patch --fuzz=0 --no-backup-if-mismatch --forward --strip=1 --reject-file=- <"$PATCH_DIR/05-update_api.patch"

# XXX hacky fix of build problem in M128
if [[ ! -L "$CHROMIUM_SRC_ROOT/buildtools/reclient_cfgs/chromium-browser-clang" ]]; then
    ln -s "$CHROMIUM_SRC_ROOT/buildtools/reclient_cfgs/linux//chromium-browser-clang" "$CHROMIUM_SRC_ROOT/buildtools/reclient_cfgs/chromium-browser-clang"
fi

# Build the linux version
# build cronet only(without java jni)
# this is failing in M138
#gn gen out/Cronet-Desktop
#autoninja -C out/Cronet-Desktop cronet # cronet_sample

# Build the various Android versions
for arch in arm arm64 x86 x64; do

    out_dir="$CHROMIUM_SRC_ROOT/out/Cronet-$arch-$BUILD_VARIANT"
    if [[ -d "${out_dir}/cronet" ]]; then
        rm -r "${out_dir}/cronet"
    fi
    # do we still need this workaround?
    # mkdir -p "${out_dir}/cronet/javadoc"

    gn_args="--out_dir=$out_dir"
    if [[ $BUILD_VARIANT == release ]]; then
        gn_args="$gn_args --release"
    fi

    # arm64 is the default these days
    if [[ $arch == "arm" ]]; then
        gn_args="$gn_args --arm"
    elif [[ $arch == "x64" ]]; then
        gn_args="$gn_args --x64"
    elif [[ $arch == "x86" ]]; then
        gn_args="$gn_args --x86"
    fi

    # XXX how is the default platform broken? 🤦
    if [[ $arch != "arm64" ]]; then
        "$CHROMIUM_SRC_ROOT/components/cronet/tools/cr_cronet.py" gn $gn_args
    fi

    # this defaults to true and errors out (thanks Google)
    sed -i 's/use_remoteexec = true/use_remoteexec = false/g' "$out_dir/args.gn"

    autoninja -C "$out_dir" cronet_package

    # Copy all the architechure outputs in the arm output directory for packaging
    # (this should probably use a temp dir)
    if [[ $arch != "arm" ]]; then
        cp -a "$out_dir/cronet/libs/"* "$out_dir/../Cronet-arm-$BUILD_VARIANT/cronet/libs/"
    fi
done

# https://gn.googlesource.com/gn/+/master/docs/cross_compiles.md
find "out/Cronet-arm-$BUILD_VARIANT/cronet/libs/" -name "libcronet.*.so" -not -name "libcronet.$CHROME_VERSION.so" -delete
ls "out/Cronet-arm-$BUILD_VARIANT/cronet/"
file "out/Cronet-arm-$BUILD_VARIANT/cronet/libs"/*/*.so

cd "$PATCH_DIR" || exit 3
CRONET_OUTPUT_DIR="$CHROMIUM_SRC_ROOT/out/Cronet-arm-$BUILD_VARIANT/cronet" bash package_aar.sh "$BUILD_VARIANT"
