:
eval 'exec perl -wS $0 ${1+"$@"}'
	if 0;

#*************************************************************************
#
#   $RCSfile: kdeint,v $
#
#   $Revision: 1.1.2.1 $
#
#   last change: $Author: khendricks $ $Date: 2002/04/01 19:56:15 $
#
#   The Contents of this file are made available subject to the terms of
#   either of the following licenses
#
#          - GNU Lesser General Public License Version 2.1
#          - Sun Industry Standards Source License Version 1.1
#
#   Sun Microsystems Inc., October, 2000
#
#   GNU Lesser General Public License Version 2.1
#   =============================================
#   Copyright 2000 by Sun Microsystems, Inc.
#   901 San Antonio Road, Palo Alto, CA 94303, USA
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License version 2.1, as published by the Free Software Foundation.
#
#   This library 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
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#   MA  02111-1307  USA
#
#
#   Sun Industry Standards Source License Version 1.1
#   =================================================
#   The contents of this file are subject to the Sun Industry Standards
#   Source License Version 1.1 (the "License"); You may not use this file
#   except in compliance with the License. You may obtain a copy of the
#   License at http://www.openoffice.org/license.html.
#
#   Software provided under this License is provided on an "AS IS" basis,
#   WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#   WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#   MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#   See the License for the specific provisions governing your rights and
#   obligations concerning the Software.
#
#   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#   Copyright: 2000 by Sun Microsystems, Inc.
#
#   All Rights Reserved.
#
#   Contributor(s): _______________________________________
#
#
#
#*************************************************************************


#*************************************************************************
#
# opens or creates a .desktop file and creates or replaces the entries
# for the specified language.
#

sub writeDesktopFile {
	local($out_dir, $filename, *ref) = @_;
    my($outfile, $infile, $tmpfile);
   	my($key,$value,$line);

    # create a temporary file to save unchanged entries
    $tmpfile = "> $out_dir/kdeint.tmp";
    
	# open output file
	unless (open(OUTFILE, $tmpfile)) {
		print STDERR "Can't create temporary output file $tmpfile: $!\n";
		return;
	}

    $infile = sprintf( "< %s/%s", $out_dir, $filename );


	# save all additional values in old file
	if (open(INFILE, $infile)) {

	    # replace all occurances of all key with the corresponding values
	    while (<INFILE>) {
            ($key, $value) = split('=', $_);

            # TryExec causes entries to get invalid
            if ( $key ne "TryExec" ) {
                # remove Name and Comment if language is english
                unless( exists %ref->{$key} || exists %ref->{ sprintf( "%s[en]", $key ) } ) {
                    print OUTFILE;
                }
            }
	    }
        
    	close(INFILE);
	}
    else
    {
        print OUTFILE "[Desktop Entry]\n";
        print OUTFILE "Version=0.92\n";
        print OUTFILE "Encoding=UTF-8\n";
    }

    if( exists %ref->{ "Name[en]" } ) {
        $_ = %ref->{ "Name[en]" };

       	# replace usual resource placeholders

        # It seems PRODUCTNAME = "OpenOffice.org 641" 
        # but Menus should not see version or build numbers 
        s/%PRODUCTNAME/<singleproductname>/g;
        s/%PRODUCTVERSION//g;

        print OUTFILE sprintf( "Name=%s\n", $_ );
    }
    
    if( exists %ref->{ "Comment[en]" } ) {
        $_ = %ref->{ "Comment[en]" };

       	# replace usual resource placeholders

        # It seems PRODUCTNAME = "OpenOffice.org 641" 
        # but Menus should not see version or build numbers 
        s/%PRODUCTNAME/<singleproductname>/g;
        s/%PRODUCTVERSION//g;

        print OUTFILE sprintf( "Comment=%s\n", $_  );
    }
    
    # write all entries to output file
	while (($key, $value) = each %ref) {

        $_ = $value;

       	# replace usual resource placeholders

        # It seems PRODUCTNAME = "OpenOffice.org 641" 
        # but Menus should not see version or build numbers 
        s/%PRODUCTNAME/<singleproductname>/g;
        s/%PRODUCTVERSION//g;

        print OUTFILE sprintf("%s=%s\n", $key, $_);
	}

	close(OUTFILE);
    
    # replace output file with temporary file
    $outfile = "$out_dir/$filename";
    $tmpfile = "$out_dir/kdeint.tmp";
    
    unless( rename $tmpfile, $outfile ) {
        print STDERR "Can't replace output file: $!\n";
    }
}

