#!/bin/sh
# Pull pinned vendor sources via git subtree. Idempotent.
#
# Usage:
#   scripts/fetch-vendor          # add or update to pinned tag, verify SHA
#
# Bump:
#   1. Pick a new tag from https://github.com/MuntashirAkon/libadb-android
#   2. git ls-remote https://github.com/MuntashirAkon/libadb-android.git \
#          refs/tags/<tag>
#      → copy the commit hex.
#   3. Update LIBADB_TAG and LIBADB_SHA below.
#   4. Run this script.
#   5. Re-prune. This repo keeps only the libadb module plus license files
#      (vendor/libadb-android/{libadb,LICENSES,COPYING,README.md,
#      SERVICES.md}); the rest of upstream - sample app, gradle wrapper,
#      jitpack.yml - is unused. A subtree pull re-adds it; delete it again.

set -eu

LIBADB_REPO='https://github.com/MuntashirAkon/libadb-android.git'
LIBADB_TAG='3.1.1'
LIBADB_SHA='c849886ebc6d48e7b46d967e78a6bb65c90c3b74'
LIBADB_PREFIX='vendor/libadb-android'

cd "$(git rev-parse --show-toplevel)"

# Tags can be re-pointed upstream; cross-check against the pinned commit.
remote_sha=$(git ls-remote "$LIBADB_REPO" "refs/tags/$LIBADB_TAG" | awk '{print $1}')
if [ "$remote_sha" != "$LIBADB_SHA" ]; then
    echo "fetch-vendor: refs/tags/$LIBADB_TAG points at $remote_sha," >&2
    echo "             expected $LIBADB_SHA. Refusing to pull." >&2
    exit 1
fi

if [ ! -d "$LIBADB_PREFIX" ]; then
    echo "fetch-vendor: adding $LIBADB_PREFIX @ $LIBADB_TAG ($LIBADB_SHA)"
    git subtree add --prefix="$LIBADB_PREFIX" "$LIBADB_REPO" "$LIBADB_SHA" --squash
else
    echo "fetch-vendor: pulling $LIBADB_PREFIX @ $LIBADB_TAG ($LIBADB_SHA)"
    git subtree pull --prefix="$LIBADB_PREFIX" "$LIBADB_REPO" "$LIBADB_SHA" --squash
fi

echo "fetch-vendor: ok"
