1
+ # !/usr/bin/perl
2
+ # ##########################################################################
3
+ # Makefile for pkgdiff
4
+ # Install/remove the tool for GNU/Linux and FreeBSD
5
+ #
6
+ # Copyright (C) 2011-2012 ROSA Laboratory.
7
+ #
8
+ # Written by Andrey Ponomarenko
9
+ #
10
+ # This program is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License or the GNU Lesser
12
+ # General Public License as published by the Free Software Foundation.
13
+ #
14
+ # This program is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # and the GNU Lesser General Public License along with this program.
21
+ # If not, see <http://www.gnu.org/licenses/>.
22
+ # ##########################################################################
23
+ use Getopt::Long;
24
+ Getopt::Long::Configure (" posix_default" , " no_ignore_case" );
25
+ use File::Path qw( mkpath rmtree) ;
26
+ use File::Copy qw( copy) ;
27
+ use File::Basename qw( dirname) ;
28
+ use Cwd qw( abs_path) ;
29
+ use File::Find;
30
+ use Config;
31
+ use strict;
32
+
33
+ my $TOOL_SNAME = " pkgdiff" ;
34
+ my $ARCHIVE_DIR = abs_path(dirname($0 ));
35
+
36
+ my $HELP_MSG = "
37
+ NAME:
38
+ Makefile for pkgdiff
39
+
40
+ DESCRIPTION:
41
+ Install $TOOL_SNAME command and private modules.
42
+
43
+ USAGE:
44
+ sudo perl $0 -install -prefix=/usr
45
+ sudo perl $0 -update -prefix=/usr
46
+ sudo perl $0 -remove -prefix=/usr
47
+
48
+ OPTIONS:
49
+ -h|-help
50
+ Print this help.
51
+
52
+ --prefix=PREFIX
53
+ Install files in PREFIX [/usr/local].
54
+
55
+ -install
56
+ Command to install the tool.
57
+
58
+ -update
59
+ Command to update existing installation.
60
+
61
+ -remove
62
+ Command to remove the tool.
63
+
64
+ EXTRA OPTIONS:
65
+ --destdir=DESTDIR
66
+ This option is for maintainers to build
67
+ RPM or DEB packages inside the build root.
68
+ The environment variable DESTDIR is also
69
+ supported.
70
+ \n " ;
71
+
72
+ if (not @ARGV ) {
73
+ print $HELP_MSG ;
74
+ exit (0);
75
+ }
76
+
77
+ my ($PREFIX , $DESTDIR , $Help , $Install , $Update , $Remove );
78
+
79
+ GetOptions(
80
+ " h|help!" => \$Help ,
81
+ " prefix=s" => \$PREFIX ,
82
+ " destdir=s" => \$DESTDIR ,
83
+ " install!" => \$Install ,
84
+ " update!" => \$Update ,
85
+ " remove!" => \$Remove
86
+ ) or exit (1);
87
+
88
+ sub scenario ()
89
+ {
90
+ if ($Help ) {
91
+ print $HELP_MSG ;
92
+ exit (0);
93
+ }
94
+ if (not $Install and not $Update and not $Remove ) {
95
+ print STDERR " ERROR: command is not selected (-install, -update or -remove)\n " ;
96
+ exit (1);
97
+ }
98
+ if ($PREFIX ne " /" ) {
99
+ $PREFIX =~s / [\/ ]+\Z // g ;
100
+ }
101
+ if (not $PREFIX )
102
+ { # default prefix
103
+ $PREFIX = " /usr/local" ;
104
+ }
105
+ if (my $Var = $ENV {" DESTDIR" })
106
+ {
107
+ print " Using DESTDIR environment variable\n " ;
108
+ $DESTDIR = $Var ;
109
+ }
110
+ if ($DESTDIR )
111
+ {
112
+ if ($DESTDIR ne " /" ) {
113
+ $DESTDIR =~s / [\/ ]+\Z // g ;
114
+ }
115
+ if ($DESTDIR !~/ \A\/ / ) {
116
+ print STDERR " ERROR: destdir is not absolute path\n " ;
117
+ exit (1);
118
+ }
119
+ if (not -d $DESTDIR ) {
120
+ print STDERR " ERROR: you should create destdir directory first\n " ;
121
+ exit (1);
122
+ }
123
+ $PREFIX = $DESTDIR .$PREFIX ;
124
+ if (not -d $PREFIX ) {
125
+ print STDERR " ERROR: you should create installation directory first (destdir + prefix):\n mkdir -p $PREFIX \n " ;
126
+ exit (1);
127
+ }
128
+ }
129
+ else
130
+ {
131
+ if ($PREFIX !~/ \A\/ / ) {
132
+ print STDERR " ERROR: prefix is not absolute path\n " ;
133
+ exit (1);
134
+ }
135
+ if (not -d $PREFIX ) {
136
+ print STDERR " ERROR: you should create prefix directory first\n " ;
137
+ exit (1);
138
+ }
139
+ }
140
+
141
+ print " INSTALL PREFIX: $PREFIX \n " ;
142
+
143
+ # paths
144
+ my $EXE_PATH = " $PREFIX /bin" ;
145
+ my $MODULES_PATH = " $PREFIX /share/$TOOL_SNAME " ;
146
+ my $REL_PATH = " ../share/$TOOL_SNAME " ;
147
+
148
+ if (not -w $PREFIX ) {
149
+ print STDERR " ERROR: you should be root\n " ;
150
+ exit (1);
151
+ }
152
+ if ($Remove or $Update )
153
+ {
154
+ # remove executable
155
+ print " -- Removing $EXE_PATH /$TOOL_SNAME \n " ;
156
+ unlink ($EXE_PATH ." /" .$TOOL_SNAME );
157
+
158
+ # remove modules
159
+ print " -- Removing $MODULES_PATH \n " ;
160
+ rmtree($MODULES_PATH );
161
+ }
162
+ if ($Install or $Update )
163
+ {
164
+ if (-e $EXE_PATH ." /" .$TOOL_SNAME or -e $MODULES_PATH )
165
+ { # check installed
166
+ if (not $Remove ) {
167
+ print STDERR " ERROR: you should remove old version first (`sudo perl $0 -remove --prefix=$PREFIX `)\n " ;
168
+ exit (1);
169
+ }
170
+ }
171
+
172
+ # configure
173
+ my $Content = readFile($ARCHIVE_DIR ." /" .$TOOL_SNAME ." .pl" );
174
+ if ($DESTDIR ) { # relative path
175
+ $Content =~s / MODULES_INSTALL_PATH/ $REL_PATH / ;
176
+ }
177
+ else { # absolute path
178
+ $Content =~s / MODULES_INSTALL_PATH/ $MODULES_PATH / ;
179
+ }
180
+
181
+ # copy executable
182
+ print " -- Installing $EXE_PATH /$TOOL_SNAME \n " ;
183
+ mkpath($EXE_PATH );
184
+ writeFile($EXE_PATH ." /" .$TOOL_SNAME , $Content );
185
+ chmod (0755, $EXE_PATH ." /" .$TOOL_SNAME );
186
+
187
+ # copy modules
188
+ if (-d $ARCHIVE_DIR ." /modules" )
189
+ {
190
+ print " -- Installing $MODULES_PATH \n " ;
191
+ mkpath($MODULES_PATH );
192
+ copyDir($ARCHIVE_DIR ." /modules" , $MODULES_PATH );
193
+ my $TOOLS_PATH = $MODULES_PATH ." /modules/Internals/Tools" ;
194
+ my @Tools = listDir($TOOLS_PATH );
195
+ foreach my $Tool (@Tools ) {
196
+ chmod (0755, $TOOLS_PATH ." /" .$Tool );
197
+ }
198
+
199
+ }
200
+
201
+ # check PATH
202
+ if ($ENV {" PATH" }!~/ (\A |:)\Q $EXE_PATH \E (\Z |:)/ ) {
203
+ print " WARNING: your PATH variable doesn't include \' $EXE_PATH \'\n " ;
204
+ }
205
+ }
206
+ exit (0);
207
+ }
208
+
209
+ sub listDir ($)
210
+ {
211
+ my $Path = $_ [0];
212
+ return () if (not $Path or not -d $Path );
213
+ opendir (my $DH , $Path );
214
+ return () if (not $DH );
215
+ my @Contents = grep { $_ ne " ." && $_ ne " .." } readdir ($DH );
216
+ return @Contents ;
217
+ }
218
+
219
+ sub copyDir ($$)
220
+ {
221
+ my ($From , $To ) = @_ ;
222
+ my %Files ;
223
+ find(\&wanted, $From );
224
+ sub wanted {
225
+ $Files {$File::Find::dir ." /$_ " } = 1 if ($_ ne " ." );
226
+ }
227
+ foreach my $Path (sort keys (%Files ))
228
+ {
229
+ my $Inst = $Path ;
230
+ $Inst =~s /\A\Q $ARCHIVE_DIR\E / $To / ;
231
+ if (-d $Path )
232
+ { # directories
233
+ mkpath($Inst );
234
+ }
235
+ else
236
+ { # files
237
+ copy($Path , $Inst );
238
+ }
239
+ }
240
+ }
241
+
242
+ sub readFile ($)
243
+ {
244
+ my $Path = $_ [0];
245
+ return " " if (not $Path or not -f $Path );
246
+ open (FILE, $Path ) || die (" can't open file \' $Path \' : $! \n " );
247
+ local $/ = undef ;
248
+ my $Content = <FILE>;
249
+ close (FILE);
250
+ return $Content ;
251
+ }
252
+
253
+ sub writeFile ($$)
254
+ {
255
+ my ($Path , $Content ) = @_ ;
256
+ return if (not $Path );
257
+ open (FILE, " >" .$Path ) || die (" can't open file \' $Path \' : $! \n " );
258
+ print FILE $Content ;
259
+ close (FILE);
260
+ }
261
+
262
+ scenario();
0 commit comments