#!/bin/bash
# One-shot: set reTerminal DM (labwc/Wayland) rotation to Left via wlr-randr, then remove self.
# Runs once as user pi at first login; deletes its autostart and this script so it never runs again.
# Logs to /var/log/first-boot.log.
FIRST_BOOT_LOG="/var/log/first-boot.log"
log() { echo "[$(date -Iseconds)] [set-rotation-once] $*" >> "$FIRST_BOOT_LOG" 2>/dev/null || true; }

log "started (labwc/wlr-randr)"
log "waiting 5s for compositor ..."
sleep 5

OUTPUT=""
if command -v wlr-randr &>/dev/null; then
  OUTPUT=$(wlr-randr 2>/dev/null | awk '/^[A-Za-z0-9_-]+ /{print $1; exit}')
fi
if [[ -z "$OUTPUT" ]]; then
  OUTPUT="DSI-1"
  log "using default output: $OUTPUT"
fi

if [[ -n "$OUTPUT" ]] && command -v wlr-randr &>/dev/null; then
  log "applying rotation left (transform 270) on $OUTPUT"
  wlr-randr --output "$OUTPUT" --transform 270 2>&1 | while read -r line; do log "$line"; done
else
  log "WARNING: wlr-randr not found or no output"
fi

log "removing one-shot desktop and script"
rm -f /home/pi/.config/autostart/set-rotation-once.desktop /home/pi/set-rotation-once.sh
log "finished"
