#!/bin/sh
# PID 1 for the TinyEMU Alpine guest. Mounts the 9P control channel and starts ultictl.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev 2>/dev/null || true
mkdir -p /dev/pts /mnt/ultictl /workspace /tmp /var/log /root
mount -t devpts devpts /dev/pts 2>/dev/null || true
hostname ulti 2>/dev/null || true
# Cert validity for apk over HTTPS. The virt machine has no wall-clock RTC.
date -u -s "2026-10-04 03:00:00" >/dev/null 2>&1 || true

i=0
while [ "$i" -lt 50 ]; do
  if mount -t 9p -o trans=virtio,version=9p2000.L ultictl /mnt/ultictl 2>/tmp/ultictl.mount; then
    break
  fi
  i=$((i + 1))
  sleep 0.2
done
mount -t 9p -o trans=virtio,version=9p2000.L workspace /workspace 2>/dev/null || true
if [ -x /usr/bin/ultictl ]; then
  /usr/bin/ultictl -dir /mnt/ultictl agent >/var/log/ultictl.log 2>&1 &
fi

# The virtio console is a tmux client attached to session "user".
# Bot screens are other sessions; term.screen runs switch-client.
# Take over keystrokes are written to this console, so they show up here.
export TERM=xterm
if [ -x /usr/bin/tmux ] && [ -c /dev/hvc0 ]; then
  exec /usr/bin/tmux -f /etc/tmux.conf new-session -A -s user <>/dev/hvc0 >&0 2>&1
fi
exec getty -L 115200 hvc0
