#! /bin/bash
#
# url.sh [url]
#
# Version 0.71
# fuer linuxclient 7
# 27.10.2023
########################################################

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-maus/X11_last_user_display" ]; then
  DPx=$(cat /etc/linuxmuster-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

if [ -e "/tmp/maussk_env_login" ]; then
  screen -S mausdaemon_url -X stuff $"export $(cat /tmp/maussk_env_login | xargs)\n"
fi
screen -S mausdaemon_url -X stuff $"sudo -u $BENUTZER /usr/bin/firefox -P default --new-tab '$1' & \n"
screen -S mausdaemon_url -X stuff $"exit\n"

exit 0


