#!/bin/csh
# Sanity check:
test -x /usr/bin/daemon
if ( $status != 0 ) then
  exit 0
endif
test -x /usr/bin/wireplumber
if ( $status != 0 ) then
  exit 0
endif
test -x /usr/bin/pipewire-pulse
if ( $status != 0 ) then
  exit 0
endif
test -x /usr/bin/pipewire
if ( $status != 0 ) then
  exit 0
endif

# Enable pipewire for use on the console without X or Wayland:
loginctl | grep -q " $USER \+seat"
if ( $status == 0 ) then
  if ! { daemon --pidfiles=~/.run --name=wireplumber --running } then
    daemon -rB --pidfiles=~/.run --name=wireplumber /usr/bin/wireplumber
  endif
  if ! { daemon --pidfiles=~/.run --name=pipewire-pulse --running } then
    daemon -rB --pidfiles=~/.run --name=pipewire-pulse /usr/bin/pipewire-pulse
  endif
  if ! { daemon --pidfiles=~/.run --name=pipewire --running } then
    daemon -rB --pidfiles=~/.run --name=pipewire /usr/bin/pipewire
  endif
endif