#************************************************************************
#
# create a panel icon and subpanel
#

sub createGroup {
	local($outpath, $icon, $lang, *ref) = @_;
	my(%replace) = (
        "Name[$lang]", *ref->{ "<group_panel_title>" },
        "Comment[$lang]", *ref->{ "<group_label>" } ,
		"Icon", "$icon.xpm" ,
        "Type", "Directory",
        "MultipleArgs", "false",
        "Terminal", "0"
    );

    # finally write desktop entry file
    writeDesktopFile( "$outpath", ".directory", \%replace );
}

#***********************************************************************
#
# create a control in subpanel
#

sub createControl {
	local($outpath, $name, $fname, $icon, $prog, $mime_types, $lang, *ref) = @_;
	my($outfile, $infile);
	my(%replace) = (
        "Type", "Application",
        "Exec", "\"<progpath>/program/$prog\"",
		"Icon", "$icon.xpm" ,
        "MimeType", "$mime_types",
#        "Name[$lang]", *ref->{ sprintf("<%s_control_label>", $name)  },
         "Name[$lang]", "$name",
#        "Comment[$lang]", *ref->{ sprintf("<%s_control_label>", $name),
        "MultipleArgs", "false",
        "Terminal", "0"
    );

    # finally write desktop entry file
    writeDesktopFile( $outpath, sprintf("%s.desktop", $fname), \%replace );
}


#**********************************************************************
#
# create a folder control in subpanel
#

sub createFolderControl {
	local($outpath, $name, $folder, $lang, $pos, *ref) = @_;
	my($outfile, $infile);
	my(%replace) = (
		"Name[$lang]", *ref->{ sprintf("<%s_control_label>", $name) },
        "Type", "Directory",
        "MultipleArgs", "false",
        "Terminal", "0"
    );

    # finally write desktop entry file
    writeDesktopFile( "$outpath/$folder", ".directory", \%replace );
}

#*********************************************************************
#
# create a data type definition file
#

sub createDataType {
	my($outpath, $mime, $pattern, $icon, $lang, $comment) = @_;

    mkdir($outpath, 0777);
    mkdir("$outpath/share", 0777);
    mkdir("$outpath/share/mimelnk", 0777);
    mkdir("$outpath/share/mimelnk/application", 0777);

	my(%replace) = (
		"Type", "MimeType",
        "Hidden", "false",
        "MimeType", "application/$mime",
        "Icon", "$icon.xpm" ,
        "Comment[$lang]", $comment,
		"Patterns", $pattern 
    );

    # finally write desktop entry file
    writeDesktopFile( "$outpath/share/mimelnk/application", "$mime.desktop", \%replace );
}

#*********************************************************************
#
# create a data type definition file
#

sub createAction {
	local($outpath, $name, $prog, $icon, $lang, *ref) = @_;
	my($outfile, $infile);
	my(%replace) = (
        "Type", "Application",
        "Exec", "\"<progpath>/program/$prog\"",
		"Icon", "$icon.xpm" ,
        "Name[$lang]", *ref->{ sprintf("<%s_action_label>", $name)  },
#        "Comment[$lang]", *ref->{ sprintf("<%s_action_label>", $name)  }
        "MultipleArgs", "false",
        "Terminal", "0",
    );

    # finally write desktop entry file
    writeDesktopFile( $outpath, sprintf("%s.desktop", $name), \%replace );
}

#*********************************************************************
#
# extract locales from tab
#

