#!/usr/bin/perl

#      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
#
#  This file is part of RoarAudio PlayList Daemon,
#  a playlist management daemon for RoarAudio.
#  See README for details.
#
#  This file is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 3
#  as published by the Free Software Foundation.
#
#  RoarAudio is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this software; see the file COPYING.  If not, write to
#  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

use strict;
use Audio::RPLD;

my $level = 0;

while (($_ = shift(@ARGV))) {
 if ( $_ eq '--tree' ) {
  $level =  0;
 } elsif ( $_ eq '--flat' ) {
  $level = -1;
 } else {
  die 'Unknown option: '.$_;
 }
}

my $rpld = Audio::RPLD->new();

$rpld->connect_default();
$rpld->is_connected() or die;

my $res = $rpld->listplaylists() or die;

$rpld->disconnect();

proc($res, $level, 1);

sub proc {
 my ($lists, $level, $nocircular) = @_;
 my $c;
 my $prefix = '';

 $level ||= 0;

 unless ( $level < 0 ) {
  $prefix = ' ' x ($level * 2);
 }

 foreach $c (@{$_[0]}) {
  if ( $c->{'processed'} ) {
   #printf("%s[%-8i]: %s ***circular parentship***\n", $prefix, $c->{'id'}, $c->{'name'}) unless $nocircular;
   printf("%s%i: %s ***circular parentship***\n", $prefix, $c->{'id'}, $c->{'name'}) unless $nocircular;
   next;
  }
  #printf("%s[%-8i]: %s\n", $prefix, $c->{'id'}, $c->{'name'});
  printf("%s%i: %s\n", $prefix, $c->{'id'}, $c->{'name'});
  $c->{'processed'} = 1;
  proc($c->{'children'}, $level + 1, 0) unless $level < 0;
 }
}

#ll
