#!/usr/bin/perl

$version="1.7.0";
# Author: Rainer Krienke, krienke@uni-koblenz.de
#
# Description:
#
# This script will create a linktree for all photos in a digikam
# database that have tags set on them. Tags are used in digikam to
# create virtual folders containing images that all have one or more tags.
# Since other programs do not know about these virtual folders this script
# maps these virtual folders into the filesystem by creating a directory
# for each tag name and then linking from the tag dir to the tagged image in the
# digikam photo directory.
#
# Call this script like:
# digitaglinktree -r /home/user/photos -l /home/user/photos/tags \
#            -d /home/user/photos/digikam.db


# Changes 
#
# 1.1->1.2:  
# Support for hierarchical tags was added. The script can either create
# a hierarchical directory structure for these tags (default) or treat them 
# as tags beeing on the same level resulting in a flat dir structure (-f)
#
# 1.2->1.3
# Added support for multiple tags assigned to one photo. Up to now only 
# one tag (the last one found) was used. 
#
# 1.3->1.4
# Bug fix, links with same name in one tag directory were no resolved
# which led to "file exists" errors when trying to create the symbolic link.
#
# 1.4-> 1.6, 2006/08/03
# Added an option (-A) to archive photos and tag structure in an extra directory 
# 1.6-> 1.6.1 2006/08/15
# Added an option (-C) for archive mode (-A). Added more information to 
# the manual page.
# 1.6.1-> 1.6.2 2008/11/24 (Peter Muehlenpfordt, muehlenp@gmx.de)
# Bug fix, subtags with the same name in different branches were merged
# into one directory.
# 1.6.2 -> 1.6.3 2008/11/25 Rainer Krienke, krienke@uni-koblenz.de
# Fixed a bug that shows in some later perl interpreter versions when 
# accessing array references with $# operator. If $r is a ref to an array
# then $#{@r} was ok in the past but now $#{r] is correct, perhaps it was
# always correct and the fact that $#{@r} worked was just good luck ....
# 1.6.3 -> 1.7.0 2008/11/27 Rainer Krienke, krienke@uni-koblenz.de
# Adapted script to handle digikams v0.10 new database scheme as well
# as the older ones. Version is determined automatically.


# ---------------------------------------------------------------------------
# LICENSE:
#
# 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, 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; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# ---------------------------------------------------------------------------


use Getopt::Std;
use File::Spec;
use File::Path;

# Please adapt the sqlite version to your digikam version.
# Do not forget to also install the correct sqlite version:
# Digikam 0.7x needs sqlite2 
# Digikam 0.8x needs sqlite3

#$SQLITE="/usr/bin/sqlite";
$SQLITE="/usr/bin/sqlite3";
$archiveDirPhotos="Photos";
$archiveDirLinks="Tags";

#
$scriptAuthor="krienke@uni-koblenz.de";	   

#
# check if sqlite can open the database
#
sub checkVersion{
    my($database)=shift;
    my(@data, $res, $version); 
    
    if( ! -r $SQLITE ){
       warn "Cannot start sqlite executable \"$SQLITE\". \n",
            "Please check if the executable searched is really installed.\n",
            "You should also check if the installed version is correct. The name\n",
	    "of the executable to search for can be set in the head of this script.\n\n";
       exit(1);    	     
    }
    
    @data=`$SQLITE $database .tables 2>&1`; # Try to get the tables information
    
    if( $? != 0 || grep(/Error:\s*file is encrypted/i, @data) != 0 ){
    	warn "Cannot open database \"$database\". Have you installed a current\n",
	     "version of sqlite? Depending on you digikam version you have to \n",
	     "install and use sqlite2 (for digikam 0.7x) or sqlite3 (for digikam >= 0.8 )\n",
	     "Please install the correct version and set the SQLITE variable in the\n",
	     "script head accordingly \n\n";
	exit(1);     
    } 
    $version="-1";
    $res=grep(/TagsTree/, @data);
    if( $res==0 ){
    	$version="7";	# digikam version 0.7
    }else{
    	$version="8";	# digikam version 0.8
    }
    # Now check if we have digikam 0.10
    $res=grep(/AlbumRoots/, @data);
 
    if( $res!=0 ){
    	$version="10";	# digikam version 0.10 KDE4
    }
    return($version);
}


