#!/usr/bin/perl -w
# Copyright 2005 Jean-Michel Fayard jmfayard_at_gmail.com
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This program 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 program; see the file COPYING.  If not, write to
#   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.

# Changelog:
#	17/01/2005  Initial version


## =========== EDIT HERE =========================
# This script will create of filesystem of
# symlink that mirrors the way the browsing is
# done in KimDaBa in the following dir :
my $destfolder="/autre/Image::Kimdaba_FS" ;
## ===============================================
chdir $destfolder 
    or die "Edit $0 to indicate where you want to export KimDaBa FS";
    

use strict;
use diagnostics;
use Image::Kimdaba;

my $folder=getRootFolder();
parseDB( "$folder" );
    # parse the xml file and create two hashes:
    # %imageattributes	: HASH OF (url of the picture, REF. HASH OF (attribute, value) )
    # %imageoptions	: HASH of (url, REF. HASH OF (optoin, REF. LIST OF value) )

# File systems supports symlinks ?
my $symlink_exists = eval { symlink("",""); 1 };
unless ($symlink_exists) {
    die "Your system doesn't support symlink, aborting\n";
}

# First create needed directories
while( my ($option,$r_values) = each %alloptions)
{
    mkdir $option
	or die "Cannot write in directory $destfolder";
    mkdir "$option/$_" foreach @$r_values;
}

# Then do all symlinks
my $nb=0;
for my $url (keys %imageoptions)
{
    $nb++;
# do a somelink to $option/$value/$nb.jpg (or .png or whatever)
# to avoid conflicts where two pictures named "picture.png"
# have the same value
    (my $basename = $url) =~ s/^.*\./$nb./ ;
    my %options=%{ $imageoptions{$url} };
    while( my ($option, $r_values) = each %options )
    {
	symlink "$folder/$url", "$destfolder/$option/$_/$basename"
	    foreach @$r_values;
    }
}
