#!/bin/ash
#(c) Copyright Barry Kauler Nov. 2010. License GPL v3 /usr/share/doc/legal.
#101119 complete rewrite.
#101121 variable delay, based on snd_ modules.
#101210 new path /tmp/pup_event_backend for files logged by /sbin/pup_event_backend_*.

#101121 wait for snd_ modules to complete loading...
#worst case is 6 seconds. best is 2. this sleep benefits all slow peripherals.
BRKCNT=1 ; GOODFLAG=0
while [ $BRKCNT -lt 7 ];do
 sleep 1
 echo "BRKCNT=${BRKCNT} SNDPATTERN='${SNDPATTERN}'" >> /tmp/rc_d_rc_services_log #for debugging.
 BRKCNT=$(($BRKCNT + 1))
 [ "`find /tmp/pup_event_backend -maxdepth 1 -type f -name 'pup_event_alsa_cnt*'`" = "" ] && continue #see /sbin/pup_event_backend_modprobe
 SNDPATTERN="`cat /tmp/pup_event_backend/pup_event_module_devpath_log* | grep '^MODULE=snd_.*DEVPATH' | cut -f 1 -d ' ' | cut -f 2 -d '=' | sort -u | tr '\n' ' ' | sed -e 's% $%%' -e 's% %.*%g'`" #see /sbin/pup_event_backend_modprobe
 #ex1: SNDPATTERN=emu10k1.*maestro3 (two snd modules). ex2: SNDPATTERN=snd_hda_intel
 if [ "`lsmod | grep -o '^snd_[^ ]*' | sort -u | tr '\n' ' ' | grep "$SNDPATTERN"`" != "" ];then
  BRKCNT=$(($BRKCNT + 2))
  GOODFLAG=1
 fi
done
[ $GOODFLAG -eq 1 ] && touch /tmp/rc_d_rc_services_alsa_ok #read by /usr/sbin/delayedrun, /etc/init.d/10alsa.

for service_script in /etc/init.d/*
do
 [ -x $service_script ] && $service_script start
done
unset service_script

###END###


