#!/bin/bash
# Quillio installer: runs as root after the files are copied (the "Install" step of the wizard).
# It protects the account that is logged in right now, so run the installer while logged in as the
# account that uses the guarded apps. Everything it does is written to /var/log/AcademicHub-install.log.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
HERE="$(cd "$(dirname "$0")" && pwd)"
LOG=/var/log/AcademicHub-install.log
exec >>"$LOG" 2>&1
echo
echo "=== Quillio install, $(date) ==="

USER_NAME="$(/usr/bin/stat -f%Su /dev/console 2>/dev/null)"
case "$USER_NAME" in
  ""|root|loginwindow)
    echo "No one is logged in at the screen, so there is no account to protect."
    exit 1 ;;
esac
USER_ID="$(/usr/bin/id -u "$USER_NAME")"
PORT="${ACADEMIC_HUB_PORT:-8765}"
echo "Protecting account: $USER_NAME (uid $USER_ID)"

AH_WHEELS="$HERE/wheels" AH_ALLOW_PYTHON_DOWNLOAD=1 /bin/bash "$HERE/install.sh" --system --user "$USER_NAME" --skip-pin --port "$PORT"
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
  echo "install.sh failed with status $STATUS"
  exit 1
fi

# Open the Quillio window for the user so the admin PIN can be created right away.
for _ in $(seq 1 30); do
  /usr/bin/curl -s -o /dev/null "http://127.0.0.1:$PORT/" && break
  sleep 1
done
# Build the Quillio app as a native AppleScript applet that starts the window through launchd.
# (An app that starts the Qt window straight from Finder could show a blank window on some Macs.)
# If anything here fails, the app from the package is kept; it uses the same open-window.sh.
make_finder_app() {
  APP="/Applications/Quillio.app"
  OPEN="/Library/Application Support/AcademicHub/bin/open-window.sh"
  [ -x "$OPEN" ] || { echo "open-window.sh missing; keeping the packaged app."; return 1; }
  [ -x /usr/bin/osacompile ] || { echo "osacompile missing; keeping the packaged app."; return 1; }
  W="$(/usr/bin/mktemp -d)" || return 1
  cat > "$W/launcher.applescript" <<'APPLESCRIPT'
on run
    do shell script "/bin/bash " & quoted form of "/Library/Application Support/AcademicHub/bin/open-window.sh"
end run
APPLESCRIPT
  /usr/bin/osacompile -o "$W/Quillio.app" "$W/launcher.applescript" || { /bin/rm -rf "$W"; return 1; }
  [ -f "$APP/Contents/Resources/AppIcon.icns" ] && /bin/cp "$APP/Contents/Resources/AppIcon.icns" "$W/Quillio.app/Contents/Resources/AppIcon.icns"
  P="$W/Quillio.app/Contents/Info.plist"
  setstr() { /usr/libexec/PlistBuddy -c "Set :$1 $2" "$P" >/dev/null 2>&1 || /usr/libexec/PlistBuddy -c "Add :$1 string $2" "$P"; }
  setstr CFBundleIdentifier "com.academichub.app"
  setstr CFBundleName "Quillio"
  setstr CFBundleDisplayName "Quillio"
  setstr CFBundleShortVersionString "1.0.0"
  setstr CFBundleVersion "1.0.0"
  setstr LSMinimumSystemVersion "12.0"
  setstr LSApplicationCategoryType "public.app-category.education"
  [ -f "$W/Quillio.app/Contents/Resources/AppIcon.icns" ] && setstr CFBundleIconFile "AppIcon.icns"
  EXE="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "$P")"
  /usr/bin/file "$W/Quillio.app/Contents/MacOS/$EXE" | /usr/bin/grep -q 'Mach-O' || { echo "The new app is not a native executable; keeping the packaged app."; /bin/rm -rf "$W"; return 1; }
  /usr/bin/plutil -lint "$P" >/dev/null || { /bin/rm -rf "$W"; return 1; }
  /bin/rm -rf "$APP"
  /usr/bin/ditto "$W/Quillio.app" "$APP"
  /usr/sbin/chown -R root:admin "$APP"
  /bin/chmod -R go-w "$APP"
  /usr/bin/codesign --force --deep --sign - "$APP" >/dev/null 2>&1 || true
  /bin/rm -rf "$W"
  echo "Built the Quillio app (AppleScript applet that starts the window through launchd)."
}
make_finder_app || echo "Using the packaged app."

# The app used to be called Academic Hub: remove the old app icons (the data is kept).
/bin/rm -rf "/Applications/Academic Hub.app" "/Applications/Uninstall Academic Hub.app" >/dev/null 2>&1
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$USER_NAME" /usr/bin/open -a "/Applications/Quillio.app" || true

echo "Done."
exit 0