sub getLocalesForLanguage {
    my($lang) = @_;
    my(@locales);

    # special case for l10n
    if ( $lang == "99" ) {
        return $ENV{L10N_framework};
    }

	# open input file
	unless (open(INFILE, "locales.tab")) {
		print STDERR "Can't open locales.tab file: $!";
		return;
	}
    
	while (<INFILE>) {
        if($lang == substr($_, 0, 2)) {
    		@locales = split( ' ', substr($_, index($_, "=") + 1));
        }
	}
	
	close(INFILE);
    return @locales;
}

#*********************************************************************
#
# extract language strings for lngconv output file
#

sub getNLSStrings {
    my($infile) = @_;
    my(%strings);

	# open input file
	unless (open(INFILE, $infile)) {
		print STDERR "Can't open $infile file: $!\n";
		return;
	}
    
	while (<INFILE>) {
        $line = $_;
        
        # split key = "value" into 2 strings
        ($key, $value) = split(' = ', $line);
        
        # use just anything inside the ""
        $value = substr($value, index($value, "\"") + 1, rindex($value, "\"") - 1);
        
        # add to existing hash
        %strings = ( $key, $value, %strings );
	}
	
	close(INFILE);
    return %strings;
}

#***********************************************************************
#
# create the office group files
#

sub createOfficeGroupFiles {
    local($out_dir, $lang, *ref) = @_;
    my($info_dir, $app_dir);

	my(%readme) = (
        "Type", "Application",
        "Exec", "kedit \"<progpath>/README\"",
		"Icon", "kedit" ,
        "Name", "README",
        "MultipleArgs", "false",
        "Terminal", "false",
    );

    $app_dir = "$out_dir/app";
#    $mime_dir = "$out_dir/mime";
    
#    $info_dir = "$app_dir/information and setup";
#    mkdir( $info_dir, 0777 );

    # create language specific group files
    createGroup($app_dir, "ooo_gulls", $lang, *ref);

    # create .order file
	unless (open(OUTFILE, "> $app_dir/.order" )) {
		print STDERR "Can't create order file : $outpath/.order !\n";
		return;
	}
    
    print OUTFILE "textdoc.desktop\n";       #1
    print OUTFILE "spreadsheet.desktop\n";   #2
    print OUTFILE "drawing.desktop\n";       #3
    print OUTFILE "presentation.desktop\n";  #4
    print OUTFILE "mathdoc.desktop\n";        #5
    print OUTFILE "setup.desktop\n";      #6
    print OUTFILE "printeradmin.desktop\n";                 #7
    close(OUTFILE);

    # create language specific control files
    createAction( $app_dir, "setup", "setup", "500_setup", $lang, *ref);
    createAction( $app_dir, "printeradmin", "spadmin", "ooo_printeradmin", $lang, *ref);

    # write desktop file for readme
    # writeDesktopFile( $info_dir, "readme.desktop", \%readme );
}

#***********************************************************************
#
# create the office writer files
#

sub createOfficeWriterFiles {
    local($out_dir, $lang, *ref) = @_;
    my($app_dir, $mime_dir);

    $app_dir = "$out_dir/app";
    $mime_dir = "$out_dir/mime";

    # create language specific control files
    createControl($app_dir, "%PRODUCTNAME Writer", "textdoc", "ooo_writer", "swriter", "application/vnd.sun.xml.writer;application/vnd.stardivision.writer;application/msword;application/vnd.ms-word;application/x-doc;text/rtf", $lang, *ref);

    # create data type files
    createDataType($mime_dir, "vnd.sun.xml.writer", "*.sxw", "002_text_document", $lang, sprintf( "<singleproductname> 1.0 %s", *ref->{ "<textdoc_control_label>" } ));
    createDataType($mime_dir, "vnd.sun.xml.writer.template", "*.stw", "003_text_template", $lang, *ref->{ "<textdoc_template_long>" } );
    createDataType($mime_dir, "vnd.sun.xml.writer.global", "*.sxg", "011_global_document", $lang, sprintf( "<singleproductname> 1.0 %s", *ref->{ "<masterdoc_action_label>" } ) );
    createDataType($mime_dir, "vnd.stardivision.writer", "*.sdw", "002_text_document", $lang, "StarWriter 5.0" );
    createDataType($mime_dir, "vnd.stardivision.writer-global", "*.sgl", "011_global_document", $lang, sprintf( "StarWriter 5.0 %s", *ref->{ "<masterdoc_action_label>" } ) );

    # create data type to disable x-staroffice mime type (SuSE 7.2)
    createDataType($mime_dir, "x-staroffice", "", "", $lang, "StarOffice 5.0" );
    
}

