#!/bin/sh
APP="/Applications/Install macOS Big Sur.app"
DMG="$APP/Contents/SharedSupport/SharedSupport.dmg"

[ -d "$APP" ] || { echo "Installer app missing. Run the .pkg again or use Internet Recovery (Opt+Cmd+R)."; exit 1; }
if [ ! -f "$DMG" ]; then
  echo "SharedSupport.dmg missing, the installer app is incomplete:"; ls -lh "$APP/Contents/SharedSupport/" 2>/dev/null; exit 1
fi
SZ=$(stat -f%z "$DMG")
[ "$SZ" -gt 10000000000 ] || { echo "SharedSupport.dmg is only $SZ bytes, too small. Incomplete install."; exit 1; }
echo "Installer looks complete (SharedSupport.dmg: $((SZ/1024/1024/1024)) GB)."

xattr -dr com.apple.quarantine "$APP" 2>/dev/null

if [ "$1" = "cli" ]; then
  echo "Starting command-line install. The Mac will restart by itself when it's ready."
  sudo "$APP/Contents/Resources/startosinstall" --agreetolicense --nointeraction
else
  echo "Opening the installer..."
  open "$APP"
fi
