#!/usr/bin/env bash

set -eu

# loggeduser=$(echo "show State:/Users/ConsoleUser" | scutil \
#   | awk '/Name :/ && ! /loginwindow/ { print $3 }')
#
# echo 'SELECT datetime(LSQuarantineTimeStamp + 978307200, "unixepoch") as LSQuarantineTimeStamp, ' \
#   'LSQuarantineAgentName, LSQuarantineOriginURLString, LSQuarantineDataURLString from LSQuarantineEvent;' \
#   | sqlite3 -init /dev/null /Users/"${loggeduser}"/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2

for db in ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV*; do
  if [ -r "${db}" ]; then
    # using .schema instead of .tables for better compatibility/clarity
    if sqlite3 -init /dev/null "${db}" ".schema LSQuarantineEvent" 2>/dev/null | grep -q 'CREATE TABLE'; then
      sqlite3 -init /dev/null "${db}" 'SELECT LSQuarantineDataURLString FROM LSQuarantineEvent'
    fi
  fi
done
exit 0
