xref: /aoo41x/main/solenv/bin/cwsattach.pl (revision 7e90fac2)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#**************************************************************
5#
6#  Licensed to the Apache Software Foundation (ASF) under one
7#  or more contributor license agreements.  See the NOTICE file
8#  distributed with this work for additional information
9#  regarding copyright ownership.  The ASF licenses this file
10#  to you under the Apache License, Version 2.0 (the
11#  "License"); you may not use this file except in compliance
12#  with the License.  You may obtain a copy of the License at
13#
14#    http://www.apache.org/licenses/LICENSE-2.0
15#
16#  Unless required by applicable law or agreed to in writing,
17#  software distributed under the License is distributed on an
18#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19#  KIND, either express or implied.  See the License for the
20#  specific language governing permissions and limitations
21#  under the License.
22#
23#**************************************************************
24
25
26#
27# cwsattach.pl - attach files to CWS
28#
29
30use strict;
31use Getopt::Long;
32use Cwd;
33
34#### module lookup
35my @lib_dirs;
36BEGIN {
37    if ( !defined($ENV{SOLARENV}) ) {
38        die "No environment found (environment variable SOLARENV is undefined)";
39    }
40    push(@lib_dirs, "$ENV{SOLARENV}/bin/modules");
41    push(@lib_dirs, "$ENV{COMMON_ENV_TOOLS}/modules") if defined($ENV{COMMON_ENV_TOOLS});
42}
43use lib (@lib_dirs);
44
45use Cws;
46
47#### script id #####
48
49( my $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
50
51my $script_rev;
52my $id_str = ' $Revision: 1.3 $ ';
53$id_str =~ /Revision:\s+(\S+)\s+\$/
54  ? ($script_rev = $1) : ($script_rev = "-");
55
56print STDERR "$script_name -- version: $script_rev\n";
57
58#### global #####
59
60my $is_debug = 1;       # enable debug
61my $opt_master = '';    # option: master workspace
62my $opt_child  = '';    # option: child workspace
63my $opt_mime_type = '';  # option: mime type
64
65
66#### main #####
67
68my $arg_file = parse_options();
69attach_cws($arg_file);
70exit(0);
71
72#### subroutines ####
73
74sub attach_cws
75{
76    my $filename = shift;
77    # get master and child workspace
78    my $masterws = $opt_master ? uc($opt_master) : $ENV{WORK_STAMP};
79    my $childws  = $opt_child  ? $opt_child  : $ENV{CWS_WORK_STAMP};
80
81    if ( !defined($masterws) ) {
82        print_error("Can't determine master workspace environment.\n"
83                    . "Please initialize environment with setsolar ...", 1);
84    }
85
86    if ( !defined($childws) ) {
87        print_error("Can't determine child workspace environment.\n"
88                    . "Please initialize environment with setsolar ...", 1);
89    }
90
91    my $cws = Cws->new();
92    $cws->child($childws);
93    $cws->master($masterws);
94
95    my $mime_type  = $opt_mime_type  ? $opt_mime_type  : find_mime_type($filename);
96
97    no strict;
98
99    if ( is_valid_cws($cws) ) {
100        #print "CWS is valid filename=" . $filename . " mime_type=" . $mime_type . "\n";
101        open(DATA,"<$filename") || die "can't open filename";
102        $data="";
103	while(<DATA>) {
104		$data.=$_;
105	}
106        my $result=$cws->save_attachment($filename,$mime_type,$data);
107    } else {
108        print STDERR "cws is not valid";
109    }
110    exit(0)
111}
112
113
114sub find_mime_type
115{
116    my $filename = shift;
117    $filename=~/(.*)\.(.*$)/;
118    my $ext=$2;
119    my $fmime='';
120
121    if ( defined($ext) ) {
122        open(MIME,"< $ENV{SOLARENV}/inc/mime.types")|| die "can not open mimetype file";
123        while (<MIME>) {
124            my @a=split();
125            my $iscomment=0;
126            if ( /(\s*\#).*/ ) {
127                $iscomment=1;
128            } else {
129                $iscomment=0;
130            }
131            if ( $iscomment eq 0 && $#a >= 1 && $fmime eq '' ) {
132                my $i=1;
133                for ($i=1; $i<=$#a; $i++) {
134                    if ( $a[$i] eq $ext ) {
135                        $fmime=$a[0];
136                    }
137                }
138            }
139        }
140
141    }
142    if ( $fmime eq '' ) {
143        $fmime="application/octet-stream";
144    }
145    return $fmime;
146}
147
148
149sub is_valid_cws
150{
151    my $cws = shift;
152
153    my $masterws = $cws->master();
154    my $childws  = $cws->child();
155    # check if we got a valid child workspace
156    my $id = $cws->eis_id();
157    if ( !$id ) {
158        print_error("Child workspace '$childws' for master workspace '$masterws' not found in EIS database.", 2);
159    }
160    print_message("Master workspace '$masterws', child workspace '$childws':");
161    return 1;
162}
163
164sub parse_options
165{
166    # parse options and do some sanity checks
167    my $help = 0;
168    my $success = GetOptions('h' => \$help, 'm=s' => \$opt_master, 'c=s'=> \$opt_child, 't=s'=> \$opt_mime_type);
169    if ( $help || !$success || $#ARGV < 0 ) {
170        usage();
171        exit(1);
172    }
173
174    return $ARGV[0];
175}
176
177sub print_message
178{
179    my $message     = shift;
180
181    print STDERR "$script_name: ";
182    print STDERR "$message\n";
183    return;
184}
185
186sub print_error
187{
188    my $message     = shift;
189    my $error_code  = shift;
190
191    print STDERR "$script_name: ";
192    print STDERR "ERROR: $message\n";
193
194    if ( $error_code ) {
195        print STDERR "\nFAILURE: $script_name aborted.\n";
196        exit($error_code);
197    }
198    return;
199}
200
201sub usage
202{
203    print STDERR "Usage: cwsattach [-h] [-m master] [-c child] [-t mimetype] filename\n";
204    print STDERR "\n";
205    print STDERR "Attach files to CWS in EIS database\n";
206    print STDERR "\n";
207    print STDERR "Options:\n";
208    print STDERR "\t-h\t\thelp\n";
209    print STDERR "\t-m master\toverride MWS specified in environment\n";
210    print STDERR "\t-c child\toverride CWS specified in environment\n";
211    print STDERR "\t-t mimetype\texplicitly set mime type\n";
212    print STDERR "Examples:\n";
213    print STDERR "\tcwsattach barfoo.html\n";
214    print STDERR "\tcwsattach -t text bar.cxx\n";
215    print STDERR "\tcwsattach -t text/rtf foo.rtf\n";
216}
217