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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
#!/bin/sh
# $FreeBSD$
# Copyright (c) 2007 Andy Kosela <andy.kosela@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
trap trap_exit 2
pkg=$@
root() {
if [ `id -u` != 0 ]; then
echo "Access denied. Root privileges required."
exit 0
fi
}
trap_exit() {
echo -e "\n*** SIGINT detected ***"
exit 1
}
installed() {
a=pkg_installed
}
missing() {
echo "It seems you typed the port which is not available in /usr/ports/"
echo "Check your port's name and try again."
exit 0
}
init_check() {
pkg_chk=`pkg_info -xoq $pkg 2>/dev/null`
[ ! -z "$pkg_chk" ] && installed
if [ -z "$a" ]; then
port_chk=`whereis -sq $pkg`
[ -z "$port_chk" ] && missing
fi
}
main() {
root
if [ -z "$pkg" ]; then
echo "bpkg: you didn't specify any package to install/upgrade."
exit 0
fi
init_check
if [ -z "$a" ]; then
pkg_install=`whereis -sq $pkg | cut -d/ -f5 | awk '{ORS=" " ; print}'`
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
echo -n "Do you want to install "$pkg_install"using binary packages [y] "
read key
case $key in
n) echo "Installing new port(s):"
for e in `whereis -sq $pkg`; do
echo -n "Do you want to set config options recursively? [y] "
read key2
case $key2 in
n) for e in `whereis -sq $pkg`; do
cd $e && make install clean
done
exit 0;;
*) for e in `whereis -sq $pkg`; do
cd $e && make config-conditional
cd $e && make install clean
done
exit 0;;
esac
done
exit 0;;
*) echo "Installing new package(s):"
pkg_add -r $pkg_install
exit 0;;
esac
fi
pkg_install=`pkg_info -xoq $pkg | cut -d/ -f2 | awk '{ORS=" " ; print}'`
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo -n "Do you want to upgrade "$pkg_install"using binary packages? [y] "
read key
case $key in
n) echo "Upgrading port(s):"
for e in `pkg_info -xoq $pkg`; do
c=`echo $e | cut -d/ -f2`
pkg_delete -rix $c
if [ ! -z `pkg_info -xoq $c 2>/dev/null` ]; then
echo "*** cannot proceed without first removing the package ***"
exit 0
fi
echo -n "Do you want to explicitly set config options? [n] "
read key2
case $key2 in
y) for e in `pkg_info -xoq $pkg`; do
cd /usr/ports/$e && make config
cd /usr/ports/$e && make install clean
done
exit 0;;
*) for e in `pkg_info -xoq $pkg`; do
cd /usr/ports/$e && make install clean
done
exit 0;;
esac
done
exit 0;;
*) echo "Upgrading package(s):"
pkg_delete -fix $pkg_install && pkg_add -r $pkg_install
exit 0;;
esac
}
simulation() {
root
init_check
if [ -z "$a" ]; then
pkg_install=`whereis -sq $pkg | cut -d/ -f5 | awk '{ORS=" " ; print}'`
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
echo -n "Do you want to install "$pkg_install"using binary packages (simulation mode) [y] "
read key
case $key in
n) echo "Installing new port(s): $pkg_install"
exit 0;;
*) echo "Installing new package(s):"
pkg_add -rn $pkg_install
exit 0;;
esac
fi
pkg_install=`pkg_info -xoq $pkg | cut -d/ -f2 | awk '{ORS=" " ; print}'`
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo -n "Do you want to upgrade "$pkg_install"using binary packages? (simulation mode) [y] "
read key
case $key in
n) echo "Upgrading port(s): $pkg_install"
exit 0;;
*) echo "Upgrading package(s):"
pkg_delete -ixn $pkg_install && pkg_add -rn $pkg_install
exit 0;;
esac
}
non-interactive() {
root
init_check
if [ -z "$a" ]; then
pkg_install=`whereis -sq $pkg | cut -d/ -f5 | awk '{ORS=" " ; print}'`
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
echo "Installing new package(s):"
pkg_add -r $pkg_install
exit 0
fi
pkg_install=`pkg_info -xoq $pkg | cut -d/ -f2 | awk '{ORS=" " ; print}'`
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo "Upgrading package(s):"
pkg_delete -fx $pkg_install && pkg_add -r $pkg_install
exit 0
}
all() {
root
echo ""
echo -n "Do you want to upgrade all installed packages using binary packages? (interactive mode) [y] "
read key
case $key in
n) echo "Upgrading all ports:"
for e in `pkg_info -aoq`; do
c=`echo "$e" | cut -d/ -f2`
pkg_delete -rix $c
cd /usr/ports/$e && make install clean
done
exit 0;;
*) echo "Upgrading all packages:"
all=`ls -l /var/db/pkg/ | awk '{print $9}' | sed 's/\(.*\)-.*/\1/g' | awk '{ORS=" " ;print}'`
pkg_delete -ifa && pkg_add -r $all
exit 0;;
esac
}
all_non-interactive() {
root
echo ""
echo -n "Do you want to upgrade all installed packages using binary packages? (non-interactive mode) [y] "
read key
case $key in
n) echo "Upgrading all ports:"
for e in `pkg_info -aoq $pkg`; do
c=`echo $e | cut -d/ -f2`
pkg_delete -rx $c
cd /usr/ports/$e && make install clean
done
exit 0;;
*) echo "Upgrading all packages:"
all=`ls -l /var/db/pkg/ | awk '{print $9}' | sed 's/\(.*\)-.*/\1/g' | awk '{ORS=" " ;print}'`
pkg_delete -fa && pkg_add -r $all
exit 0;;
esac
}
pkg_info-xI() {
init_check
if [ -z "$a" ]; then
echo -e "\033[33m*** not installed ***\033[0m"
path=`whereis -sq $pkg`
echo "$path: `cat $path/Makefile | grep COMMENT | cut -d= -f2`"
else
echo -e "\033[33m*** installed ***\033[0m"
pkg_info -xI $pkg
fi
exit 0
}
pkg_info-xL() {
z=`pkg_info -xL $pkg 2>/dev/null`
if [ -z "$z" ]; then
echo "bpkg: no installed packages match pattern(s)"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo "$z"
fi
exit 0
}
pkg_inf() {
pkg_info
echo ""
echo "***`pkg_info | wc -l` packages installed ***"
exit 0
}
pkg_info-xs() {
z=`pkg_info -xs $pkg 2>/dev/null`
if [ -z "$z" ]; then
echo "bpkg: no installed packages match pattern(s)"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo "$z"
fi
exit 0
}
pkg_info-xr() {
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
for e in $pkg; do
path=`whereis -sq $e`
echo "$pkg: `cat $path/Makefile | grep COMMENT | cut -d= -f2`"
cd /usr/ports && make search name=^$e-[0-9] | grep .*-deps
echo ""
done
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
pkg_info -xrR $pkg
fi
exit 0
}
pkg_info-xg() {
z=`pkg_info -xg $pkg 2>/dev/null`
if [ -z "$z" ]; then
echo "bpkg: no installed packages match pattern(s)"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo "$z"
fi
exit 0
}
pkg_info-xf() {
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
for e in $pkg; do
path=`whereis -sq $e`
echo "$pkg: `cat $path/Makefile | grep COMMENT | cut -d= -f2`"
echo ""
cat $path/pkg-plist
echo ""
done
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
pkg_info -xf $pkg
fi
exit 0
}
pkg_info-xk() {
z=`pkg_info -xk $pkg 2>/dev/null`
if [ -z "$z" ]; then
echo "bpkg: no installed packages match pattern(s)"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo "$z"
fi
exit 0
}
pkg_info-x() {
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
path=`whereis -sq $pkg`
echo "$pkg: `cat $path/Makefile | grep COMMENT | cut -d= -f2`"
echo ""
echo "Description:"
echo "`cat $path/pkg-descr`"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
pkg_info -x $pkg
fi
exit 0
}
pkg_ver() {
pkg_version -Iv
echo ""
echo "***`pkg_info | wc -l` packages installed ***"
exit 0
}
pkg_path() {
for e in `ls -l /usr/ports/*/ | grep $pkg | awk '{print $9}'`; do
path=`whereis -sq $e`
echo "$path" ; cat $path/Makefile | grep COMMENT | cut -d= -f2
done
exit 0
}
pkg_makefile() {
init_check
path=`whereis -sq $pkg`
less $path/Makefile 2>/dev/null
exit 0
}
make_search() {
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo -e "\033[33m*** not installed ***\033[0m"
echo ""
for e in $pkg; do
cd /usr/ports && make search name=^$e
done
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo -e "\033[33m*** installed ***\033[0m"
echo ""
for e in $pkg; do
name=`pkg_info -xoq $e`
for i in $name; do
cd /usr/ports && make search path=$i$
done
done
fi
exit 0
}
make_search_w() {
cd /usr/ports && make search name=$pkg
exit 0
}
pkg_find() {
arch=`uname -m`
sys=`uname -r | cut -d- -f1,2 | tr '[:upper:]' '[:lower:]'`
ftp -4Va ftp://ftp.freebsd.org/pub/FreeBSD/ports/$arch/packages-$sys/Latest/
exit 0
}
pkg_delete-xr() {
root
z=`pkg_info -xoQ $pkg 2>/dev/null`
if [ -z "$z" ]; then
echo "bpkg: no installed packages match pattern(s)"
exit 0
fi
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo -n "Do you want to use recursive removal? [y] "
read key
case $key in
n) echo -n "Do you want to use force removal? [n] "
read key2
case $key2 in
y) pkg_delete -xf $pkg
exit 0;;
*) pkg_delete -x $pkg
exit 0;;
esac;;
*) echo -n "Do you want to use force removal? [n] "
read key2
case $key2 in
y) pkg_delete -xrfi $pkg
exit 0;;
*) pkg_delete -xri $pkg
exit 0;;
esac;;
esac
}
pkg_clean() {
root
echo "Cleaning /usr/ports/distfiles/*"
rm -rf /usr/ports/distfiles/* 1>/dev/null 2>&1
echo "Done."
exit 0
}
pkg_clean2() {
root
echo "Cleaning /usr/ports/distfiles/* and /usr/ports/*/*/work/"
rm -rf /usr/ports/distfiles/* 1>/dev/null 2>&1 ; rm -rf /usr/ports/*/*/work/ 1>/dev/null 2>&1
echo "Done."
exit 0
}
pkg_which-() {
grep "$pkg"$ /var/db/pkg/*/+CONTENTS | awk -F 'pkg/' '{ print $2 }' | \
sed '/@dirrm/ d ; /@unexec/ d ; /@comment/ d ; s/\/+CONTENTS//g ; s/:@comment .*ORIGIN//g' | \
awk -F: '{ print $1 ":\t/usr/local/" $2 }'
exit 0
}
make_showc() {
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
fi
echo -n "Do you want to display config options recursively? [n] "
read key
case $key in
y) for e in `whereis -sq $pkg`; do
cd $e && make showconfig-recursive
done
exit 0;;
*) for e in `whereis -sq $pkg`; do
cd $e && make showconfig
done
exit 0;;
esac
}
make_rmc() {
root
if [ -z "$pkg" ]; then
echo "bpkg: you didn't specify any package/port."
exit 0
fi
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
fi
echo -n "Do you want to remove config options recursively? [n] "
read key
case $key in
y) for e in `whereis -sq $pkg`; do
cd $e && make rmconfig-recursive
done
exit 0;;
*) for e in `whereis -sq $pkg`; do
cd $e && make rmconfig
done
exit 0;;
esac
}
make_config() {
root
if [ -z "$pkg" ]; then
echo "bpkg: you didn't specify any package/port."
exit 0
fi
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
fi
echo -n "Do you want to set config options recursively? [n] "
read key
case $key in
y) for e in `whereis -sq $pkg`; do
cd $e && make config-recursive
done
exit 0;;
*) for e in `whereis -sq $pkg`; do
cd $e && make config
done
exit 0;;
esac
}
make_depends() {
root
if [ -z "$pkg" ]; then
echo "bpkg: you didn't specify any port to install/upgrade."
exit 0
fi
init_check
if [ -z "$a" ]; then
whereis -sq $pkg
echo ""
echo -e "\033[33m*** not installed ***\033[0m"
echo "Installing new port(s):"
else
pkg_info -xoQ $pkg | awk -F: '{OFS=": " ; print $2, $1}'
echo ""
echo -e "\033[33m*** installed ***\033[0m"
echo "Upgrading port(s):"
fi
echo -n "Do you want to set config options recursively? [y] "
read key
case $key in
n) for e in `whereis -sq $pkg`; do
cd $e && make config
cd $e && make depends && make install clean
done
exit 0;;
*) for e in `whereis -sq $pkg`; do
cd $e && make config-recursive
cd $e && make depends && make install clean
done
exit 0;;
esac
}
help() {
echo "bpkg version `pkg_info -xI bpkg | cut -d- -f2 | cut -d' ' -f1`"
echo ""
echo "Usage: bpkg [-dDeEfgiIkLmnNoOprsSw] pkg-name ..."
echo " bpkg [-aAcCFhlv]"
echo ""
echo "Please see the bpkg(8) man page for more information"
echo ""
exit 0
}
# End of functions
################################################################################
while getopts d:D:e:E:f:g:i:I:k:L:m:n:N:o:O:p:r:s:S:w:aAcCFhlv opts; do
case $opts in
d) pkg=$OPTARG
pkg_delete-xr;;
D) pkg=$OPTARG
pkg_info-x;;
e) pkg=$OPTARG
make_config;;
E) pkg=$OPTARG
make_depends;;
f) pkg=$OPTARG
pkg_info-xf;;
g) pkg=$OPTARG
pkg_info-xg;;
i) pkg=$OPTARG
make_search;;
I) pkg=$OPTARG
pkg_info-xI;;
k) pkg=$OPTARG
pkg_info-xk;;
L) pkg=$OPTARG
pkg_info-xL;;
m) pkg=$OPTARG
pkg_makefile;;
n) pkg=$OPTARG
simulation;;
N) pkg=$OPTARG
non-interactive;;
o) pkg=$OPTARG
make_showc;;
O) pkg=$OPTARG
make_rmc;;
p) pkg=$OPTARG
pkg_path;;
r) pkg=$OPTARG
pkg_info-xr;;
s) pkg=$OPTARG
pkg_info-xs;;
S) pkg=$OPTARG
make_search_w;;
w) pkg=$OPTARG
pkg_which-;;
a) all;;
A) all_non-interactive;;
c) pkg_clean;;
C) pkg_clean2;;
F) pkg_find;;
h) help;;
l) pkg_inf;;
v) pkg_ver;;
*) help;;
esac
done
main
|