#
# Get all images that have tags from digikam database
# refPathNames will contain all photos including path having tags 
# The key in refPathNames is the path, value is photos name
# rootdir is the relative album path in which photos are stored
# in digikam <0.10 this is the path given by the user via -r option
# starting with digikam 0.10 rootDir may be any of the entries of table 
# AlbumRoots column specificPath, albumId is the corresponding entry
# of column id.
#
sub getTaggedImages{
   my($refImg, $database, $rootDir, $albumId, $refPaths, $refPathNames, $vers)=@_;
   my($i, $image, $path, $tag, $status, $id, $pid, $tmp);
   my($sqliteCmd, @data, @tags, %tagPath, $tagCount, $refTag);
   
   
   # Get tag information from database
   $sqliteCmd="\"select id,pid,name from tags;\"";
   
   @data=`$SQLITE $database $sqliteCmd`;
   $status=$?;
   if( $status == 0 ){
      foreach $i ( @data ){
      	 chomp($i);
	 ($id, $pid, $tag)=split(/\|/, $i);
	 # map tag id data into array
	 $tags[$id]->{"tag"}="$tag";
	 $tags[$id]->{"pid"}="$pid";
      }
      
      # Now build up the path for tags having parents
      # Eg: a tag people might have a subtag friends: people
      #                                                     |->friends
      # We want to find out the complete path for the subtag. For friends this
      # would be the path "people/friends".  Images with tag friends would then be
      # linked in the directory <tagsroot>/people/friends/
	for($i=0; $i<=$#tags; $i++){
	 $pid=$tags[$i]->{"pid"};   # Get parent tag id of current tag
	 $tag=$tags[$i]->{"tag"};   # Start constructing tag path
         if( $pid == 0 ){
             $tagPath{$i}=$tag;
             next;
         }
	 
	 while( $pid != 0){
	    $tag=$tags[$pid]->{"tag"} . "/$tag"; # add parents tag name to path
	    $pid=$tags[$pid]->{"pid"};           # look if parent has another parent
	 }
	 # Store path constructed
	 $tagPath{$i}=$tag;
	 #warn "+ $tag \n";	 
      } 
   }else{
      print "Error running SQL-Command \"$sqliteCmd\" on database \"$database\"\n";   
   }
   
   
   if( $vers==7 ){
      # SQL to get all tagged images from digikam DB with names of tags 
      $sqliteCmd="\"select ImageTags.name,Albums.Url,ImageTags.tagid from " . 
           " ImageTags, Albums,Tags  " .
	   " where Albums.id = ImageTags.dirid; \"";
   
   }elsif( $vers == 8 ){
      # SQL to get all tagged images from digikam DB with names of tags 
      $sqliteCmd="\"select Images.name,Albums.url,ImageTags.tagid from"            .
               " Images,Albums,ImageTags where "                        .
	       " Images.dirid=Albums.id AND Images.id=ImageTags.imageid; \"";
   }elsif( $vers == 10 ){
      # SQL to get all tagged images from digikam DB with names of tags 
      # Only those images are considered that belong to the album in question
      $sqliteCmd="\"select Images.name,Albums.relativePath,ImageTags.tagid from" .
                         " Images,Albums,ImageTags " .
                         " where Images.album=Albums.id AND" .
                         " Images.id=ImageTags.imageid AND  " .
			 " Albums.albumRoot=$albumId \""
   }else{
   	warn "Unsupported digikam database version. Contact $scriptAuthor \n\n";
	exit(1);
   }   
   @data=`$SQLITE $database $sqliteCmd`;
   $status=$?;
   #print "Status: $status, AlbumId: $albumId\n";
   if( $status == 0 ){
      foreach $i ( @data ){
      	 chomp($i);
	 #print "$i, $albumId \n";
	 ($image, $path, $tag)=split(/\|/, $i);
	 $path=~s/\/$// if( $path =~ /\/$/ ); # remove a trailing /
	 $refPaths->{"$rootDir$path"}=1;
	 $refPathNames->{"$rootDir$path/$image"}=1;
	 
	 #print "-- $rootDir$path/$image \n";
	 
	 $tmp="$path";
	 #warn "- $rootDir, $path, $image \n";
	 # Enter all subpaths in $path as defined too
	 do{
          	$tmp=~s/\/[^\/]+$//;
		$refPaths->{"$rootDir$tmp"}=1;
		$tmp=~s/^\/$//;     # Finally delete a "/" standing alone
	 } while (length($tmp)); 
	 
	 $refImg->{"$rootDir/$path/$image"}->{"path"}=$path;
	 $refImg->{"$rootDir/$path/$image"}->{"image"}=$image;
	 $refImg->{"$rootDir/$path/$image"}->{"rootDir"}=$rootDir;
	 $refTag=$refImg->{"$rootDir/$path/$image"}->{"tag"};
	 # 
	 # One image can have several tags assigned. So we manage 
	 # a list of tags for each image
	 $tagCount=$#{$refTag}+1;	
         # The tags name
	 $refImg->{"$rootDir/$path/$image"}->{"tag"}->[$tagCount]=$tag;
         # tags path for sub(sub+)tags
	 $refImg->{"$rootDir/$path/$image"}->{"tagpath"}->[$tagCount]=$tagPath{$tag};                   

	 #print "$path/$image -> $tagPath($tag} \n";
      }
   }else{
      print "Error running SQL-Command \"$sqliteCmd\" on database \"$database\"\n";   
   } 
   return($status);
}


