#!/usr/bin/perl
#
#########################################################################
#
# Image Display System Updater 				5/23/2001     
# 			Clayton Hynfield			weefle@users.sourceforge.net
#
#########################################################################
# Use this script update from the directory structure of IDS 0.41-0.52 
# installations to that used by IDS 0.711 
#
# Please see the files "LICENSE," "README," "INSTALL," and "UPGRADE" for 
# more information.
#
#########################################################################
# 
# Copyright (C) 2001  Clayton Hynfield
# 
# 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; if not, write to:
# 
#	Free Software Foundation, Inc.
#	59 Temple Place - Suite 330
#	Boston, MA  02111-1307, USA.
# 
#########################################################################

# Change these if you use different names:
my $dirToSearch = './albums/';

#########################################################################


use File::Basename;
use File::Find;
use File::Path;
use Image::Magick;
use strict qw(vars);
use lib qw(./);
use idsShared;

my $debug = 0;
my ($file, $newFile, @albumIcons, @descriptions, @albumDescriptions, @imageDescriptions);

readPreferences('./ids.conf');

print "\n### IDS Updater ###\n\nThis script will upgrade the directory structure of IDS 0.41-0.52\n" 
."installations to that used by IDS 0.711. The directory \"$dirToSearch\" will\n"
."be recursively searched for image descriptions and album descriptions.\n"
."These files will be moved to the \"album-data\" directory and renamed. Old\n"
."album icons will be deleted."
."\n\nEnter \"y\" to continue or any other key to quit: ";

my $choice = <STDIN>;
chomp $choice;
exit unless ($choice eq 'y');



print "\n##########\n\nLocating description and album icon files in \"$dirToSearch\"...\n";
find (\&findFilesToMove, $dirToSearch);

determineDescType();

foreach $file ( @albumIcons ) {
	unless ( unlink $file ) {
		print STDERR "WARNING: Unable to delete album icon \"$file\": $!\n";
	} else {
		print "Deleted album icon \"$file\"\n";
	}
}

foreach $file ( @albumDescriptions ) {
	$newFile = $file;
	$newFile =~ s/\.\/albums\//\.\/album-data\//;
	my ($base, $path, $type) = fileparse($newFile, '\.[^.]+\z');
	unless ( -d $path ) {
		mkpath($path, 0, 0755) || print STDERR "Couldn't create path \"$path\": $!\n";
	}
	$base =~ s/_desc//;
	$newFile = "$path$base/album_description.txt";
	unless ( rename $file, $newFile ) {
		print STDERR "WARNING: Unable to migrate album description \"$file\" to \"$newFile\": $!\n";
	} else {
		print "Migrated album description \"$file\" to \"$newFile\"\n";
	}
}

foreach $file ( @imageDescriptions ) {
	$newFile = $file;
	$newFile =~ s/\.\/albums\//\.\/album-data\//;
	my ($base, $path, $type) = fileparse($newFile, '\.[^.]+\z');
	unless ( -d $path ) {
		mkpath($path, 0, 0755) || print STDERR "Couldn't create path \"$path\": $!\n";
	}
	unless ( rename $file, $newFile ) {
		print STDERR "WARNING: Unable to migrate image description \"$file\" to \"$newFile\": $!\n";
	} else {
		print "Migrated image description \"$file\" to \"$newFile\"\n";
	}
}

sub determineDescType {
	foreach my $descFile ( @descriptions ) {
		my ($base, $path, $type) = fileparse($descFile, '\_desc.txt');
		if (-d "$path$base") {
			push @albumDescriptions, $descFile;
			print "Found album description \"$descFile\"\n" if $debug;
		} else {
			push @imageDescriptions, $descFile;
			print "Found image description \"$descFile\"\n" if $debug;
		}
	}
}

sub findFilesToMove {
	my $file = $_;
	my $fileWithPath = $File::Find::name;
	if ( $file =~ /\A\.album\.jpg\Z/ ) {
		push @albumIcons, $fileWithPath;
		print "Found album icon \"$fileWithPath\"\n" if $debug;
	} elsif ( $file =~ /\.txt\Z/ ) {
		push (@descriptions, $fileWithPath);
		print "Found description \"$fileWithPath\"\n" if $debug;
	}
}

print "Upgrade complete.\n\n";