#!/data/data/com.termux/files/usr/bin/bash
set -e

if [ -z "${PREFIX:-}" ] || [ ! -d /data/data/com.termux/files/usr ]; then
  echo "This script must be run inside Termux." >&2
  exit 1
fi

arch=$(uname -m)
if [ "$arch" != "aarch64" ]; then
  echo "Unsupported architecture: $arch (repo is aarch64 only)." >&2
  exit 1
fi

pkg install -y gnupg curl

KEYDIR=/data/data/com.termux/files/usr/etc/apt/trusted.gpg.d
LISTDIR=/data/data/com.termux/files/usr/etc/apt/sources.list.d
CONF_DIR=/data/data/com.termux/files/usr/etc/apt/apt.conf.d

mkdir -p "$KEYDIR" "$LISTDIR" "$CONF_DIR"

curl -fsSL https://repo.mizodevelopment.com/termux/keys/mizo.gpg \
  | gpg --batch --yes --dearmor -o "$KEYDIR/mizo.gpg"

echo "deb [signed-by=$KEYDIR/mizo.gpg] https://repo.mizodevelopment.com/termux stable main" \
  > "$LISTDIR/mizo.list"

cat > "$CONF_DIR/99mizo-releaseinfo.conf" <<'EOF'
Acquire::AllowReleaseInfoChange::Origin "Mizo Development";
Acquire::AllowReleaseInfoChange::Label "Mizo";
Acquire::AllowReleaseInfoChange::Suite "stable";
Acquire::AllowReleaseInfoChange::Codename "stable";
EOF

apt update --allow-releaseinfo-change

echo "Mizo repo added."
