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

#*************************************************************************
#
#   $RCSfile: cdeint,v $
#
#   $Revision: 1.3 $
#
#   last change: $Author: vg $ $Date: 2003/04/11 16:04:31 $
#
#   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/<singleproductname>/g;
        s/%PRODUCTVERSION//g;
       
		print OUTFILE;
	}
	
	close(INFILE);
	close(OUTFILE);
}

#*********************************************************************
#
# 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 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;
}


#*********************************************************************
#
# 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 = sprintf("%s/res", $outpath);

$lang_dir = "$res_dir/$lang";
mkdir($lang_dir, 0777);

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

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

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

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

    createFile( "> $locale_dir/$module.fp", "$module.fp", \%replace );
    createFile( "> $locale_dir/$module.dt", "$module.dt", \%replace );
    
    # create data types for applications
    if ( $module ne "group" && $module ne "player" ) {
        createFile( "> $locale_dir/vnd.sun.xml.$module.dt", "vnd.sun.xml.$module.dt", \%replace );
        createFile( "> $locale_dir/vnd.stardivision.$module.dt", "vnd.stardivision.$module.dt", \%replace );
    }

    # create data type for master and math documents and ms-word
    if ( $module eq "writer" )  { 
        createFile( "> $locale_dir/vnd.sun.xml.writer.global.dt", "vnd.sun.xml.writer.global.dt", \%replace );
        createFile( "> $locale_dir/vnd.stardivision.writer-global.dt", "vnd.stardivision.writer-global.dt", \%replace ); 
        createFile( "> $locale_dir/ms-word.dt", "ms-word.dt", \%replace ); 
        createFile( "> $locale_dir/richtext.dt", "richtext.dt", \%replace ); 
    }
    if ( $module eq "math" )  { 
        createFile( "> $locale_dir/vnd.sun.xml.math.dt", "vnd.sun.xml.math.dt", \%replace );
        createFile( "> $locale_dir/vnd.stardivision.math.dt", "vnd.stardivision.math.dt", \%replace ); 
    }

    # create data type for 5.x chart and ms-excel
    if ( $module eq "calc" ) { 
        createFile( "> $locale_dir/vnd.stardivision.chart.dt", "vnd.stardivision.chart.dt", \%replace ); 
        createFile( "> $locale_dir/ms-excel.dt", "ms-excel.dt", \%replace ); 
    }

    # create data type for and ms-powerpoint
    if ( $module eq "impress" ) { 
        createFile( "> $locale_dir/ms-powerpoint.dt", "ms-powerpoint.dt", \%replace ); 
    }

}

@args = ( "zip", "-R", "../../../bin/cde$module.zip", "*" );

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




