#!/usr/bin/perl
#
# fixed and persistent naming for multiple soundcards, 
# based on which kernel name they have (see man udev)
#
# gmaruzz (at) celliax.org 
# nando (at) ccrma.stanford <dot> edu
#
# based on: http://alsa.opensrc.org/index.php/Udev
#
# This is to be executed by udev with the following rules:
# KERNEL=="controlC[0-9]*", DRIVERS=="sound", PROGRAM="/usr/bin/alsa_name %k", NAME="snd/%c{1}"
# KERNEL=="hwC[D0-9]*", DRIVERS=="sound", PROGRAM="/usr/bin/alsa_name %k", NAME="snd/%c{1}"
# KERNEL=="midiC[D0-9]*", DRIVERS=="sound", PROGRAM="/usr/bin/alsa_name %k", NAME="snd/%c{1}"
# KERNEL=="pcmC[D0-9cp]*", DRIVERS=="sound", PROGRAM="/usr/bin/alsa_name %k", NAME="snd/%c{1}"
#
use strict;
use warnings;

# udev called us with this argument (%k)
my $alsaname = $ARGV[0];
# udev put this in our environment
my $physdevpath = $ENV{PHYSDEVPATH};
my $alsanum = "";

# you can find the physdevpath of a device with "udevinfo -a -p $(udevinfo -q path -n /dev/snd/pcmC0D0c)"

# eliminate until last slash
$physdevpath =~ s/.*\/([^\/]*)/$1/;
# eliminate until first colon
$physdevpath =~ s/[^:]*:(.*)/$1/;

# Delta 66 (found with /sbin/lspci)
if($physdevpath eq "05:01.0") {
    $alsanum="0";
}
# Intel HDA Asus P5K/EPU
if($physdevpath eq "00:1b.0") {
    $alsanum="1";
}

# other bus positions don't change....
if($alsanum ne "") {
    $alsaname=~ s/(.*)C([0-9]+)(.*)/$1C$alsanum$3/;
}
print $alsaname;
exit 0;
