#!/bin/bash
# 
# Autor: Julian Forster (www.ProgMaschine.de.vu)
# Autor: markus schnalke (prog.marmaro.de)
#


if [ $# -eq 1 ]; then
  if [ $1 = '--version' ] ; then
    echo 'Buddylistgrapher'
    echo 'shellscript edition'
    echo 'version 0.1'
    exit 0
  else

    file=/tmp/`basename $0`-$$
    file2=/tmp/`basename $0`-$$-2

    # extract all needed lines
    grep -e "Profil von:" -e "Nachricht schreiben" $1 > $file

    # collect all profil names (we only want them as nodes)
    cat $file | while read line ; do
      echo -n "`echo $line | grep "Profil von:" | awk '{print " " $3 " "}'`" >> $file2
    done
    echo >> $file2

#cat $file2
#exit 


    # output
    echo "digraph G {"
    
    cat $file | while read line ; do
      isProfil=`echo $line | grep "Profil von:"`
      if [ -n "$isProfil" ] ; then
        name=$(echo $line |  awk '{ print $3 }')
      else
        buddy=$(echo $line |  awk '{ print $3 }')
        if [ -n "`grep -F " $buddy " $file2`" ] ; then
          echo "  \"$name\" -> \"$buddy\";"
        fi
      fi    
    done

    echo "}"

    #rm $file $file2
    exit 0

  fi
else
    echo "usage: $0 <input.txt>"
    exit 1
fi
