#!/bin/sh # Apply Geminux local signed @geminux APKs, overlay configs, firmware blobs, # BusyBox links, SSH keys, OpenRC runlevels, and permissions to a target rootfs. set -eu BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)" TARGET_ROOTFS="${1:-${ROOTFS_DIR:-$BASE_DIR/rootfs}}" DEVICE="${2:-gemini-x27}" if [ ! -d "$TARGET_ROOTFS" ]; then echo "Error: Target rootfs directory does not exist: $TARGET_ROOTFS" >&2 exit 1 fi case "$DEVICE" in gemini-x27|gemini|geminux) DEVICE="gemini-x27" ;; *) echo "Error: Unsupported device '$DEVICE'." >&2 exit 1 ;; esac VER="$("$BASE_DIR/scripts/version.sh" --print)" echo "[1/4] Configuring @geminux + pure Alpine Edge repositories..." mkdir -p "$TARGET_ROOTFS/etc/apk/keys" "$TARGET_ROOTFS/var/lib/geminux/apk-repo" if [ -f "$BASE_DIR/keys/geminux-local.rsa.pub" ]; then cp -f "$BASE_DIR/keys/geminux-local.rsa.pub" "$TARGET_ROOTFS/etc/apk/keys/geminux-local.rsa.pub" fi cat > "$TARGET_ROOTFS/etc/apk/repositories" </dev/null 2>&1 && \ [ -f "$TARGET_ROOTFS/var/lib/geminux/apk-repo/aarch64/APKINDEX.tar.gz" ] && \ [ -f "$TARGET_ROOTFS/etc/apk/keys/geminux-local.rsa.pub" ]; then if [ "$TARGET_ROOTFS" != "/" ]; then mkdir -p /var/lib/geminux ln -sfn "$TARGET_ROOTFS/var/lib/geminux/apk-repo" /var/lib/geminux/apk-repo cp -f "$TARGET_ROOTFS/etc/apk/keys/geminux-local.rsa.pub" /etc/apk/keys/geminux-local.rsa.pub 2>/dev/null || true fi apk --root "$TARGET_ROOTFS" --arch aarch64 update --force-refresh >/dev/null # Clear any older @geminux world pins and installed hashes so rebuilt local # packages with unchanged -r0 version strings always extract updated payloads. if [ -f "$TARGET_ROOTFS/etc/apk/world" ]; then grep -v -E '^geminux-(kernel-modules|xlibre|suckless|dunst|ui|bantam|config)(@|=|$)' \ "$TARGET_ROOTFS/etc/apk/world" > "$TARGET_ROOTFS/etc/apk/world.tmp" || true mv "$TARGET_ROOTFS/etc/apk/world.tmp" "$TARGET_ROOTFS/etc/apk/world" fi apk --root "$TARGET_ROOTFS" --arch aarch64 del --no-scripts --purge=yes \ geminux-config geminux-bantam geminux-ui geminux-dunst geminux-suckless \ geminux-xlibre geminux-kernel-modules 2>/dev/null || true apk --root "$TARGET_ROOTFS" --arch aarch64 add --no-scripts --clean-protected=no \ "geminux-kernel-modules@geminux=7.3.5-r0" \ "geminux-xlibre@geminux=25.2.0-r0" \ "geminux-suckless@geminux=${VER}-r0" \ "geminux-dunst@geminux=1.12.2-r0" \ "geminux-ui@geminux=${VER}-r0" \ "geminux-bantam@geminux=${VER}-r0" \ "geminux-config@geminux=${VER}-r0" else # Fallback direct copy when running on host without apk-tools if [ -d "$BASE_DIR/overlay/rootfs" ]; then cp -af "$BASE_DIR/overlay/rootfs/." "$TARGET_ROOTFS/" fi for subdir in xlibre suckless dunst geminux bantam; do if [ -d "$BASE_DIR/prebuilt/$subdir" ]; then cp -af "$BASE_DIR/prebuilt/$subdir/." "$TARGET_ROOTFS/" fi done if [ -d "$BASE_DIR/prebuilt/kernel/lib/modules" ]; then mkdir -p "$TARGET_ROOTFS/lib/modules" cp -af "$BASE_DIR/prebuilt/kernel/lib/modules/." "$TARGET_ROOTFS/lib/modules/" fi if [ -d "$BASE_DIR/prebuilt/kernel/boot" ]; then mkdir -p "$TARGET_ROOTFS/boot" cp -af "$BASE_DIR/prebuilt/kernel/boot/." "$TARGET_ROOTFS/boot/" fi fi # Inject device-owned firmware extracted from stock Gemini system image (gitignored). if [ -d "$BASE_DIR/firmware-cache/mt6797" ]; then mkdir -p "$TARGET_ROOTFS/lib/firmware/mediatek/mt6797" cp -af "$BASE_DIR/firmware-cache/mt6797/." "$TARGET_ROOTFS/lib/firmware/mediatek/mt6797/" fi echo "[3/4] Ensuring BusyBox symlinks, SSH keys, OpenRC services, and skel..." mkdir -p "$TARGET_ROOTFS/bin" "$TARGET_ROOTFS/sbin" "$TARGET_ROOTFS/usr/bin" "$TARGET_ROOTFS/usr/sbin" if [ -e "$TARGET_ROOTFS/bin/busybox" ]; then ln -sfn /bin/busybox "$TARGET_ROOTFS/sbin/init" for spec in \ "busybox-applets.txt:$TARGET_ROOTFS/bin" \ "busybox-sbin-applets.txt:$TARGET_ROOTFS/sbin" \ "busybox-usr-bin-applets.txt:$TARGET_ROOTFS/usr/bin" \ "busybox-usr-sbin-applets.txt:$TARGET_ROOTFS/usr/sbin"; do list_file="$BASE_DIR/scripts/${spec%%:*}" dest_dir="${spec#*:}" [ -f "$list_file" ] || continue while IFS= read -r applet; do [ -n "$applet" ] || continue if [ ! -e "$dest_dir/$applet" ]; then [ ! -L "$dest_dir/$applet" ] || rm -f "$dest_dir/$applet" ln -s /bin/busybox "$dest_dir/$applet" fi done < "$list_file" done fi if [ -f "$BASE_DIR/keys/geminux_id_ed25519.pub" ]; then mkdir -p "$TARGET_ROOTFS/root/.ssh" "$TARGET_ROOTFS/home/user/.ssh" cp "$BASE_DIR/keys/geminux_id_ed25519.pub" "$TARGET_ROOTFS/root/.ssh/authorized_keys" cp "$BASE_DIR/keys/geminux_id_ed25519.pub" "$TARGET_ROOTFS/home/user/.ssh/authorized_keys" chmod 0700 "$TARGET_ROOTFS/root/.ssh" "$TARGET_ROOTFS/home/user/.ssh" chmod 0600 "$TARGET_ROOTFS/root/.ssh/authorized_keys" "$TARGET_ROOTFS/home/user/.ssh/authorized_keys" fi if command -v glib-compile-schemas >/dev/null 2>&1 && \ [ -d "$TARGET_ROOTFS/usr/share/glib-2.0/schemas" ]; then glib-compile-schemas "$TARGET_ROOTFS/usr/share/glib-2.0/schemas" 2>/dev/null || true fi rm -f "$TARGET_ROOTFS/etc/runlevels/sysinit/hwdrivers" rm -f "$TARGET_ROOTFS/etc/runlevels/default/wpa_supplicant" if [ -f "$TARGET_ROOTFS/etc/inittab" ]; then sed -i 's/^tty1::respawn:/#tty1::respawn:/' "$TARGET_ROOTFS/etc/inittab" fi if [ -e "$TARGET_ROOTFS/usr/lib/dri/libdril_dri.so" ]; then ln -sfn libdril_dri.so "$TARGET_ROOTFS/usr/lib/dri/simpledrm_dri.so" fi if command -v ssh-keygen >/dev/null 2>&1; then mkdir -p "$TARGET_ROOTFS/etc/ssh" ssh-keygen -A -f "$TARGET_ROOTFS" >/dev/null 2>&1 || true fi if [ -e "$TARGET_ROOTFS/etc/init.d/geminux-usb-gadget" ]; then mkdir -p "$TARGET_ROOTFS/etc/runlevels/boot" ln -sf "/etc/init.d/geminux-usb-gadget" "$TARGET_ROOTFS/etc/runlevels/boot/geminux-usb-gadget" fi rm -f "$TARGET_ROOTFS/etc/runlevels/default/geminux-usb-gadget" for svc in geminux-hwinit geminux-ccci geminux-powerd geminux-leds bluetooth polkit; do if [ -e "$TARGET_ROOTFS/etc/init.d/$svc" ]; then mkdir -p "$TARGET_ROOTFS/etc/runlevels/default" ln -sf "/etc/init.d/$svc" "$TARGET_ROOTFS/etc/runlevels/default/$svc" fi done for fdir in misc TTF OTF; do mkdir -p "$TARGET_ROOTFS/usr/share/fonts/$fdir" if [ ! -f "$TARGET_ROOTFS/usr/share/fonts/$fdir/fonts.dir" ]; then printf '0\n' > "$TARGET_ROOTFS/usr/share/fonts/$fdir/fonts.dir" fi done if [ -f "$TARGET_ROOTFS/usr/share/dbus-1/system-services/org.freedesktop.ModemManager1.service" ]; then sed -i 's|^Exec=.*|Exec=/bin/false|' \ "$TARGET_ROOTFS/usr/share/dbus-1/system-services/org.freedesktop.ModemManager1.service" fi if [ -f "$TARGET_ROOTFS/etc/rc.conf" ] && ! grep -q '^rc_logger=' "$TARGET_ROOTFS/etc/rc.conf"; then printf '\nrc_logger="YES"\n' >> "$TARGET_ROOTFS/etc/rc.conf" fi if [ -x "$BASE_DIR/scripts/version.sh" ]; then "$BASE_DIR/scripts/version.sh" "$TARGET_ROOTFS" fi "$BASE_DIR/scripts/set-geminux-hostname.sh" "$TARGET_ROOTFS" "$DEVICE" if [ -x "$TARGET_ROOTFS/bin/bash" ] && [ ! -e "$TARGET_ROOTFS/usr/bin/bash" ]; then ln -sf /bin/bash "$TARGET_ROOTFS/usr/bin/bash" fi if [ -f "$TARGET_ROOTFS/etc/passwd" ]; then sed -i -E \ -e 's#^(root:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*):.*#\1:/bin/bash#' \ -e 's#^(user:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*):.*#\1:/bin/bash#' \ "$TARGET_ROOTFS/etc/passwd" fi mkdir -p "$TARGET_ROOTFS/root" for f in .bashrc .profile .bash_profile .gitconfig; do if [ -f "$TARGET_ROOTFS/etc/skel/$f" ]; then cp -f "$TARGET_ROOTFS/etc/skel/$f" "$TARGET_ROOTFS/root/$f" fi done echo "[4/4] Populating /home/user skel and setting permissions..." mkdir -p "$TARGET_ROOTFS/home/user/.cache" "$TARGET_ROOTFS/home/user/.config" \ "$TARGET_ROOTFS/home/user/.local/share/geminux" \ "$TARGET_ROOTFS/home/user/Pictures" "$TARGET_ROOTFS/home/user/Videos" \ "$TARGET_ROOTFS/home/user/Downloads" if [ -d "$TARGET_ROOTFS/etc/skel" ]; then cp -af "$TARGET_ROOTFS/etc/skel/." "$TARGET_ROOTFS/home/user/" fi chown -R 10000:10000 "$TARGET_ROOTFS/home/user" 2>/dev/null || true chmod 0600 "$TARGET_ROOTFS/etc/NetworkManager/system-connections/"*.nmconnection \ "$TARGET_ROOTFS/usr/lib/NetworkManager/system-connections/"*.nmconnection 2>/dev/null || true chown 0:0 "$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/"* 2>/dev/null || true chmod 0755 "$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/"* 2>/dev/null || true if [ ! -f "$TARGET_ROOTFS/etc/timezone" ] && [ -f "$TARGET_ROOTFS/usr/share/zoneinfo/UTC" ]; then printf 'UTC\n' > "$TARGET_ROOTFS/etc/timezone" ln -sfn /usr/share/zoneinfo/UTC "$TARGET_ROOTFS/etc/localtime" fi chmod 0640 "$TARGET_ROOTFS/etc/doas.d/"* 2>/dev/null || true echo "Applied Geminux overlay and @geminux APKs for $DEVICE."