xref: /aoo41x/main/solenv/bin/cwsattach.pl (revision cdf0e10c)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29#*************************************************************************
30#
31# cwsattach.pl - attach files to CWS
32#
33
34use strict;
35use Getopt::Long;
36use Cwd;
37
38#### module lookup
39my @lib_dirs;
40BEGIN {
41    if ( !defined($ENV{SOLARENV}) ) {
42        die "No environment found (environment variable SOLARENV is undefined)";
43    }
44    push(@lib_dirs, "$ENV{SOLARENV}/bin/modules");
45    push(@lib_dirs, "$ENV{COMMON_ENV_TOOLS}/modules") if defined($ENV{COMMON_ENV_TOOLS});
46}
47use lib (@lib_dirs);
48
49use Cws;
50
51#### script id #####
52
53( my $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
54
55my $script_rev;
56my $id_str = ' $Revision: 1.3 $ ';
57$id_str =~ /Revision:\s+(\S+)\s+\$/
58  ? ($script_rev = $1) : ($script_rev = "-");
59
60print STDERR "$script_name -- version: $script_rev\n";
61
62#### global #####
63
64my $is_debug = 1;       # enable debug
65my $opt_master = '';    # option: master workspace
66my $opt_child  = '';    # option: child workspace
67my $opt_mime_type = '';  # option: mime type
68
69
70#### main #####
71
72my $arg_file = parse_options();
73attach_cws($arg_file);
74exit(0);
75
76#### subroutines ####
77
78sub attach_cws
79{
80    my $filename = shift;
81    # get master and child workspace
82    my $masterws = $opt_master ? uc($opt_master) : $ENV{WORK_STAMP};
83    my $childws  = $opt_child  ? $opt_child  : $ENV{CWS_WORK_STAMP};
84
85    if ( !defined($masterws) ) {
86        print_error("Can't determine master workspace environment.\n"
87                    . "Please initialize environment with setsolar ...", 1);
88    }
89
90    if ( !defined($childws) ) {
91        print_error("Can't determine child workspace environment.\n"
92                    . "Please initialize environment with setsolar ...", 1);
93    }
94
95    my $cws = Cws->new();
96    $cws->child($childws);
97    $cws->master($masterws);
98
99    my $mime_type  = $opt_mime_type  ? $opt_mime_type  : find_mime_type($filename);
100
101    no strict;
102
103    if ( is_valid_cws($cws) ) {
104        #print "CWS is valid filename=" . $filename . " mime_type=" . $mime_type . "\n";
105        open(DATA,"<$filename") || die "can't open filename";
106        $data="";
107	while(<DATA>) {
108		$data.=$_;
109	}
110        my $result=$cws->save_attachment($filename,$mime_type,$data);
111    } else {
112        print STDERR "cws is not valid";
113    }
114    exit(0)
115}
116
117
118sub find_mime_type
119{
120    my $filename = shift;
121    $filename=~/(.*)\.(.*$)/;
122    my $ext=$2;
123    my $fmime='';
124
125    if ( defined($ext) ) {
126        open(MIME,"< $ENV{SOLARENV}/inc/mime.types")|| die "can not open mimetype file";
127        while (<MIME>) {
128            my @a=split();
129            my $iscomment=0;
130            if ( /(\s*\#).*/ ) {
131                $iscomment=1;
132            } else {
133                $iscomment=0;
134            }
135            if ( $iscomment eq 0 && $#a >= 1 && $fmime eq '' ) {
136                my $i=1;
137                for ($i=1; $i<=$#a; $i++) {
138                    if ( $a[$i] eq $ext ) {
139                        $fmime=$a[0];
140                    }
141                }
142            }
143        }
144
145    }
146    if ( $fmime eq '' ) {
147        $fmime="application/octet-stream";
148    }
149    return $fmime;
150}
151
152
153sub is_valid_cws
154{
155    my $cws = shift;
156
157    my $masterws = $cws->master();
158    my $childws  = $cws->child();
159    # check if we got a valid child workspace
160    my $id = $cws->eis_id();
161    if ( !$id ) {
162        print_error("Child workspace '$childws' for master workspace '$masterws' not found in EIS database.", 2);
163    }
164    print_message("Master workspace '$masterws', child workspace '$childws':");
165    return 1;
166}
167
168sub parse_options
169{
170    # parse options and do some sanity checks
171    my $help = 0;
172    my $success = GetOptions('h' => \$help, 'm=s' => \$opt_master, 'c=s'=> \$opt_child, 't=s'=> \$opt_mime_type);
173    if ( $help || !$success || $#ARGV < 0 ) {
174        usage();
175        exit(1);
176    }
177
178    return $ARGV[0];
179}
180
181sub print_message
182{
183    my $message     = shift;
184
185    print STDERR "$script_name: ";
186    print STDERR "$message\n";
187    return;
188}
189
190sub print_error
191{
192    my $message     = shift;
193    my $error_code  = shift;
194
195    print STDERR "$script_name: ";
196    print STDERR "ERROR: $message\n";
197
198    if ( $error_code ) {
199        print STDERR "\nFAILURE: $script_name aborted.\n";
200        exit($error_code);
201    }
202    return;
203}
204
205sub usage
206{
207    print STDERR "Usage: cwsattach [-h] [-m master] [-c child] [-t mimetype] filename\n";
208    print STDERR "\n";
209    print STDERR "Attach files to CWS in EIS database\n";
210    print STDERR "\n";
211    print STDERR "Options:\n";
212    print STDERR "\t-h\t\thelp\n";
213    print STDERR "\t-m master\toverride MWS specified in environment\n";
214    print STDERR "\t-c child\toverride CWS specified in environment\n";
215    print STDERR "\t-t mimetype\texplicitly set mime type\n";
216    print STDERR "Examples:\n";
217    print STDERR "\tcwsattach barfoo.html\n";
218    print STDERR "\tcwsattach -t text bar.cxx\n";
219    print STDERR "\tcwsattach -t text/rtf foo.rtf\n";
220}
221