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

#*************************************************************************
#
#   $RCSfile: cdeint,v $
#
#   $Revision: 1.10 $
#
#   last change: $Author: obr $ $Date: 2001/08/30 11:18:18 $
#
#   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): _______________________________________
#
#
#
#*************************************************************************

#*************************************************************************
#
# copy file line by line and replace every all occurances of all keys with
# the corresponding values.
#

sub createFile {
	local($outfile, $infile, *ref) = @_;
	my($key,$value);
	
	# open output file
	unless (open(OUTFILE, $outfile)) {
		print STDERR "Can't create output file $outfile: $!";
		return;
	}

	# open input file
	unless (open(INFILE, $infile)) {
		print STDERR "Can't open input file $infile: $!";
		return;
	}

	# replace all occurances of all key with the corresponding values
	while (<INFILE>) {
		while (($key, $value) = each %ref) {
			s/$key/$value/g;
		}

		# replace usual resource placeholders
        s/%PRODUCTNAME/<productname>/g;
        s/%PRODUCTVERSION//g;
        
		print OUTFILE;
	}
	
	close(INFILE);
	close(OUTFILE);
}


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

sub createGroup {
	local($outpath, $name, $icon, $prog, *ref) = @_;
	my($outfile);
	my(%replace) = (
		'<group_icon>', "<singleproductname>_<productversion>_$icon", 
		'<group_prog>', $prog,
		'<group_name>', "<singleproductname_nospace>_<productversion>_$name",
        %ref);

	# assemble output file name for front panel file
	$outfile = sprintf("> %s/%s.fp", $outpath, $name);

	# create front panel file
	createFile($outfile, "group.fp", \%replace);

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

	# create data type file
	createFile($outfile, "group.dt", \%replace);
}

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

sub createControl {
	local($outpath, $name, $icon, $group, $pos, $prog, *ref) = @_;
	my($outfile, $infile);
	my(%replace) = (
        '<control>', "<singleproductname_nospace>_<productversion>_$name",
		'<control_pos>', $pos,
		'<control_prog>', $prog,
		'<control_icon>', "<singleproductname>_<productversion>_$icon" ,
		'<control_group>', "<singleproductname_nospace>_<productversion>_$group" 
    );
    
    # append label and description from languag strings hash    
    %replace->{"<control_label>"} = *ref->{ sprintf("<%s_control_label>", $name) };
#    %replace->{"<control_open_description>"} = *ref->{ sprintf("<%s_control_open_description>", $name) };
#    %replace->{"<control_new_description>"} = *ref->{ sprintf("<%s_control_new_description>", $name) };

	# assemble output file name for front panel file
	$outfile = sprintf("> %s/%s.fp", $outpath, $name);

	# create front panel file
	createFile($outfile, 'control.fp', \%replace);

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

	# create data type file
	createFile($outfile, 'control.dt', \%replace);
}


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

