#!/bin/sh

pulseaudio --check >/dev/null 2>&1
if [ "$?" = "0" ]; then
   pulseaudio --kill
fi

# If start-pulseaudio-x11 doesn't launch pulseaudio, we should
# wait the pulseaudio ready before run start-pulseaudio-x11
for i in $(seq 10); do
   sleep 1

   # Check pulseaudio process first, to avoid pactl triger new
   # pulseaudio process quickly.
   pulseaudio --check >/dev/null 2>&1
   if [ "$?" = "0" ]; then
      if [ -f "/usr/bin/pactl" ]; then
         /usr/bin/pactl info >/dev/null 2>&1 && break
      else
         break
      fi
   fi

   if [ "$i" = "10" ]; then
      echo "pulseaduio doesn't ready for start-pulseaudio-x11"
   fi
done

exec start-pulseaudio-x11
