#!/bin/sh

lsdvb_pci() {
    ls -d /sys/class/dvb/*.frontend0 | xargs readlink | sed -e 's/\/dvb.*$//' -e 's/^.*pci//' | sort | uniq
}

lsdvb_adapter() {
    lsdvb_pci | while read PCI
    do
        cd "/sys/devices/pci$PCI/dvb"
        ls -d *.frontend0 | sed -e 's/^.*dvb//' -e 's/\..*$//' | sort -g
    done
}

lsdvb_enumerate() {
    lsdvb_adapter | awk '{printf "%s\t%d\n", $0, (NR-1);}'
}

lsdvb_enumerate | while read FROM TO
do
    if [ "x$FROM" != "x$TO" ] ; then
        mv /dev/dvb/adapter$FROM /dev/dvb/adapter-$FROM-$TO
    fi
done

ls -d /dev/dvb/adapter-* | tr '-' '\t' | while read T FROM TO
do
    echo "adapter$FROM => adapter$TO"
    mv /dev/dvb/adapter-$FROM-$TO /dev/dvb/adapter$TO
done
