#!/usr/bin/env bash
BACKTRACEFILE=$(ls -t1 /tmp | grep "cathook-$USER-.*-segfault.log" |  head -n 1)
echo "$BACKTRACEFILE"

unknown=0

while read p; do
    out=$(addr2line $(echo $p | cut -f 1 -d " ") -pCe ./build/bin/libcathook.so)
    if [ "$out" != "??:0" ] && [ "$out" != "??:?" ]; then
        if [ $unknown -gt 0 ]; then
            echo "${unknown}x Unknown"
            unknown=0
        fi
        echo "$out"
    else
        unknown=$(($unknown + 1))
    fi
done <"/tmp/$BACKTRACEFILE"

if [ $unknown -gt 0 ]; then
    echo "${unknown}x Unknown"
fi
