#! /bin/bash
#
# url.sh [url]
#
# Version 0.5
# 10.11.2021
########################################################

for session in $(screen -ls | grep -o '[0-9]\+\.mausdaemon_url')
do
  screen -S "${session}" -X quit
done

screen -d -m -S mausdaemon_url
sleep 1
echo "screen-session fuer URL gestartet."

DP=":0"
if [ -e "/etc/linuxmuster-client/maus/X11_last_user_display" ]; then
  DPx=$(cat /etc/linuxmuster-client/maus/X11_last_user_display)
  if [[ $DPx =~ ^:[0-3]$ ]]; then
    DP=$DPx
  fi
fi

screen -S mausdaemon_url -X stuff $"export DISPLAY=$DP\n"
BENUTZER=$(who -u | awk -v dp="$DP" '$2==dp { print $1 }' | head -1)
if [ -z "$BENUTZER" ]; then
  BENUTZER=$(who -u | awk '{print $1}' | head -1)
fi

screen -S mausdaemon_url -X stuff $"sudo -u $BENUTZER /usr/bin/firefox -new-tab '$1' & \n"
screen -S mausdaemon_url -X stuff $'exit\n'

exit 0

