source: main/trunk/greenstone2/bin/script/gs-magick.pl@ 24612

Last change on this file since 24612 was 24612, checked in by ak19, 13 years ago

Windows specific changes to gs-magick.pl and setup.bat: bat file used to add bin\windows\imagemagick to path (not bin\windows\imagemagick\bin). It no longer does that, and gs-magick.pl sets this.

  • Property svn:executable set to *
File size: 6.2 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2009 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27
28# gs-magick.pl:
29# Script to set the environment for imagemagick and then run it, returning
30# both the exit code and printing the output to STDOUT.
31# Setting the env vars necessary for imagemagick here locally means
32# they won't interfere with the normal environment it would have
33# if the environment had been set in setup.bash/setup.bat instead
34
35
36BEGIN {
37 die "GSDLHOME not set - run the (gs3-)setup script\n" unless defined $ENV{'GSDLHOME'};
38 die "GSDLOS not set - run (gs3-)setup script\n" unless defined $ENV{'GSDLOS'};
39 if($ENV{'GSDLOS'} =~ m/windows/) { # windows doesn't (yet) define GSDLARCH env var
40 $ENV{'GSDLARCH'} = "" unless defined $ENV{'GSDLARCH'};
41 }
42 die "GSDLARCH not set - run (gs3-)setup script\n" unless defined $ENV{'GSDLARCH'};
43 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
44}
45
46
47use strict;
48no strict 'refs'; # make an exception so we can use variables as filehandles
49use util;
50
51
52sub main
53{
54 my ($argc,@argv) = @_;
55
56 my $usage = "Usage: $0 [--usage|--verbosity <num>] <imagick-command> <arguments to imagick command>";
57
58 my $verbosity = 0;
59 my $magick_cmd = "";
60
61
62 # Construct the imagemagick cmd string from all the arguments,
63 # embedding any arguments that contain spaces in quotes.
64 # We'll remove the --options afterwards.
65 my $count = 0;
66 for ($count = 0; $count < scalar(@argv); $count++) {
67 if($argv[$count] =~ m/ /) {
68 $argv[$count] = "\"".$argv[$count]."\"";
69 }
70 $magick_cmd = "$magick_cmd $argv[$count]";
71 }
72
73 # process the --options in the imagemagick command
74 # Tried using the official GetOptions module to parse options, except that
75 # the --verbosity|--v option to gs-magick.pl interfered with the -verbose
76 # option that image-magick accepts.
77
78 if($magick_cmd =~ m/--usage/) {
79 print STDERR "$usage\n";
80 exit(0);
81 }
82
83 $magick_cmd =~ s/\s*--verbosity(\s+|=)(\d*)\s*/ /; # --verbosity=4 or --verbosity 4
84 $verbosity = $2 if defined $2; # print STDERR "subst 2 is : $2\n" if defined $2;
85
86 if($verbosity > 2) {
87 print STDERR "***** Running MAGICK_CMD: $magick_cmd\n";
88 #print STDERR "***** verbosity: $verbosity\n";
89 }
90
91 ## SET THE ENVIRONMENT AS USED TO BE DONE IN SETUP.BASH/BAT
92
93 my $magick_home = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}.$ENV{'GSDLARCH'},"imagemagick");
94 if (-d $magick_home) { # "$GSDLHOME/bin/$GSDLOS$GSDLARCH/imagemagick"
95 $ENV{'MAGICK_HOME'} = $magick_home;
96 }
97
98 # if Greenstone came with imagick, or if the user otherwise has an
99 # imagick to fall back on (and set MAGICK_HOME that way) we use that
100 if(defined $ENV{'MAGICK_HOME'} && -d $ENV{'MAGICK_HOME'}) {
101 if($ENV{'GSDLOS'} =~ m/windows/) {
102 &util::envvar_prepend("PATH", $ENV{'MAGICK_HOME'}); # the imagemagick folder (no bin therein)
103 }
104
105 else { # linux and mac
106 &util::envvar_prepend("PATH", &util::filename_cat($ENV{'MAGICK_HOME'}, "bin"));
107
108 my $magick_lib = &util::filename_cat($ENV{'MAGICK_HOME'}, "lib");
109 if($ENV{'GSDLOS'} eq "linux") {
110 &util::envvar_prepend("LD_LIBRARY_PATH", $magick_lib);
111 } elsif ($ENV{'GSDLOS'} eq "darwin") {
112 &util::envvar_prepend("DYLD_LIBRARY_PATH", $magick_lib);
113 }
114 }
115
116 if($verbosity > 2) {
117 print STDERR "\t*** MAGICK_HOME".$ENV{'MAGICK_HOME'}."\n";
118 print STDERR "\t*** LD_LIB_PATH".$ENV{'LD_LIBRARY_PATH'}."\n" if defined $ENV{'LD_LIBRARY_PATH'};
119 print STDERR "\t*** DYLD_LIB_PATH".$ENV{'DYLD_LIBRARY_PATH'}."\n" if defined $ENV{'DYLD_LIBRARY_PATH'};
120 print STDERR "\t*** PATH".$ENV{'PATH'}."\n\n";
121 }
122 }
123
124 # if no MAGICK_HOME, maybe they want to run with the system imagemagick
125 elsif($verbosity > 2) {
126 print STDERR "**** No ImageMagick in Greenstone. Will try to use any imagemagick on the system.\n\n";
127 }
128
129 # RUN THE IMAGEMAGICK COMMAND
130
131 # John Thompson's manner of using backticks to preserve the output of
132 # running imagemagick.
133 # $? contains the exit code of running the imagemagick command.
134 # This needs to be shifted by 8 and then converted to be a signed value
135 # to work out the actual exit code value.
136
137 #my $result = `$magick_cmd`; # This way will trap STDOUT into local variable
138
139 my $result = "";
140 if (!open(PIN, "$magick_cmd |")) {
141 print STDERR "*** Can't run $magick_cmd. Error was: $!";
142 } else {
143 while (defined (my $imagick_output_line = <PIN>)) {
144 $result = $result.$imagick_output_line;
145 }
146 close(PIN);
147 }
148
149 # Perl Special Variables http://www.kichwa.com/quik_ref/spec_variables.html
150 # $? The status returned by the last pipe close, backtick(``) command or system operator.
151 # Note that this is the status word returned by the wait() system call, so the exit value
152 # of the subprocess is actually ($? >>*). $? & 255 gives which signal, if any, the process
153 # died from, and whether there was a core dump.
154
155 # Shift by 8 to get a value between 0 and 255, then work out if it is signed or unsigned
156 # http://stackoverflow.com/questions/2726447/why-is-the-exit-code-255-instead-of-1-in-perl
157 my $status = $?;
158 $status >>= 8;
159 $status = (($status & 0x80) ? -(0x100 - ($status & 0xFF)) : $status);
160
161 # send the output to STDOUT, since calling functions may call gs-magick.pl with backticks
162 print STDOUT $result;
163 exit($status);
164}
165
166&main(scalar(@ARGV),@ARGV);
Note: See TracBrowser for help on using the repository browser.