#
# Create a directory with tag-subdirectories containing links to photos having
# tags set. Links can be absolute or relative as well as hard or soft. 
#
sub createLinkTree{
    my($refImages)=shift;
    my($fakeRoot, $linktreeRootDir, $opt_flat, $opt_absolute, $linkMode)=@_;
    my( $i, $j, $cmd, $path, $image, $tag, $qpath, $qtag, $qimage, $linkName, 
        $tmp, $ret, $sourceDir, $destDir, $photoRootDir);
    my($qtagPath, $relPath, $count);

    if( -d "$linktreeRootDir" ){
    	# Remove old backuplinktree
	system("rm -rf ${linktreeRootDir}.bak");
	if( $? ){
	     die "Cannot run \"rm -rf ${linktreeRootDir}.bak\"\n";
	}
	
	# rename current tree to .bak 
	$ret=rename("$linktreeRootDir", "${linktreeRootDir}.bak" );
	if( !$ret ){
	     die "Cannot rename \"$linktreeRootDir\" to \"${linktreeRootDir}.bak\"\n";
	}
    }
	
    # Create new to level directory to hold linktree 	
    $ret=mkdir("$linktreeRootDir");
    if( !$ret ){
	 die "Cannot mkdir \"$linktreeRootDir\"\n";
    }
    # Iterate over all tagged images and create links
    $count=0;
    foreach $i (keys(%$refImages)){
	$path=$refImages->{$i}->{"path"};
	$image=$refImages->{$i}->{"image"};
	
	#print "-> $photoRootDir, $path, $image, $fakeRoot\n";
	if( ! length($fakeRoot) ){
	   $photoRootDir=$refImages->{$i}->{"rootDir"};
	}else{
	   $photoRootDir=$fakeRoot;
	}
	#$qimage=quotemeta($image);
	#$qpath=quotemeta($path);

	# Check that the images in the linktrees themselves are
	# ignored in the process of link creation. If we do not do this
	# we might get into trouble when the linktree root is inside the
	# digikam root dir and so the image links inside the liktree directory 
	# are indexed by digikam.
	 next if( -l "${photoRootDir}$path/$image" );
	# $refImages always contains all images from all albums. For Mode -A 
	# in digikam 0.10 it may happen that some albums are not on the same filesystem 
	# like the photoRoot. in this case this album is not cloned to another 
	# directory buth neverless
	# refImages contains also data about photos in this album. But since it is 
	# not cloned, the cloned root # does not exits and so the photo therin does 
	# not exist. Thats what we check here.
	next if( ! -r "${photoRootDir}$path/$image" ); 
        $count++;

        # Handle all of the tags for the current photo
	for( $j=0;  $j<=$#{$refImages->{$i}->{"tag"}}; $j++){	
	   $tag=$refImages->{$i}->{"tagpath"}->[$j];
	   #$qtagPath=quotemeta($tagPath);

	   # For tags that have subtags there is a path defined in qtagPath
	   # describing the parentrelationship as directory path like "friends/family/brothers"
	   # If it is defined we want to use this path instead of the flat tag name like "brothers"
	   # Doing so results in a directory hirachy that maps the tags parent relationships 
	   # into the filesystem.
	   if( $opt_flat ){
	   	# For flat option just use the last subdirectory
	   	$tag=~s/^.+\///;
	   }
	   # Create subdirectories needed
	   if( ! -d "${linktreeRootDir}/$tag" ){
		   $ret=mkpath("${linktreeRootDir}/$tag", 0, 0755);

		   if( !$ret ){
			die "Cannot mkdir \"${linktreeRootDir}/$tag\"\n";
		   }
	   }
	   # Avoid conflicts for images with the same name in one tag directory
	   $linkName=$image;
	   $tmp=$image;
	   while( -r "${linktreeRootDir}/$tag/$tmp" ){
		   $linkName="_" . $linkName;		
		   $tmp="_" . $tmp;		
	   }

	   if(length($opt_absolute)){
	      # Create link
	      $sourceDir="${photoRootDir}$path/$image";  
	      $destDir="${linktreeRootDir}/$tag/$linkName";		
	   }else{
	      # Get relative path from absolute one
	      # $rel=File::Spec->abs2rel( $path, $base ) ; 
	      $relPath="";
	      $relPath=File::Spec->abs2rel("${photoRootDir}$path", "${linktreeRootDir}/$tag" ) ;
	      if( !length($relPath)){
	         die "Cannot create relative path from \"${linktreeRootDir}/$tag\" to \"${photoRootDir}$path\" . Abort.\n";
	      }
	      # Create link
	      #print "-> $relPath\n";
	      $sourceDir="$relPath/$image"; 
	      $destDir="${linktreeRootDir}/$tag/$linkName";
	   }
	   
	   if( $linkMode eq "symbolic" ){
	      $ret=symlink($sourceDir, $destDir);
	      if( !$ret ){
		  $ret="$!";
		  warn "Warning: Failed symbolic linking \"$sourceDir\" to \"$destDir\": ",
		       "\"$ret\"\n";
	      }
	   }elsif( $linkMode eq "hard" ){
	      $ret=link($sourceDir, $destDir);
	      if( !$ret ){
		  $ret="$!";
		  warn "Warning: Failed hard linking \"$sourceDir\" to \"$destDir\": ",
		       "\"$ret\"\n";
	      }
	   }else{
	      die "$0: Illegal linkMode: \"$linkMode\". Abort.\n";
	   }
	   
	}# for
    }
    return($count);
}



