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

#*************************************************************************
#
#   $RCSfile: gnomeint,v $
#
#   $Revision: 1.1.2.1 $
#
#   last change: $Author: khendricks $ $Date: 2002/04/01 19:56:09 $
#
#   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);
    my($name, $comment);

    # create a temporary file to save unchanged entries
    $tmpfile = "> $out_dir/gnomeint.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" ) {
                # Name and Comment must be removed 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=Legacy-Mixed\n";
    }

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

       	# replace usual resource placeholders

        # It seems PRODUCTNAME = "OpenOffice.org 641" 
        # but neither version nor build number should be present in Menus 
        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
        # Its seems PRODUCTNAME = "OpenOffice.org 641" 
        # but neither version nor build number should be present in Menus 
        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 neither version nor build number should be present in Menus 
        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/gnomeint.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", "<progpath>/share/icons/$icon.png" ,
        "Type", "Directory",
        "MultipleArgs", "false",
        "Terminal", "false"
    );

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

    # create .order file
	unless (open(OUTFILE, "> $outpath/.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";    #7

	close(OUTFILE);
}

#***********************************************************************
#
# 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\"",
        "Exec", "<progpath>/program/$prog",
		"Icon", "<progpath>/share/icons/$icon.png" ,
        "MimeType", "$mime_types",
        "Name[$lang]", $name,
#        "Comment[$lang]", *ref->{ sprintf("<%s_control_label>", $name),
        "MultipleArgs", "false",
        "Terminal", "false"
    );

    # 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",
        "Icon", "gnome-folder.png",
        "MultipleArgs", "false",
        "Terminal", "false"
    );

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

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

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

	# ensure control does not contain spaces
	$_ = $control;
	s/ /_/g;

	my(%replace) = (
		'<data_type>', $name,
		'<data_type_mime>', $mime,
        '<data_type_icon>', "<singleproductname>_<productversion>_$icon",
		'<data_type_pattern>', $pattern,
		'<data_type_control>', "<singleproductname>_<productversion>_$_");

	# assemble output file name for data type file
	$outfile = sprintf("> %s/%s.dt", $outpath, $name);

	# create data type file
	createFile($outfile, 'datatype.dt', \%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\"",
        "Exec", "<progpath>/program/$prog",
		"Icon", "<progpath>/share/icons/$icon.png" ,
        "Name[$lang]", *ref->{ sprintf("<%s_action_label>", $name)  },
#        "Comment[$lang]", *ref->{ sprintf("<%s_action_label>", $name)  }
        "MultipleArgs", "false",
        "Terminal", "false",
    );

    # 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(%readme) = (
        "Type", "Application",
        "Exec", "gless \"<progpath>/README\"",
		"Icon", "gnome-note.png" ,
        "Name", "README",
        "MultipleArgs", "false",
        "Terminal", "false",
    );

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

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

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

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

    # create language specific control files
    createControl($out_dir, "%PRODUCTNAME Writer", "textdoc", "ooo_writer", "swriter", "application/vnd.sun.xml.writer application/vnd.stardivision.writer", $lang, *ref);

    # create data type files
#    createDataType($out_dir, "vnd.sun.xml.writer.document", "vnd.sun.xml.writer", "*.[sS][xX][wW]", "002_text_document", "textdoc");
#    createDataType($out_dir, "vnd.sun.xml.writer.template", "vnd.sun.xml.writer", "*.[sS][tT][wW]", "003_text_template", "textdoc");
#    createDataType($out_dir, "vnd.sun.xml.writer.global", "vnd.sun.xml.writer.global", "*.[sS][xX][gG]", "011_master_document", "masterdoc");
#    createDataType($out_dir, "vnd.stardivision.writer", "vnd.stardivision.writer", "*.[sS][dD][wW]", "002_text_document", "textdoc");
#    createDataType($out_dir, "vnd.stardivision.writer.global", "vnd.stardivision.writer-global", "*.[sS][dD][wW]", "011_master_document", "textdoc");
    
}


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

sub createOfficeMathFiles {
    local($out_dir, $lang, *ref) = @_;
    
    # create language specific control files
    createControl($out_dir, "%PRODUCTNAME Math", "mathdoc", "ooo_math", "smath", "application/vnd.sun.xml.math application/vnd.stardivision.math", $lang, *ref);

    # create data type files
#    createDataType($out_dir, "vnd.sun.xml.math", "vnd.sun.xml.math", "*.[sS][xX][mM]", "015_math_document", "mathdoc");
#    createDataType($out_dir, "vnd.stardivision.math", "vnd.stardivision.math", "*.[sS][dD][fF]", "015_math_document", "mathdoc");
    
}



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

sub createOfficeCalcFiles {
    local($out_dir, $lang, *ref) = @_;

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

    # create data type files
#    createDataType($out_dir, "vnd.sun.xml.calc.document", "vnd.sun.xml.calc", "*.[sS][xX][cC]", "004_spreadsheet_document", "spreadsheet");
#    createDataType($out_dir, "vnd.sun.xml.calc.template", "vnd.sun.xml.calc", "*.[sS][tT][cC]", "005_spreadsheet_template", "spreadsheet");
#    createDataType($out_dir, "vnd.stardivision.calc", "vnd.stardivision.calc", "*.[sS][dD][cC]", "004_spreadsheet_document", "spreadsheet");
#    createDataType($out_dir, "vnd.stardivision.chart", "vnd.stardivision.chart", "*.[sS][dD][sS]", "004_spreadsheet_document", "spreadsheet");
}

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

sub createOfficeDrawFiles {
    local($out_dir, $lang, *ref) = @_;

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

    # create data type files
#    createDataType($out_dir, "vnd.sun.xml.draw.document", "vnd.sun.xml.draw", "*.[sS][xX][dD]", "006_drawing_document", "drawing");
#    createDataType($out_dir, "vnd.sun.xml.draw.document", "vnd.sun.xml.draw", "*.[sS][tT][dD]", "007_drawing_template", "drawing");
#    createDataType($out_dir, "vnd.stardivision.draw", "vnd.stardivision.draw", "*.[sS][dD][aA]", "006_drawing_document", "drawing");
}

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

sub createOfficeImpressFiles {
    local($out_dir, $lang, *ref) = @_;

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

    # create data type files
 #   createDataType($out_dir, "vnd.sun.xml.impress.document", "vnd.sun.xml.impress", "*.[sS][xX][iIpP]", "008_presentation_document", "presentation");
 #   createDataType($out_dir, "vnd.sun.xml.impress.template", "vnd.sun.xml.impress", "*.[sS][tT][iIpP]", "009_presentation_template", "presentation");
 #   createDataType($out_dir, "vnd.stardivision.draw.impress", "vnd.stardivision.impress", "*.[sS][dD][dD]", "009_presentation_document", "presentation");
}


#*********************************************************************
#
# 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/oognomegroup01.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, "gnome" ) + 5 ); 
$zipkind = substr( $zipkind, 0, rindex( $zipkind, $lang ) );
$outfile = "oognome$zipkind$lang.zip";

#
# create directories
#

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

$gnome_dir = sprintf("%s/res/gnome", $outpath );
mkdir($gnome_dir, 0777);

$target_dir = sprintf("%s/%s", $gnome_dir, $zipkind );
mkdir($target_dir, 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 = $_;

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

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

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