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