#!/bin/bash

INITSKIP="false"

#first init check for pure systemd, no shim
INITCHECK=$(readlink /usr/sbin/init)
echo $INITCHECK
if [ "$INITCHECK" = "/lib/systemd/systemd" ]; then
        INITSKIP="true"
fi

#second init check for system-shim configuration
INITCHECK=$(/usr/bin/ps -p 1 -o cmd -h)
echo $INITCHECK
if [ "$INITCHECK" = "/lib/systemd/systemd" ]; then 
	  INITSKIP="true"
fi

#else start them up
echo "start pipewire"

if [ "$INITSKIP" = "false" ]; then
	#kill existing servers per user
	if [ -n "$(pgrep -x -u $USER pipewire)" ]; then
		pkill -x -u $USER pipewire
	fi
	if [ -n "$(pgrep -x -u $USER pipewire-pulse)" ]; then
		pkill -x -u $USER pipewire-pulse
	fi
	if [ -n "$(pgrep -x -u $USER wireplumber)" ]; then
    	pkill -u $USER wireplumber
	fi
	#start up new servers
	/usr/bin/pipewire 2>/dev/null &
 	/usr/bin/pipewire-pulse &
	/usr/bin/wireplumber 2>/dev/null &
fi

exit 0

