THE SCRIPT:

Code:
--------------------
    #!/bin/sh
  
  # Script to be used by udev rules that detect when a USB DAC is connected or 
disconnected,
  # in order to start or stop Squeezelite
  # $1 : is the script option: 'restart', 'stop', 'find' or 'install'
  # $2 : is the (CARD)name of the DAC, as used in the squeezelite command 
string.
  #      Used to make card-specific $kernel name file (/tmp/$cardname.kername),
  #      and card-specific rules file (/etc/udev/rules.d/10-$cardname.rules).
  #      Not used by 'install', since cardname is extracted from 
/usr/local/etc/pcp/pcp.cfg
  # $3 : Only used for 'restart' and 'stop'.
  #      $3 is the kernel name ($kernel) that is generated by the udev event
  #      It is used to match a 'removed' USB device against the device assigned 
when the DAC was inserted
  
  # The 'find' option is used at boot to detect which device the DAC is 
connected to, 
  # in the case that the DAC is up before squeezelite attempts to start.
  # The 'install' option creates the udev rules file and adds it to 
/opt/.filetool.lst,
  # so that it is included in the user's backup.  It also inserts the startup 
command into /opt/bootlocal.sh
  
  # Examples:
  # SQLITE-control install DAC
  # This will create a rules file in /etc/udev/rules.d and insert the startup 
command into /opt/bootlocal.sh
  # The rule number is curently fixed at 10, and the rule name will equal the 
card name; in this case 'DAC'
  # Run this once, manually from the command line.
  
  # SQLITE-control find DAC
  # This will check whether the DAC is connected, and write its kernel id to 
/tmp/DAC_kername.txt
  # This is intended to be used as a User Command, run at boot, so that the 
'stop' option knows which kernel id to look for.
  
  # SQLITE-control restart $kernel and SQLITE-control stop $kernel
  # (Re)start and stop squeezelite, respectively.
  # These two options are called by the udev rules in response to 'add' and 
'remove' events
  # They take the $kernel argument that is available when the udev rule is 
triggered.
  
  # The associated udev rules are of the form:
  # SUBSYSTEM=="usb", ACTION=="add", ATTR{idVendor}=="08bb", 
ATTR{idProduct}=="2704", RUN+="/home/tc/SQLITE-control.sh restart DAC $kernel"
  # SUBSYSTEM=="usb", ACTION=="remove", RUN+="/home/tc/SQLITE-control.sh stop 
DAC $kernel"
  
  SQLITErestart() {
        local logfile=$1
        local attempts=5 # number of tries
        local interval=1 # interval between tries (seconds)
        
        local uptime
        local count=$attempts 
        while [ "$(sudo /usr/local/etc/init.d/squeezelite status)" == 
"Squeezelite not running." ]; do
                if [ $((count--)) -le 0 ]; then
                        uptime=$(printf [%12s $(cat /proc/uptime | awk {'print 
$1'})])
                        echo  "$uptime Squeezelite failed to initialize within 
$attempts attempts." >> $logfile
                        exit 1 
                fi
  
                uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})])
                echo  "$uptime Attempting to start squeezelite" >> $logfile
                sudo /usr/local/etc/init.d/squeezelite restart >> $logfile
                sleep $interval
        done
  
        # determine cardname from squeezelite process id
        local PID=$(sudo /usr/local/etc/init.d/squeezelite status | awk -F PID= 
{'print $2'})
        local cardname=$(ps | grep $PID | awk -F CARD= {'print $2'} | awk -F , 
{'print $1'})
        sudo /usr/local/sbin/alsactl restore $cardname
  }
  
  cardname=$2
  
  logfile=/var/log/pcp_$cardname.log
  
  case $1 in
        restart ) # restart squeezelite when DAC is inserted/powered up
                # output the device '$kernel' name to a text file, so that it 
can be used to check against USB 'remove' events
                kername="$3"
                uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})])
                if [ -z $kername ]; then
                        echo "$uptime $cardname not detected" >> $logfile
                else
                        echo "$uptime $cardname detected on $kername" >> 
$logfile
                        echo $kername > /tmp/$cardname.kername
                        SQLITErestart $logfile &
                fi
                ;;
        stop ) # stop squeezelite when DAC is removed/powered down
                # compare $kernel device name to the one created by this script 
when the DAC was inserted/powered up
                startname=$(cat /tmp/$cardname.kername)
                kername="$3"
                if [ $kername == $startname ]; then # stop squeezelite if names 
