#!/usr/bin/perl
#
# re-run this scrip if hosted_freedict.txt has changed
# generates hosted_freedict.{desc,sel,cfg}
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/import/freedict/hosted_freedict.pl
# ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
# 
# 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. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

use warnings;
use strict;

use XML::Parser;

our $baseurl="http://jaist.dl.sourceforge.net/sourceforge/freedict/";
our (%hosted,%lang,$current);

my $p= new XML::Parser( Handlers => { Start => start_handler() } );

%lang=(
  afr => 'Afrikaans',
  cro => 'Croatian',
  cze => 'Czech',
  dan => 'Danish',
  nld => 'Dutch',
  eng => 'English',
  deu => 'German',
  fra => 'French',
  ara => 'Arabic',
  hin => 'Hindi',
  hun => 'Hungarian',
  iri => 'Irish',
  ita => 'Italian',
  lat => 'Latin',
  por => 'Portuguese',
  rom => 'Romanian',
  rus => 'Russian',
  san => 'Sanskrit',
  sco => 'Scottish',
  slo => 'Slovak',
  spa => 'Spanish',
  swa => 'Swahili',
  swe => 'Swedish',
  wel => 'Welsh',
  jpn => 'Japanese',
  kha => 'Khasi',
  scr => 'Serbo-Croat',
  tur => 'Turkish' );


my $database='download/mirror/f/freedict-database-20050726.xml';
die 'Please download freedict dictionary list with "./scripts/Download freedict" and try again.'
  unless -f $database;

$p->parsefile($database); # No need, to say die.

my %cksum_cache;

open(D, "package/import/freedict/hosted_freedict.desc");
while (<D>) {
       $cksum_cache{$2} = $1 if /^\[D\] (\d+) (\S+)/;    
}
close D;

open(D, ">package/import/freedict/hosted_freedict.desc") || die $!;
open(S, ">package/import/freedict/hosted_freedict.sel") || die $!;
open(C, ">package/import/freedict/hosted_freedict.cfg") || die $!;

print D "# Auto-generated by hosted_freedict.pl from $database\n";
print S "# Auto-generated by hosted_freedict.pl from $database\n";
print C "# Auto-generated by hosted_freedict.pl from $database\n";

print S "\ncase \"\$xpkg\" in\n";
print D "\n"; 
print C "\n";

foreach my $name ( sort keys %hosted ){

        my $dict=$hosted{$name};

	next unless $dict->{'file'};

	if( $ARGV[0] ){
    		print "# $name: $dict->{'fullname'} $dict->{'version'} $dict->{'file'}\n";
    		print "./scripts/Build-Pkg freedict-$name\n";
	}

        my $f =$dict->{'file'};
        my $fn=$dict->{'fullname'};
        my $v =$dict->{'version'};
        my $ydict=uc $name;
        $ydict=~ s/\-/_/g;
    
        my $c = 0;
        $c = $cksum_cache{"freedict-$name-$v.tar.gz"}
                if defined $cksum_cache{"freedict-$name-$v.tar.gz"};

	print D "#if xpkg == freedict-$name\n";
        print D "[I] $fn dictionary\n";
        print D '[T] headwords: '.$dict->{'headwords'}."\n";
        print D '[U] '.$dict->{'url'}."\n" if $dict->{'url'};
	print D "[V] $v\n";
	print D "[D] $c freedict-$name-$v.tar.gz !$baseurl$f\n";
	print D "#endif\n\n";

	print S "\tfreedict-$name)\n";
        print S "\t\tfreedict=\"$name\"\n";
	print S "\t\tfreedictver=\"$v\"\n";
	print S "\t\tsrctar=\"freedict-$name-$v.tar.bz2\"\n";
	print S "\t\t;;\n";

	print C "bool 'Building package freedict-$name ($fn)' ROCKCFG_PKG_FREEDICT_$ydict 1\n";
	print C "if [ \$ROCKCFG_PKG_FREEDICT_$ydict = 1 ]; then pkgfork freedict freedict-$name priority 700.000; fi\n\n"
}

print S "esac\n\n";

close D; close S;
close C; 


#########################
sub start_handler{ sub{
#########################
	my $expat=shift;
	my $tag  =shift;
	my %attr =@_;

	if( $tag eq 'dictionary' ){
		my %dict=();
		$current=$attr{'name'};
    		my ($source,$target)=split /-/,$current;
		warn "Unknown source language $source" unless $lang{$source};
		warn "Unknown target language $target" unless $lang{$target};
    		$dict{'fullname'}="$lang{$source}-$lang{$target}";
		$dict{'headwords'}=$attr{'headwords'};
		$dict{'url'}= $attr{'sourceURL'} =~ /freedict/ ? undef : $attr{'sourceURL'};
		$dict{'version'}=$attr{'edition'};
	
		$hosted{$current}=\%dict;	
	}

	if( $tag eq 'release' ){
		if( $attr{'platform'} eq "dict-tgz" ){
			my $file= $attr{'URL'} =~ m'freedict/([^\?]+)\?download' && $1;
			$hosted{$current}->{'file'}=$file;
		}
	}

}}