-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkpackage.pl
executable file
·442 lines (391 loc) · 9.4 KB
/
mkpackage.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $home = $ENV{HOME};
my $upload = 1;
my $rpm = 1;
my $deb = 1;
my $debug = 0;
GetOptions(
'upload!' => \$upload,
'rpm!' => \$rpm,
'deb!' => \$deb,
'debug' => \$debug
);
if(RepoClean() == 0) {
print "Repo is modified. Please checkin changes or revert changes before making a package.\n";
exit;
}
my ($main,$sub,$rev) = GetVersion();
my $name = "trsh-$main.$sub-$rev";
if(RunTests() == 0) {
print "Tests failed. No point in making a package.\n";
exit;
}
print "making package for version = $main.$sub-$rev\n";
# Clean junk.
system("rm -rf $home/$name") if(-d "$home/$name");
system("rm -rf $home/$name.tar.gz") if(-e "$home/$name.tar.gz");
system("rm -rf $home/trsh-build") if(-d "$home/trsh-build");
system("mkdir $home/trsh-build");
# Get an archive from the repo
system("git archive --prefix=$name/ HEAD | gzip > $home/$name.tar.gz");
chdir($home);
system("tar -xf $name.tar.gz");
chdir "$name.tar.gz";
unlink "mkpackage.pl";
unlink "checkin.pl";
unlink "test-trsh.bash";
unlink "VERSION";
chdir "$home";
unlink "$name.tar.gz";
system("mv $name $name.src");
chdir("$name.src");
system("gzip trsh.1");
chdir($home);
MakeTGZ();
if($rpm) {
MakeRPM();
}
if($deb) {
MakeDEB();
}
chdir("$home");
system("rm -rf $name.src");
chdir "$home/trsh-build";
#############################################################################################
##################################### FUNCTIONS #############################################
#############################################################################################
sub GetVersion
{
my $verstr = `cat VERSION`;
if($verstr =~ /(\d+)\.(\d+)-(\d+)/) {
return ($1,$2,$3);
}
print "ERROR In reading VERSION File.\n";
exit;
}
sub RepoClean
{
my $srev = `git status`;
if($srev =~ /modified:\s+/) {
return 0;
}
if($srev =~ /added:\s+/) {
return 0;
}
if($srev =~ /deleted:\s+/) {
return 0;
}
return 1;
}
sub RunTests
{
if(-e "/bin/bash"){
if(system("./test-trsh.bash") != 0){
return 0;
}
} else {
print "Tests are not performed for bash as the shell was not found on your system. Please install it for a better package.\n";
return 0;
}
return 1;
}
sub IsCommandInstalled
{
my $cmd = shift;
my $output = `which $cmd`;
if($output =~ /no $cmd in/) {
return 0;
}
if($output =~ /^\s*$/) {
return 0;
}
return 1;
}
sub MakeTGZ
{
system("cp -r $name.src $name");
system("tar -zcf $name.tar.gz $name");
system("rm -rf $name");
system("mv $name.tar.gz trsh-build");
}
sub MakeRPM
{
# Check if rpmbuild exists.
if(IsCommandInstalled("rpmbuild") == 0) {
print "rpmbuild is not installed on the system. Skipping rpm generation.\n";
return;
}
my $packages_dir = "$home/packages";
system("rm -f $home/.rpmmacros") if(-e "$home/.rpmmacros");
Create_File("$home/.rpmmacros",'%_topdir ' . "$home/packages");
system("rm -rf $packages_dir") if(-d $packages_dir);
system("mkdir -p $packages_dir/BUILD");
system("mkdir -p $packages_dir/RPMS");
system("mkdir -p $packages_dir/RPMS/i386");
system("mkdir -p $packages_dir/RPMS/i686");
system("mkdir -p $packages_dir/RPMS/noarch");
system("mkdir -p $packages_dir/SOURCES");
system("mkdir -p $packages_dir/SPECS");
system("mkdir -p $packages_dir/SRPMS");
system("cp -r $name.src $name");
system("rm $name/configure.pl");
system("tar -zcf $name.tar.gz $name");
system("rm -rf $name");
system("mv $name.tar.gz $packages_dir/SOURCES") == 0 or die "could not move source to SOURCE dir\n";
Create_File("trsh.spec", RPM_control() . RPM_description() . RPM_prep() . RPM_post() . RPM_install() . RPM_preun() . RPM_postun() . RPM_verify() . RPM_files());
system("rpmbuild -bb trsh.spec > /dev/null 2> /dev/null") == 0 or die "rpmbuild failed\n";
system("mv $packages_dir/RPMS/noarch/$name.noarch.rpm trsh-build");
system("rm -rf $name");
system("rm -rf trsh.spec");
system("rm -rf $packages_dir");
}
sub MakeDEB
{
if(IsCommandInstalled("dpkg") == 0) {
print "dpkg is not installed on the system. Skipping deb generation.\n";
return;
}
system("mkdir $name");
system("mkdir $name/DEBIAN");
Create_File("$name/DEBIAN/control", DEB_control());
Create_File("$name/DEBIAN/postinst", DEB_postinst());
Create_File("$name/DEBIAN/prerm", DEB_prerm());
system("chmod 0755 $name/DEBIAN/postinst");
system("chmod 0755 $name/DEBIAN/prerm");
system("mkdir -p $name/usr/bin");
system("mkdir -p $name/usr/share/doc/trsh");
system("mkdir -p $name/usr/share/man/man1");
system("cp $name.src/trsh.pl $name/usr/bin");
system("chmod +x $name/usr/bin/trsh.pl");
system("cp $name.src/trsh.1.gz $name/usr/share/man/man1");
system("cp $name.src/README $name/usr/share/doc/trsh");
system("cp $name.src/COPYING.GPL $name/usr/share/doc/trsh/copyright");
system("dpkg -b $name");
system("mv $name.deb trsh-build");
if($debug == 0) {
system("rm -rf $name");
}
}
sub RPM_control
{
return "
Summary: A Trash manager aliased to rm.
Name: trsh
Version: $main.$sub
Release: $rev
Group: Utilities
License: GPL
BuildArch: noarch
URL: http://code.google.com/p/trsh
Vendor: Amithash Prasad
Packager: Amithash Prasad <amithash\@gmail.com>
Source: \$RPM_SOURCE_DIR/trsh-$main.$sub-$rev.tar.gz
Provides: trsh
Requires: perl, perl(strict), perl(warnings), perl(File::Basename), perl(File::Spec), perl(Cwd), perl(Getopt::Long), perl(Fcntl), perl(Term::ANSIColor), perl(Term::ReadKey)
";
}
sub RPM_description
{
return '
%description
' . GetDescString();
}
sub RPM_prep
{
return '
%prep
rm -rf $RPM_BUILD_DIR/%name-%version-%release
zcat $RPM_SOURCE_DIR/%name-%version-%release.tar.gz | tar -xvf -
mkdir -p %buildroot/%_bindir
mkdir -p %buildroot/%_mandir/man1
';
}
sub RPM_post
{
return '
%post
if [ $1 -gt 1 ]
then
exit 0
fi
' . AddAlias();
}
sub RPM_install
{
return '
%install
cp $RPM_BUILD_DIR/%name-%version-%release/trsh.pl %buildroot/%_bindir
cp $RPM_BUILD_DIR/%name-%version-%release/trsh.1.gz %buildroot/%_mandir/man1
chmod +x %buildroot/%_bindir/trsh.pl
exit 0
';
}
sub RPM_preun
{
return '
%preun
if [ $1 -gt 0 ]
then
exit 0
fi
' . RemoveAlias() . "\nexit 0\n";
}
sub RPM_postun
{
return '
%postun
rm -f %buildroot/%_bindir/trsh.pl
rm -f %buildroot/%_mandir/man1/trsh.1.gz
exit 0
';
}
sub RPM_verify
{
return '%verifyscript\n\n' . '
TRSH_SHELL=$SHELL
SHELL_NAME=${TRSH_SHELL##/bin/}
for rc in $( ls /etc/*rc* | grep $SHELL_NAME | grep -vP "\.bac$" | grep -vP "\.new$" | grep -vP "\.dpkg" )
do
RC_FILE=$rc
done
if [ -z $RC_FILE ]
then
echo "ERROR! No RC FILE Found"
exit -127
fi
RC_TEST=`grep "# TRSH" $RC_FILE | wc -l`
if [ $RC_TEST -ne 2 ]
then
echo "Alias entries not found in $RC_FILE" >&2
exit -127
fi
if [ ! -e %_bindir/trsh.pl ]
then
echo "trsh.pl not found in %_bindir" >&2
exit -127
fi
if [ ! -e %_mandir/man1/trsh.1.gz ]
then
echo "Man page not found in %_mandir/man1/" >&2
exit -127
fi
exit 0
';
}
sub RPM_files
{
return '
%files
%_bindir/trsh.pl
%_mandir/man1/trsh.1.gz
';
}
sub GetDescString
{
return "A Trash manager aliased to rm.Trsh is a trash manager
with an attitude! Once aliased to rm
it provides a full wrapper to rm enabling the user to use it
just like he/she would with rm, with extra features like trash
listing, undo, recover, etc etc.";
}
sub Create_File
{
my $file = shift;
my $cont = shift;
open OUT,"+>$file" or die "Could not create $file\n";
print OUT "$cont\n";
close(OUT);
}
sub DEB_control
{
return "Package: trsh
Version: $main.$sub-$rev
Architecture: all
Maintainer: Amithash Prasad <amithash\@gmail.com>
Installed-Size: 100
Section: utils
Priority: extra
Depends: perl, libterm-readkey-perl
Description: " . GetDescString();
}
sub DEB_postinst
{
return "#!/bin/bash\n\n" . AddAlias() . "\nexit 0\n";
}
sub DEB_prerm
{
return "#!/bin/bash\n\n" . RemoveAlias() . "\nexit 0\n";
}
sub RemoveAlias
{
return '
TRSH_SHELL=$SHELL
SHELL_NAME=${TRSH_SHELL##/bin/}
if [[ $SHELL_NAME -ne "csh" ]] && [[ $SHELL_NAME -ne "tcsh" ]] && [[ $SHELL_NAME -ne "bash" ]]
then
echo "ERROR! Unsupported shell $SHELL_NAME"
exit -127
fi
for rc in $( ls /etc/*rc* | grep $SHELL_NAME | grep -vP "\.bac$" | grep -vP "\.new$" | grep -vP "\.dpkg" )
do
RC_FILE=$rc
ALIAS_RM=""
ALIAS_UNDO=""
if [[ $SHELL_NAME -eq "bash" ]]
then
ALIAS_RM="alias rm=\"/usr/bin/trsh.pl\" # TRSH"
ALIAS_UNDO="alias undo=\"/usr/bin/trsh.pl -u\" # TRSH"
elif [[ $SHELL_NAME -eq "csh" ]] || [[ $SHELL_NAME -eq "tcsh" ]]
then
ALIAS_RM="alias rm \"/usr/bin/trsh.pl\" # TRSH"
ALIAS_UNDO="alias undo \"/usr/bin/trsh.pl -u\" # TRSH"
fi
sed -e \'/.* # TRSH/d\' $RC_FILE > $RC_FILE.new
mv $RC_FILE.new $RC_FILE
done
if [[ -z $RC_FILE ]]
then
echo "ERROR! No RC FILE Found"
exit -127
fi
';
}
sub AddAlias
{
return '
TRSH_SHELL=$SHELL
SHELL_NAME=${TRSH_SHELL##/bin/}
if [[ $SHELL_NAME -ne "csh" ]] && [[ $SHELL_NAME -ne "tcsh" ]] && [[ $SHELL_NAME -ne "bash" ]]
then
echo "ERROR! Unsupported shell $SHELL_NAME"
exit -127
fi
for rc in $( ls /etc/*rc* | grep $SHELL_NAME | grep -vP "\.bac$" | grep -vP "\.new$" | grep -vP "\.dpkg" )
do
RC_FILE=$rc
ALIAS_RM=""
ALIAS_UNDO=""
if [[ $SHELL_NAME -eq "bash" ]]
then
ALIAS_RM="alias rm=\"/usr/bin/trsh.pl\" # TRSH"
ALIAS_UNDO="alias undo=\"/usr/bin/trsh.pl -u\" # TRSH"
elif [[ $SHELL_NAME -eq "csh" ]] || [[ $SHELL_NAME -eq "tcsh" ]]
then
ALIAS_RM="alias rm \"/usr/bin/trsh.pl\" # TRSH"
ALIAS_UNDO="alias undo \"/usr/bin/trsh.pl -u\" # TRSH"
fi
sed -e \'/.* # TRSH/d\' $RC_FILE > $RC_FILE.new
echo $ALIAS_RM >> $RC_FILE.new
echo $ALIAS_UNDO >> $RC_FILE.new
mv $RC_FILE.new $RC_FILE
done
if [[ -z $RC_FILE ]]
then
echo "ERROR! No RC FILE Found"
exit -127
fi
';
}