match
                        uptime=$(printf [%12s $(cat /proc/uptime | awk {'print 
$1'})])
                        echo "$uptime $cardname on $kername removed" >> $logfile
                        sudo /usr/local/etc/init.d/squeezelite stop >> $logfile
                fi
                ;;
        find ) # create the file with the kernel name
                # find Vendor and Product IDs for the DAC from the udev rules
                # name the rules file after the (CARD)name of the DAC
                rulesfile=etc/udev/rules.d/10-$cardname.rules
                idVendor=$(cat /$rulesfile | grep idVendor | awk -F idVendor 
{'print $2'} | awk -F \" {'print $2'})
                idProduct=$(cat /$rulesfile | grep idProduct | awk -F idProduct 
{'print $2'} | awk -F \" {'print $2'})
                uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})])
                echo "$uptime Searching for $cardname with idVendor=$idVendor 
and idProduct=$idProduct in dmesg" >> $logfile
                kername=$(dmesg | grep -m 1 "idVendor=$idVendor, 
idProduct=$idProduct" | awk -F usb {'print $2'} | awk -F : {'print $1'})
                # output the $kernel device name to a file for use when 
detecting 'removed' USB devices
                echo $kername > /tmp/$cardname.kername
                uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})])
                if [ -z $kername ]; then
                        echo "$uptime $cardname not detected" >> $logfile
                else
                        echo "$uptime $cardname detected on $kername" >> 
$logfile
                        sudo /usr/local/sbin/alsactl restore
                fi
                ;;
        install ) # make the rules file
                # one command line parameter: the name of the 'CARD', as used 
in the squeezelite command line
                
                scriptfile=$(realpath $0)
                
                # find cardname from /usr/local/etc/pcp/pcp.cfg
                cardname=$(cat /usr/local/etc/pcp/pcp.cfg | grep ^OUTPUT | awk 
-FCARD= {'print $2'} | awk -F, {'print $1'})
                echo "Squeezelite output device found in 
/usr/local/etc/pcp/pcp.cfg = $cardname"
  
                # look for cardname in /proc/asound/cards to determine card 
number
                cardno=$(cat /proc/asound/cards | grep -m 1 $cardname | awk 
{'print $1'})
                echo "$cardname found on card$cardno"
  
                # extract idVendor and idProduct from /proc/asound/card<n>/usbid
                usbid=$(cat /proc/asound/card$cardno/usbid)
                idVendor=$(echo $usbid | awk -F: {'print $1'})
                idProduct=$(echo $usbid | awk -F: {'print $2'})
                echo "$cardname idVendor = $idVendor and idProduct = $idProduct"
  
                # create udev rules
                rule_add='SUBSYSTEM=="usb", ACTION=="add", 
ATTR{idVendor}=="'$idVendor'", ATTR{idProduct}=="'$idProduct'", 
RUN+="'$scriptfile' restart '$cardname' $kernel"'
                rule_remove='SUBSYSTEM=="usb", ACTION=="remove", 
RUN+="'$scriptfile' stop '$cardname' $kernel"'
  
                # create rules file
                rulesfile=etc/udev/rules.d/10-$cardname.rules
                echo $rule_add > /$rulesfile
                echo $rule_remove >> /$rulesfile
                echo "udev rules written to $rulesfile"
  
                # add rules file to /opt/.filetool.lst
                sed -i "\#$rulesfile#d" /opt/.filetool.lst #remove all previous 
matching entries, to make sure it's only listed once
                echo $rulesfile >> /opt/.filetool.lst
                echo "$rulesfile added to /opt/.filetool.lst"
                
                # add SQLITE-control.sh to /opt/bootlocal.sh
                # delete first so that lines only appear once
                sed -i "\#$(basename $0)#d" /opt/bootlocal.sh # delete lines 
that mention this script, so that lines only appear once
                sed -i "3 a$scriptfile find $cardname &" /opt/bootlocal.sh
                sed -i "3 aecho \"Starting $(basename $0) from 
/opt/bootlocal.sh...\" | tee -a /var/log/pcp_boot.log" /opt/bootlocal.sh
                echo 
                echo "Installation complete"
                echo "Squeezelite is now set up to autostart/stop with 
$cardname power"
                echo "Backup and reboot ('pcp br') to enable"
                ;;
  esac
--------------------


------------------------------------------------------------------------
chill's Profile: http://forums.slimdevices.com/member.php?userid=10839
View this thread: http://forums.slimdevices.com/showthread.php?t=113661

_______________________________________________
unix mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/unix

Reply via email to