#!/usr/bin/perl
#
# Version 0.1 vom 04.02.2007
#
# Author: Mike Constabel Portal: <vejoun @ vdrportal>
#                         eMail: <vejoun @ toppoint . de>
#
# README:
# -------
#
# Change the vars below to reflect your needs and switch $isconfigured to 1
#
#

use strict;
use warnings;
use File::Basename;
use Fcntl qw(:DEFAULT :flock);

#################
# Configuration #
#################

my @SearchDirs = ("/video1.0/Musik/Mitschnitte/Retro_Charts/", "/video1.0/Musik/Mitschnitte/Charts/", "/video1.0/Musik/Mitschnitte/DM/");

my $ArtistDir = "/video1.0/Musik/By_Artist/";
my $TitleDir  = "/video1.0/Musik/By_Titles/";

my $isconfigured	= 0;

##################
# /Configuration #
##################

my $Usage = qq{
This scripts reads the video-clip-files under the pathes stored in \@SearchDirs
and create sorted lists under \$ArtistDir and \$TitleDir.

The files must follow the format "Artist - Title" as created per default by
the vdr-plugin deluxe.

Changes the Config-Vars inside this script and switch \$isconfigured to 1.

The default ist now:

Search in:
@SearchDirs

Write below:
$ArtistDir
$TitleDir

Please call

rm -rf $ArtistDir
rm -rf $TitleDir

before if necessary. Be careful while using rm -rf!

Don't forget to "touch /video1.0/.update" after calling this script.

};

unless ( $isconfigured ) {
  print $Usage;
  exit;
}

mkdir($ArtistDir);# || print("error $!");
mkdir($TitleDir);# || print("error $!");

foreach ( @SearchDirs ) {
  my $SearchDir = $_;

  my @Records;

  next if ( ! $SearchDir && ! -d $SearchDir );

  my @Alls = glob $SearchDir."*";

  foreach ( @Alls ) {
    my $tmp = $_;
    if ( -d $tmp && $tmp =~ /\/%/ && $tmp !~ /\w_\d\d\.\d\d\.\d\d\d\d/ ) {
      $tmp =~ s/^.*\/%//;
      push @Records, $tmp;
    }
  }

  foreach ( @Records ) {
    my $target = $_;
    
    my @AllThisArtist = glob $SearchDir."%".$target."/*";

    my ($artist, $title) = split(/_-_/, $target);
    
    foreach ( @AllThisArtist ) {
      my $code = $_;
      $code =~ s/^.*\///;
    
      if ( $artist && $title && $code ) {

        my $partist = $artist;
        my $ptitle	 = $title;
        $partist =~ s/_/ /g;
        $ptitle  =~ s/_/ /g;
        $partist = substr($partist, 0, 39);
        $ptitle  = substr($ptitle, 0, 39);
        printf("%-40s %-40s %s\n", $partist, $ptitle, $code);

        mkdir($ArtistDir.$artist);# || print("error $!");
        mkdir($ArtistDir.$artist."/".$title);# || print("error $!");
        mkdir($TitleDir.$title);# || print("error $!");

        symlink($SearchDir."%".$target."/".$code, $ArtistDir.$artist."/".$title."/".$code );# || print("error $!\n");
        symlink($SearchDir."%".$target."/".$code, $TitleDir.$title."/".$code );# || print("error $!\n");
      }
    }
  }
}

__END__
