#!/bin/bash

APP_NAME="libgofunc"
VERSION="${1:-v0.1.1}"
OUTPUT_DIR="assets"
BUILD_DIR="build"

# need Android NDK
NDK_HOME="$HOME/Android/Sdk/ndk/28.2.13676358" # <--- CHECK YOUR VERSION
API=21

TOOLCHAIN="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"
OS="$(uname -s)"

if [ "$OS" = "Darwin" ]; then
    export IOS_SDK=$(xcrun --sdk iphoneos --show-sdk-path)
    export IOS_SIM_SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)

    echo "Building for iOS arm64..."
    GOOS=ios GOARCH=arm64 CGO_ENABLED=1 \
    CC="$(xcrun --sdk iphoneos --find clang)" \
    CGO_CFLAGS="-isysroot $IOS_SDK -arch arm64" \
    CGO_LDFLAGS="-isysroot $IOS_SDK -arch arm64" \
    go build -buildmode=c-archive -o build/ios/device/libgofunc_arm64.a .

    GOOS=ios GOARCH=amd64 CGO_ENABLED=1 \
    CC="$(xcrun --sdk iphonesimulator --find clang)" \
    CGO_CFLAGS="-isysroot $IOS_SIM_SDK -arch x86_64" \
    CGO_LDFLAGS="-isysroot $IOS_SIM_SDK -arch x86_64" \
    go build -buildmode=c-archive -o build/ios/sim/libgofunc_arm64_sim.a .

    # xcodebuild -create-xcframework \
    # -library build/ios/device/libgofunc_arm64.a -headers build/ios/device/ \
    # -library build/ios/sim/libgofunc_arm64_sim.a -headers build/ios/sim/ \
    # -output build/libgofunc.xcframework

    # CGO_ENABLED=1 GOOS=ios GOARCH=arm64 SDK=iphoneos \
    # go build -buildmode=c-archive -o ./build/libgofunc_arm64.a .

    # CGO_ENABLED=1 GOOS=ios GOARCH=arm64 SDK=iphonesimulator \
    # go build -buildmode=c-archive -o ./build/libgofunc_arm64_sim.a .

    lipo -create build/ios/device/libgofunc_arm64.a build/ios/sim/libgofunc_arm64_sim.a -output build/ios/libgofunc.a

    cp ./build/ios/libgofunc.a $HOME/ws/forward_pos/native/ios/x86_64/libgofunc.a
elif [ "$OS" = "Linux" ]; then
    mkdir -p "${OUTPUT_DIR}/${VERSION}"

    echo "Building for Android amd64..."
    CC="$TOOLCHAIN/x86_64-linux-android$API-clang" \
    CGO_ENABLED=1 GOOS=android GOARCH=amd64 \
    go build -buildmode=c-shared -o $BUILD_DIR/libgofunc_amd64.so .

    ARCHIVE_NAME="${APP_NAME}-${VERSION}-amd64.tar.gz"
    tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" libgofunc_amd64.so

    echo "Building for Android arm64..."
    CC="$TOOLCHAIN/aarch64-linux-android$API-clang" \
    CGO_ENABLED=1 GOOS=android GOARCH=arm64 \
    go build -buildmode=c-shared -o $BUILD_DIR/libgofunc_arm64.so .

    ARCHIVE_NAME="${APP_NAME}-${VERSION}-arm64.tar.gz"
    tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" libgofunc_arm64.so

    echo "Building for Android armv7a..."
    CC="$TOOLCHAIN/armv7a-linux-androideabi$API-clang" \
    CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \
    go build -buildmode=c-shared -o $BUILD_DIR/libgofunc_armv7a.so .

    ARCHIVE_NAME="${APP_NAME}-${VERSION}-armv7a.tar.gz"
    tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" libgofunc_armv7a.so

    # cp ./assets/libgofunc_x64.so $HOME/ws/forward_pos/native/android/x86_64/libgofunc.so
    # cp ./assets/libgofunc_arm64.so $HOME/ws/forward_pos/native/android/arm64-v8a/libgofunc.so
    # cp ./assets/libgofunc_armv7a.so $HOME/ws/forward_pos/native/android/armeabi-v7a/libgofunc.so

else
    echo "Unsupported OS: $OS"
    exit 1
fi

echo "Done!"
echo "Artifacts:"
ls -lh "${OUTPUT_DIR}"