#***********************************************************************
#
# create the office math files
#

sub createOfficeMathFiles {
    local($out_dir, $lang, *ref) = @_;
    my($app_dir, $mime_dir);

    $app_dir = "$out_dir/app";
    $mime_dir = "$out_dir/mime";

    # create language specific control files
    createControl($app_dir, "%PRODUCTNAME Math", "mathdoc", "ooo_math", "smath", "application/vnd.sun.xml.writer.math;application/vnd.stardivision.math", $lang, *ref);

    # create data type files
    createDataType($mime_dir, "vnd.sun.xml.writer.math", "015_math_document", $lang, sprintf( "<singleproductname> 1.0 %s", *ref->{ "<mathdoc_action_label>" } ) );
    createDataType($mime_dir, "vnd.stardivision.math", "*.sdf", "015_math_document", $lang, "StarMath 5.0" );
}





#***********************************************************************
#
# create the office calc files
#

sub createOfficeCalcFiles {
    local($out_dir, $lang, *ref) = @_;
    my($mime_dir);
    
    $mime_dir = "$out_dir/mime";

    # create language specific control files
    createControl("$out_dir/app", "%PRODUCTNAME Calc", "spreadsheet", "ooo_calc", "scalc", "application/vnd.sun.xml.calc;application/vnd.stardivision.calc;application/vnd.stardivision.chart;application/msexcel;application/vnd.ms-excel", $lang, *ref);

    # create data type files
    createDataType($mime_dir, "vnd.sun.xml.calc", "*.sxc", "004_spreadsheet_document", $lang, sprintf( "<singleproductname> 1.0 %s", *ref->{ "<spreadsheet_control_label>" } ) );
    createDataType($mime_dir, "vnd.sun.xml.calc.template", "*.stc", "005_spreadsheet_template", $lang, *ref->{ "<spreadsheet_template_long>" } );
    createDataType($mime_dir, "vnd.stardivision.calc", "*.sdc", "004_spreadsheet_document", $lang, "StarCalc 5.0" );
    createDataType($mime_dir, "vnd.stardivision.chart", "*.sds", "004_spreadsheet_document", $lang, "StarChart 5.0" );
}

#***********************************************************************
#
# create the office draw files
#

sub createOfficeDrawFiles {
    local($out_dir, $lang, *ref) = @_;
    my($mime_dir);
    
    $mime_dir = "$out_dir/mime";

    # create language specific control files
    createControl("$out_dir/app", "%PRODUCTNAME Draw", "drawing", "ooo_draw", "sdraw", "application/vnd.sun.xml.draw;application/vnd.stardivision.draw", $lang, *ref);

    # create data type files
    createDataType($mime_dir, "vnd.sun.xml.draw", "*.sxd", "006_drawing_document", $lang, sprintf( "<singleproductname> 1.0 %s", *ref->{ "<drawing_control_label>" } ) );
    createDataType($mime_dir, "vnd.sun.xml.draw.template", "*.std", "007_drawing_template", $lang, *ref->{ "<drawing_template_long>" } );
    createDataType($mime_dir, "vnd.stardivision.draw", "*.sda", "006_drawing_document", $lang, "StarDraw 5.0" );
}

#***********************************************************************
#
# create the office impress files
#

