#!/bin/bash

# 1. Mount debugfs on /sys/kernel/debug
# 2. Run the script on the board
# 3. Copy/paste the script output in a text file on our workstation
# 4. On our workstation, run dot -Tpdf -o output.pdf myfile.txt
# 'dot' is usually part of the 'graphviz' package

echo 'digraph {'
OIFS=$IFS
IFS=$(echo -ne "\n")

# Edit this line to match your board
card=/sys/kernel/debug/asoc/sun8i-a33-audio
widgets="${card}"/*/dapm/*
card_widgets="${card}"/dapm/*

for w in $card_widgets $widgets; do
	name=`basename "$w"`
	path=`dirname "$w"`
	path=`dirname "$path"`
	if [ "$name" = "bias_level" ]; then
		continue
	fi
	if [ "$w" != "${card}/dapm/${name}" ]; then
		name="`basename ${path}`/${name}"
	fi
        echo -n \"${name}\"
        cat $w | grep ':' | grep -q '\<On\>' > /dev/null
        if [ $? -eq 0 ]; then
                echo " [style=\"filled\" fillcolor=\"green\"]"
        else
                echo " [style=\"filled\" fillcolor=\"red\"]"
        fi
        b=`cat $w | grep -v ':' | grep -E "^[[:space:]]+out"`
        for io in $card_widgets $widgets; do
                io_name=`basename "$io"`
		io_path=`dirname "$io"`
		io_path=`dirname "$io_path"`
		if [ "$io" != "${card}/dapm/${io_name}" ]; then
			io_name="`basename ${io_path}`/${io_name}"
		fi
                echo "$b" | grep -q "\"`basename "$io"`\"\$" > /dev/null
                if [ $? -eq 0 ]; then
                        echo \"$name\" '->' \"$io_name\"
                fi
        done
done
echo '}'
IFS=$OIFS
