#!/bin/bash

#echo "check active user"
#is user running this script logged in at the workstation or remotely?

user=`whoami`

loggedList=`last -i -w | grep "still logged in" | grep $user | awk '{ print $3 }' | grep "0.0.0.0" `

#echo $loggedList

if [ -n "$loggedList" ]; then
    #   user logged in directly at workstation
    #   start pulseaudio if not already running
    paprocesses=`pgrep pulseaudio | awk '{ printf "%s", $1 }' `

    if [ -z $paprocesses ]; then
    #   echo "starting pulseaudio"
       pulseaudio --daemonize
    #else
    #   pulseaudio already running
    #   echo process id for pulseaudio:  $paprocesses
    fi
   # report user logged in directly and pulseaudio running
   echo ok
else
   # report user logged in remotely and should be denied access
   echo no
fi