sub createOfficeImpressFiles {
    local($out_dir, $lang, *ref) = @_;
    my($mime_dir);
    
    $mime_dir = "$out_dir/mime";

    # create language specific control files
    createControl("$out_dir/app", "%PRODUCTNAME Impress", "presentation", "ooo_impress", "simpress", "application/vnd.sun.xml.impress;application/vnd.stardivision.impress;application/mspowerpoint", $lang, *ref);

    # create data type files
    createDataType($mime_dir, "vnd.sun.xml.impress", "*.sxi", "008_presentation_document", $lang, sprintf( "<singleproductname> 1.0 %s", *ref->{ "<presentation_control_label>" } ) );
    createDataType($mime_dir, "vnd.sun.xml.impress.template","*.sti", "009_presentation_template", $lang, *ref->{ "<presentation_template_long>" } );
    createDataType($mime_dir, "vnd.stardivision.impress", "*.sdd", "008_presentation_document", $lang, "StarImpress 5.0" );
}

#*********************************************************************
#
# main
#

# exspected command line arguments
if( $#ARGV < 2 ) {
    print "\nUsage: $0 <output file> <lang id> <charset>\n\n";
    print "Parameters:\n";
    print "  <output file> - the bin sub-directory of the output path\n";
    print "                  (e.g. ../../unxsols.pro/bin/kdegroup01.zip).\n";
    print "  <lang id>     - the numeric language id\n";
    print "                  (e.g. 49 for german).\n";
    print "  <charset>     - the bin sub-directory of the output path\n"; 
    print "                  (e.g. \"CHARSET_ASCII_US\").\n";
    exit(-1);
}

my($outfile, $lang, $charset) = @ARGV;

# parse outfile parameter
$outpath = substr( $outfile, 0, index( $outfile, "/bin/" ) );
$zipkind = substr( $outfile, rindex( $outfile, "kde" ) + 3 ); 
$zipkind = substr( $zipkind, 0, rindex( $zipkind, $lang ) );
$outfile = "kde$zipkind$lang.zip";

#
# create directories
#

$res_dir = sprintf("%s/res/%s", $outpath, $lang);
mkdir($res_dir, 0777);

$kde_dir = sprintf("%s/res/kde", $outpath );
mkdir($kde_dir, 0777);

$target_dir = sprintf("%s/%s", $kde_dir, $zipkind );
mkdir($target_dir, 0777);
mkdir("$target_dir/app", 0777);
mkdir("$target_dir/mime", 0777);

# scan table for locales
@locales = getLocalesForLanguage($lang);

# run lngconv tool for selected language
@args = ( "$outpath/bin/lngconv", "-o", $res_dir, "../cde/$zipkind.lng", $lang, $charset, @locales );
system(@args);

for (@locales) {
    $locale = $_;
    s/.UTF-8//g;
    $lang = $_;

    # read the strings in specific encoding
    %replace = getNLSStrings(sprintf("%s/$zipkind.%s", $res_dir, $locale));

    SWITCH: {
        if ( $zipkind eq "group" )   { createOfficeGroupFiles  ( $target_dir, $lang, \%replace ); last SWITCH; }
        if ( $zipkind eq "writer" )  { createOfficeWriterFiles ( $target_dir, $lang, \%replace ); last SWITCH; }
        if ( $zipkind eq "calc" )    { createOfficeCalcFiles   ( $target_dir, $lang, \%replace ); last SWITCH; }
        if ( $zipkind eq "draw" )    { createOfficeDrawFiles   ( $target_dir, $lang, \%replace ); last SWITCH; }
        if ( $zipkind eq "impress" ) { createOfficeImpressFiles( $target_dir, $lang, \%replace ); last SWITCH; }
        if ( $zipkind eq "math" )    { createOfficeMathFiles   ( $target_dir, $lang, \%replace ); last SWITCH; }
    }
}

# create file for dependencies
@args = ( "touch", "$outpath/bin/kde$zipkind.$lang" );
system(@args);
