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

#*************************************************************************
#
#   $RCSfile: gnomeint,v $
#
#   $Revision: 1.14.4.1 $
#
#   last change: $Author: mh $ $Date: 2002/11/01 10:10:50 $
#
#   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
        s/%PRODUCTNAME/<productname>/g;
        s/%PRODUCTVERSION//g;

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

       	# replace usual resource placeholders
        s/%PRODUCTNAME/<productname>/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
        s/%PRODUCTNAME/<productname>/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 "webdoc.desktop\n";        #5
    print OUTFILE "template.desktop\n";      #6
    print OUTFILE "other\n";                 #7
    print OUTFILE "auto pilots\n";           #8
    print OUTFILE "information and setup\n"; #9    

	close(OUTFILE);
}

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

sub createControl {
	local($outpath, $name, $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]", *ref->{ sprintf("<%s_control_label>", $name)  },
#        "Comment[$lang]", *ref->{ sprintf("<%s_control_label>", $name),
        "MultipleArgs", "false",
        "Terminal", "false"
    );

    # finally write desktop entry file
    writeDesktopFile( $outpath, sprintf("%s.desktop", $name), \%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 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);

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

    $info_dir = "$out_dir/information and setup";
    mkdir( $info_dir, 0777 );

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

    # create language specific control files
    createControl($out_dir, "template", "016_template", "fromtemplate", "", $lang, *ref);
    createFolderControl( $out_dir, "info_and_setup", "information and setup", $lang, *ref );
    createAction( $info_dir, "setup", "setup", "500_setup", $lang, *ref);
    createAction( $info_dir, "printeradmin", "spadmin", "501_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($other_dir, $auto_dir, $info_dir);

    $other_dir = "$out_dir/other";
    mkdir( $other_dir, 0777 );

    $auto_dir = "$out_dir/auto pilots";
    mkdir( $auto_dir, 0777 );
    
    # create language specific control files
    createControl($out_dir, "textdoc", "002_text_document", "swriter", "application/vnd.sun.xml.writer application/vnd.stardivision.writer", $lang, *ref);
    createControl($out_dir, "webdoc", "012_html_document", "sweb", "application/vnd.sun.xml.writer.global application/vnd.stardivision.writer.global", $lang, *ref);
    createFolderControl( $out_dir, "other", "other", $lang, *ref );
    createFolderControl( $out_dir, "auto_pilots", "auto pilots", $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.sun.xml.math", "vnd.sun.xml.math", "*.[sS][xX][mM]", "015_math_document", "textdoc");
#    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");
#    createDataType($out_dir, "vnd.stardivision.math", "vnd.stardivision.math", "*.[sS][dD][fF]", "015_math_document", "textdoc");
    
    # create actions for other documents
    createAction($other_dir, "masterdoc", "smaster", "011_master_document", $lang, *ref);
    createAction($other_dir, "mathdoc", "smath", "015_math_document", $lang, *ref);
    createAction($other_dir, "labels", "slabel", "017_macrolibrary", $lang, *ref);
    createAction($other_dir, "vcards", "svcard", "017_macrolibrary", $lang, *ref);

    # create actions for autopilots
    createAction($auto_dir, "letter", "sletter", "017_macrolibrary", $lang, *ref);
    createAction($auto_dir, "fax", "sfax", "017_macrolibrary", $lang, *ref);
    createAction($auto_dir, "agenda", "sagenda", "017_macrolibrary", $lang, *ref);
    createAction($auto_dir, "memo", "smemo", "017_macrolibrary", $lang, *ref);

}

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

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

    # create language specific control files
    createControl($out_dir, "spreadsheet", "004_spreadsheet_document", "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, "drawing", "006_drawing_document", "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, "presentation", "008_presentation_document", "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 <in/out path> <lang id> <module>\n\n";
    print "Parameters:\n";
    print "  <in/out path> - the input and output path\n";
    print "                  (e.g. ../../unxsols.pro).\n";
    print "  <lang id>     - the numeric language id\n";
    print "                  (e.g. 49 for german).\n";
    print "  <module>      - the module\n"; 
    print "                  (e.g. \"writer\").\n";
    exit(-1);
}

my($outpath, $lang, $module, @locales) = @ARGV;

#
# create directories
#

$res_dir = "$outpath/res";
mkdir($res_dir, 0777);

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

$target_dir = "$gnome_dir/$module";
mkdir($target_dir, 0777);

for (@locales) {
    $locale = $_;

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

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