#
# Clone a directory into another directory.
# source and destination have to exist
# Files in $srcDir  will symbolically or hard linked
# to the $cloneDir according to $cloneMode which can
# be symbolic for symbolic links or hard for hardlinks
# refPathNames contains path for all photos having tags set
#
sub cloneDir{
   local($srcDir, $cloneDir, $linkMode, $cloneMode, $refPaths, $refPathNames)=@_;
   local(@entries, $ki, $path, $name, $ret);

   # Read directory entries
   opendir( DIR, $srcDir ) || warn "Cannot read directory $srcDir \n";
   local(@entries)=readdir(DIR);
   closedir(DIR);

   foreach $k (@entries){
      next if( $k eq '.' );
      next if( $k eq '..');
      next if( $k =~ /digikam.*\.db/o );

      #warn "-> $srcDir, $k, $cloneDir\n";
      # Recurse into directories
      if( -d "$srcDir/$k" ){
         # if cloneMode is "minimal" then only clone directories and files
	 # with photos that have tags set.
	 if( $cloneMode ne "minimal" ||
     	               defined($refPaths->{"$srcDir/$k"}) ){ 
	     #warn "* $srcDir/$k defined \n";
	     mkdir("$cloneDir/$k", 0755);
  	     if( $? ){
		die "Cannot run \"mkdir $cloneDir/$k\"\n";
	     }
             cloneDir("$srcDir/$k", "$cloneDir/$k", $linkMode, 
	               $cloneMode, $refPaths, $refPathNames );
         }		    
      }else{
         # if cloneMode is "minimal" then only clone directories and files
	 # with photos that have tags set.
	 #print "+ refPathNames, $srcDir/$k , ",$refPathNames->{"$srcDir/$k"}, " \n";
	 if( $cloneMode ne "minimal" ||
     	               defined($refPathNames->{"$srcDir/$k"}) ){ 
	    # link files
	    if( $linkMode eq "symbolic" ){
               $ret=symlink( "$srcDir/$k",  "$cloneDir/$k");
	       if( !$ret ){
	           $ret="$!";
		   warn "Warning: In cloning failed symbolic linking \"$srcDir/$k\" to \"$cloneDir/$k\": ",
			"\"$ret\"\n";
	       }
	    }elsif( $linkMode eq "hard" ){
	       $ret=link( "$srcDir/$k",  "$cloneDir/$k");
	    
	       if( !$ret ){
	           $ret="$!";
		   warn "Warning: In cloning failed hard linking \"$srcDir/$k\" to \"$cloneDir/$k\": ",
			"\"$ret\"\n";
	       }
	    }else{
               die "$0: Illegal cloneLinkMode: $linkMode set. Aborting.\n";
	    }
	 }
      }
   }
}