sub createFolderControl {
	local($outpath, $name, $group, $folder, $pos, *ref) = @_;
	my($outfile, $infile);
	my(%replace) = (
		'<control_pos>', $pos,
        '<control_folder>', $folder,
		'<control_group>', "<singleproductname_nospace>_<productversion>_$group",
		'<control>', $name);

    # append label and description from languag strings hash    
    %replace->{"<control_label>"} = *ref->{ sprintf("<%s_control_label>", $name) };
#    %replace->{"<control_open_description>"} = *ref->{ sprintf("<%s_control_open_description>", $name) };

	# assemble output file name for front panel file
	$outfile = sprintf("> %s/%s.fp", $outpath, $name);

	# create front panel file
	createFile($outfile, 'folder.fp', \%replace);

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

	# create data type file
	createFile($outfile, 'folder.dt', \%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_nospace>_<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, *ref) = @_;
    my($outfile);

	my(%replace) = (
        '<action_name>', "<singleproductname_nospace>_<productversion>_$name",
		'<action_prog>', $prog,
		'<action_icon>', "<singleproductname>_<productversion>_$icon" 
    );

    # append label and description from languag strings hash    
    %replace->{"<action_label>"} = *ref->{ sprintf("<%s_action_label>", $name) };
#    %replace->{"<action_description>"} = *ref->{ sprintf("<%s_action_description>", $name) };


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

	# create data type file
	createFile($outfile, 'action.dt', \%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, *ref) = @_;

    # disable due to #87427# - setup zip library to old
#    mkdir( sprintf( "%s/../../%s", $out_dir, "information and setup" ), 0777 );
#    mkdir( sprintf( "%s/../../%s", $out_dir, "other" ), 0777);
#    mkdir( sprintf( "%s/../../%s", $out_dir, "auto pilots" ), 0777 );

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

    # create language specific control files
    createControl($out_dir, "template", "016_template", "group", 6, "fromtemplate", *ref);
    createFolderControl( $out_dir, "info_and_setup", "group", "information and setup", 9, *ref );
    
    # create action for setup
    createAction($out_dir, "setup", "setup", "500_setup", *ref);
}

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

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


    # create language specific control files
    createControl($out_dir, "textdoc", "002_text_document", "group", 1, "swriter", *ref);
    createControl($out_dir, "webdoc", "012_html_document", "group", 5, "swriter", *ref);
    createFolderControl( $out_dir, "other", "group", "other", 7, *ref );
    createFolderControl( $out_dir, "auto_pilots", "group", "auto pilots", 8, *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_global_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][gG][lL]", "01_global_document", "textdoc");
    createDataType($out_dir, "vnd.stardivision.math", "vnd.stardivision.math", "*.[sS][dD][fF]", "015_math_document", "textdoc");
    
    # create actions for other documents
    createAction($out_dir, "masterdoc", "smaster", "011_global_document", *ref);
    createAction($out_dir, "mathdoc", "smath", "015_math_document", *ref);
    createAction($out_dir, "labels", "slabel", "017_macrolibrary", *ref);
    createAction($out_dir, "vcards", "svcard", "017_macrolibrary", *ref);

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

}

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

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

    # create language specific control files
    createControl($out_dir, "spreadsheet", "004_spreadsheet_document", "group", 2, "scalc", *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, *ref) = @_;

    # create language specific control files
    createControl($out_dir, "drawing", "006_drawing_document", "group", 3, "sdraw", *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, *ref) = @_;

    # create language specific control files
    createControl($out_dir, "presentation", "008_presentation_document", "group", 4, "simpress", *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]", "008_presentation_document", "presentation");
}

#***********************************************************************
#
# create the office player files
#

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

    createGroup($out_dir, "player", "100_player", "soplayer", *ref);

    # create language specific control files
    createControl($out_dir, "setup", "500_setup", "player", 2, "setup", *ref);
}

#*********************************************************************
#
# 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/cdegroup01.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, "cde" ) + 3 ); 
$zipkind = substr( $zipkind, 0, rindex( $zipkind, $lang ) );
$outfile = "cde$zipkind$lang.zip";

#
# create directories
#

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

$target_dir = sprintf("%s/%s", $res_dir, $zipkind);
mkdir($target_dir, 0777);

$types_dir = sprintf("%s/%s", $target_dir, "types");
mkdir($types_dir, 0777);

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

# run lngconv tool for selected language
@args = ( "$outpath/bin/lngconv", "-o", $res_dir, "$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));

    # create a directory for this locale
    $locale_dir = sprintf("%s/%s", $types_dir, $locale);
    mkdir($locale_dir, 0777);

    SWITCH: {
        if ( $zipkind eq "group" )   { createOfficeGroupFiles  ( $locale_dir, \%replace ); last SWITCH; }
        if ( $zipkind eq "writer" )  { createOfficeWriterFiles ( $locale_dir, \%replace ); last SWITCH; }
        if ( $zipkind eq "calc" )    { createOfficeCalcFiles   ( $locale_dir, \%replace ); last SWITCH; }
        if ( $zipkind eq "draw" )    { createOfficeDrawFiles   ( $locale_dir, \%replace ); last SWITCH; }
        if ( $zipkind eq "impress" ) { createOfficeImpressFiles( $locale_dir, \%replace ); last SWITCH; }
        if ( $zipkind eq "player" )  { createOfficePlayerFiles ( $locale_dir, \%replace ); last SWITCH; }
    }
}

@args = ( "zip", "-R", "../../../bin/$outfile", "*" );
@args2 = ( "touch", "../../../bin/cde$zipkind.$lang" );

# create zip file
chdir($target_dir);
system(@args);
system(@args2);
chdir( %ENV->{"PWD"} );



#createFolderControl($out, "testcontrol2", $group, 2, "temporary control label 2", "temporary control description");


