#!/bin/bash
# Check for user presence at console by checking for screen lock and console login status
# Thanks to https://stackoverflow.com/users/908494/abarnert at
#    https://stackoverflow.com/a/11511419/4326287

# Wait until console is logged in and screen is unlocked
while true ;
do
  timestamp=$(date +%Y-%m-%dT%H:%M:%S%z)
  python -c 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); sys.exit(d and d.get("CGSSessionScreenIsLocked", 0) == 0 and d.get("kCGSSessionOnConsoleKey", 0) == 1)'
  if [ $? != 0 ] ; then
    echo "$timestamp [userPresence.sh] User presence detected"
    break
  else
    echo "$timestamp [userPresence.sh] Screen is locked or no console user is logged in"
  fi
  sleep 10
done