#
# Create directory for cloning photos when running with -A 
#
sub createCloneDir{
    my( $cloneDir, $photoDir)=@_;
    my($ret);
    
    if( -d "$cloneDir" ){
    	# Remove old backuplinktree
	system("rm -rf ${cloneDir}.bak");
	if( $? ){
	     die "Cannot run \"rm -rf ${cloneDir}.bak\"\n";
	}
	
	# rename current tree to .bak 
	$ret=rename("$cloneDir", "${cloneDir}.bak" );
	if( !$ret ){
	     die "Cannot rename \"$cloneDir\" to \"${cloneDir}.bak\"\n";
	}
    }
    $ret=mkdir("$cloneDir", 0755);
    if( !$ret ){
	die "Cannot run \"mkdir $cloneDir\"\n";
    }
    $ret=mkdir("$cloneDir/$photoDir", 0755);
    if( !$ret ){
	die "Cannot run \"mkdir $cloneDir/$photoDir\"\n";
    }
}

#
# Manage cloning of a complete directory. If the clone directory exists
# a .bak will be created
# Files will be linked (hard/symbolic) according to cloneLinkMode which
# should be "symbolic" or "hard"
# cloneMode can "minimal" or anything else. minimal means only to clone 
# those files and directories that have tags set. Anything else means
# to clone the whole photo tree with all files
#
sub runClone{
   my($rootDir, $cloneDir, $photoDir, $cloneMode, $cloneLinkMode, 
      $refPaths, $refPathNames)=@_;
   my($ret);
   my($dev_r,$dev_A, $ino,$mode,$nlink,$uid,$gid,$rdev,$size,
      $atime,$mtime,$ctime,$blksize,$blocks);
   
    # Check if root and archive dir are on same filesystem
    ($dev_r,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks)       = stat($rootDir);
    ($dev_A,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks)       = stat($cloneDir);
    #
    if( $dev_r != $dev_A ){
	warn "Warning: \"$rootDir\" and \"$cloneDir\" are not on the same filesystem. Please select ",
	     "an archive directory on the same filesystem like digikams root directory! Skipped.\n";
	return(-1);     
    }	   

    # Create clone of all entries in $rootDir into $clonedir
    cloneDir($rootDir, "$cloneDir/$photoDir", $cloneLinkMode, 
             $cloneMode, $refPaths, $refPathNames);
    
    return(0);
}


#
# print out usage 
#
sub usage{
    print "                  -l <taglinkdir> | -A <archivedir> \n",
	  "                  -d <digikamdatabasefile> \n",
	  "                  [-r <photoroot>]  \n",	  
	  "                  [-H|-f|-a|-v|-C] \n",
	  "  -l <tagdir>  path to directory where the tag directory structure with \n",
	  "               links to the original images should be created \n",
	  "  -d <dbfile>  full path to the digikam database file \n",
          "  -r <rootdir> path to digikams root directory containing all photos. Not allowed\n",
	  "               for digikam vers >= 0.10. Mandatory for earlier digikam versions!\n",
	  "  -A <ardir>   Create selfcontained archive of photos and tags in directory\n",
	  "               ardir. rootdir and arDir have to be on the *same* filesystem!\n",
	  "  -C           If -A <archdir> was given this option will put hardlinks of all\n",
	  "               photos in the \"$archiveDirPhotos\" directory not only of those with tags.\n", 
	  "  -a           Create absolute symbolic links instead of relative ones \n",
	  "  -H           Use hard links instead of symbolic links in linktree. \n",
	  "  -f           If there are hierarchical tags (tags that have subtags) \n",
	  "               create a flat tag directory hierarchy. So directories for\n",
	  "               subtags are at the same directory level like their parent tags\n",
	  "  -h           Print this help \n",
	  "  -v           Print scripts version number \n";
     	
    print "Exit.\n\n";
    exit 1;
}
 
 
# 
# get Album roots if digikam is 0.10 or higher 
#
sub getAlbumRoots{
   my($database, $refAlbumRoots)=@_;
   my($ret, @data, $sql, $i, $id, $path);
 
   $sql="\"select id, specificPath from AlbumRoots\"";
   @data=`$SQLITE $database $sql  2>&1`; # Try to get the tables information
   for( $i=0; $i <= $#data; $i++) {
      chomp($data[$i]);
      ($id, $path)=split(/\|/, $data[$i] );
      $refAlbumRoots->{$id}=$path; # Enter Album Data
   }
   
   if( $? != 0 ){
       warn "$0: Cannot execute  $sql on database $database \n";
       exit(1);
   }

}
 
