#!/bin/bash

tune_dot() {
	font="Ubuntu";
	awk '{print} (NR==1) { print "\tbgcolor=\"transparent\";\n\tnode[style=\"filled\", fillcolor=\"white\", fontname=\"'"$font"'\"];\n\tedge[fontname=\"'"$font"'\"];";}' | sed 's/setlinewidth/filled,setlinewidth/';
}

generate_graph() {
	debtree "$@" | tee "$name.dot" | tune_dot | dot -Tpng > "$name.png";
	[[ -f ../image-comment.sh ]] && ../image-comment.sh "$name.png" "$(basename $(realpath .)) | generated at: $(lsb_release -d --short) using: debtree $*";
	cat "$name.dot" | grep -oE '"[^<("]+"' | sort -u > "$name.txt";
}

generate_graphs() {
	name="${1}_debtree_default"      generate_graph "$1";
	name="${1}_debtree_minimal"      generate_graph --no-provides --no-recommends --no-conflicts --no-alternatives "$1"
	name="${1}_debtree_alternatives" generate_graph --no-provides --no-recommends --no-conflicts "$1"
	name="${1}_debtree_build"        generate_graph --build-dep "$1";
}

while read package; do
	generate_graphs "$package";
done