# ------------------------------------------------------------------
# main
# ------------------------------------------------------------------
$ret=getopts('HhCar:l:d:A:fv');

if( defined($opt_h) ){
        usage(0);
}

if( defined($opt_v) ){
    print "Version: $version\n";
    exit 0;    
}

usage if ((!length($opt_l) && ! length($opt_A)) 
           || !length($opt_d) );

$opt_f=0 if( ! length($opt_f) );

if( ! -f $opt_d ){
	print "Digikam database \"$opt_d\" not found. Exit.\n";
	exit 1;
}
$database=$opt_d;

# Remove trailing /
$opt_r=~s/\/$//;
$opt_l=~s/\/$//;
#
$rootDir=$opt_r;
$linkDir=$opt_l;

if( length($opt_C) ){
   $cloneMode="full";     # Clone all files/dirs
}else{
   $cloneMode="minimal";  # Only clone dirs and files haviong tags not all files
}

if( length($opt_H) ){
   $linkMode="hard";  # type of link from linktree to phototree
}else{
   $linkMode="symbolic";  # type of link from linktree to phototree
}

# Check digikam Database "version"
$vers=checkVersion($database); # Find out version of digikam 

if( ! -d $opt_r && $vers < 10 ){
	print "Invalid/empty digikam photoroot directory  \"$opt_r\" . Use -r to give correct one. Exit.\n";
	exit 1;
}

# For version .10 of digikam and newer get root albumpaths
$ret=0;
if( $vers >= 10 ){
    getAlbumRoots($database, \%albumRoots);
    # Now Process all albums found
    if( defined($opt_r) ){
       warn "\n$0: Hint: Album root definitions given by \"-r\" ignored \n",
            "for digikam version >= 0.10 . Instead all Albums defined in digikams \n",
	    "database are used. Continuing work ... \n\n";
    }
    foreach $i  (keys(%albumRoots)) {
      #print "Album roots -> $i:", $albumRoots{$i}, "\n";
      # Extract data needed from database
    
      $ret+=getTaggedImages(\%images, $database, $albumRoots{$i},  $i, \%paths, \%pathNames, $vers);
    }   
}else{
    $ret=getTaggedImages(\%images, $database, $opt_r, -1, \%paths, \%pathNames, $vers);
}

if( $ret != 0 ){
   warn "$0: Error looking up tag information in database. Exit.\n\n";
   exit 1;
}

# Handle Archiving photos and tag structure
# digikams photo dir will be cloned as a whole by using 
# either soft or hard links ($cloneMode)
$fakeRoot="";
if( length($opt_A) ){
    #    
    # Create a new empty directory for coling, rename old if existing
    $ret=createCloneDir($opt_A, $archiveDirPhotos);
    if( $vers >= 10 ){
       foreach $i  (keys(%albumRoots)) {
           #print "$i, $albumRoots{$i}\n";
	   $ret+=runClone($albumRoots{$i}, $opt_A, $archiveDirPhotos, $cloneMode, "hard", \%paths, \%pathNames);
       }

    }else{
       $ret=runClone($opt_r, $opt_A, $archiveDirPhotos, $cloneMode, "hard", \%paths, \%pathNames);
    
    }
    $fakeRoot="$opt_A/$archiveDirPhotos";
    $linkDir="$opt_A/$archiveDirLinks";
}

if( ( length($opt_l) || length($opt_A)) ){
    # When doing hard links we always use absolute linking
    if( $linkMode eq "hard" ){
	warn "Will use absolute hard links for linktree in archive mode...\n" if( !length($opt_a) ); 
        $opt_a="1";
    }	

    # Create the link trees for all tagged images
    $count=createLinkTree(\%images, $fakeRoot, $linkDir, $opt_f, $opt_a, $linkMode);	
    
    print "Processed $count photos. \n";
}
