-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetpkg
More file actions
executable file
·1113 lines (938 loc) · 35.3 KB
/
netpkg
File metadata and controls
executable file
·1113 lines (938 loc) · 35.3 KB
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright Jean-Philippe Guillemin <h1p8r10n@yandex.com>. This program is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your option)
# any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm
#
# Netpkg is a tool for easily install or upgrade packages via the network. With Netpkg,
# you can make a minimal installation of Zenwalk Linux and install/upgrade just the
# packages you need the most.
#
#
V="11.5"
zenwalk_version="$(</etc/zenwalk-version)"
# Globals #####################################
export LANG=en_US
export configfile="/etc/netpkg.conf"
export PATH="/usr/libexec:/sbin:/bin:/usr/sbin:/usr/bin"
. /etc/shell-colors
# Parsing config files ######################################
NetpkgDir=$(sed -n 's/^[[ \t]*Netpkg_dir[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export NetpkgDir="${NetpkgDir:=/var/netpkg}"
mkdir -p $NetpkgDir 2>/dev/null
RemotesDir=$(sed -n 's/^[[ \t]*Remotes_dir[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export RemotesDir="${RemotesDir:=/etc/netpkg.d}"
mkdir -p $RemotesDir 2>/dev/null
export sourceslist=$(ls /etc/netpkg.d/)
LocalPkgDir=$(sed -n 's/^[[ \t]*Local_repository[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export LocalPkgDir="${LocalPkgDir:=/var/packages}"
mkdir -p ${LocalPkgDir}
packagelogs=$(sed -n 's/^[[ \t]*Package_logs[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export packagelogs="${packagelogs:=/var/log/packages}"
export blacklist="${blacklist:=aaa_base}"
keepit=$(sed -n 's/^[[ \t]*Keep_packages[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export keepit="${keepit:=yes}"
untouchable=$(sed -n 's/^[[ \t]*Protected_files[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export untouchable="${untouchable:=/etc/lilo.conf /boot/grub/grub.cfg /etc/fstab}"
ProcessDependencies=$(sed -n 's/^[[ \t]*Handle_dependencies[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export ProcessDependencies="${ProcessDependencies:=no}"
export http_proxy=$(sed -n 's/^[[ \t]*Proxy_Socket[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export proxyusr=$(sed -n 's/^[[ \t]*Proxy_User[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export proxypwd=$(sed -n 's/^[[ \t]*Proxy_Password[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
export WGET="wget --no-check-certificate "
[[ "$proxyusr" ]] && export WGET="$WGET --proxy-user=${proxyusr} "
[[ "$proxypwd" ]] && export WGET="$WGET --proxy-password=${proxypwd} "
logfile=$(sed -n 's/^[[ \t]*Logfile[[ \t]*=[[ \t]*\(.*\)$/\1/p' $configfile)
if [[ "$logfile" == "none" ]]; then
export logfile="/dev/null"
else
export logfile="${logfile:=/var/log/netpkg.log}"
fi
# Get Host part of URL $1
getHostFromURL(){
## Remove protocol part of URL ##
HOST="${1#http://}"
HOST="${HOST#https://}"
HOST="${HOST#ftp://}"
HOST="${HOST#scp://}"
HOST="${HOST#scp://}"
HOST="${HOST#sftp://}"
## Remove username and/or username:password part of URL ##
HOST="${HOST#*:*@}"
HOST="${HOST#*@}"
## Remove rest of URL ##
HOST=${HOST%%/*}
## Show domain name only ##
echo -n "$HOST"
}
mill[0]="|" ; mill[16]="/" ; mill[32]="-" ; mill[48]="\\"
# Enable deps in netpkg.conf
function enableDependencies(){
sed -i "s|.*Handle_dependencies.*|Handle_dependencies = yes|" $configfile
echo -e "${BOLDWHITE}Dependency processing${COLOR_RESET} has been ${BOLDGREEN}enabled${COLOR_RESET}"
}
# Disable deps in netpkg.conf
function disableDependencies(){
sed -i "s|.*Handle_dependencies.*|Handle_dependencies = no|" $configfile
echo -e "${BOLDWHITE}Dependency processing${COLOR_RESET} has been ${BOLDRED}disabled${COLOR_RESET}"
}
# fetch the packages list for available packages
function getRemotePkgs(){
cut -d " " -f2 $NetpkgDir/RemotePkgs.db
}
function getLocalPkgs(){
cat $NetpkgDir/LocalPkgs.db | sort
}
# fetch the packages list for package named $1
function getPkgFromShortName(){
cut -d " " -f2 $NetpkgDir/RemotePkgs.db | sed -n "s/^\($1-[^\-]*-[^\-]*-[^\-]*\.t[glx]z\)$/\1/p"
}
# The uniq name for pkg
function getShortNameFromPkg(){
ShortName=${1%-*}; ShortName=${ShortName%-*}; ShortName=${ShortName%-*}
echo -n "$ShortName"
}
# Get the location for package $1
function getLocationForPkg(){
location="$(sed -n "s/^\([^ \t]*\)[[ \t]*$1[[ \t]*$/\1/p" $NetpkgDir/RemotePkgs.db)"
if [[ "$location" ]] ; then
echo -n "$location"
else
echo -n "local"
fi
}
function getSourceForPkg(){
source="$(sed -n "s/^\([^ \t]*\)[[ \t]*$1[[ \t]*$/\1/p" $NetpkgDir/RemotePkgs.db | cut -d"/" -f3)"
if [[ "$source" ]] ; then
echo -n "$source"
else
echo -n "local"
fi
}
# Build a list of installed packages
function buildLocalPkgsDb(){
rm -f $NetpkgDir/LocalPkgs.db
for meta in $(ls $packagelogs) ; do
# package="$(sed -n 's/^[[ \t]*PACKAGE LOCATION:[[ \t]*\(.*\)[[ \t]*$/\1/p' $packagelogs/$meta)"
# echo ${package##*/} >> $NetpkgDir/LocalPkgs.db
echo $meta.txz >> $NetpkgDir/LocalPkgs.db
done
}
function installKernel(){
if [[ -x /usr/sbin/install-kernel.sh ]]; then
echo "Checking for new kernel to install ... "
/usr/sbin/install-kernel.sh
fi
}
# Download and process the meta file
function fetchRemotes(){
if [[ "$source" != 'local' ]] ; then
echo 'Cleaning cache'
rm -f $NetpkgDir/RemotePkgs.db 2>/dev/null
rm -f $NetpkgDir/RemoteDeps.db 2>/dev/null
rm -f $NetpkgDir/PACKAGES.TXT* 2>/dev/null
rm -f $NetpkgDir/*.md5 2>/dev/null
for source in $(ls $RemotesDir/) ; do
[[ "$source" == 'local' ]] && continue
rm -f $NetpkgDir/$source.TXT*
mv $NetpkgDir/RemotePkgs.db.$source $NetpkgDir/OldRemotePkgs.db.$source 2>/dev/null
mv $NetpkgDir/RemoteDeps.db.$source $NetpkgDir/OldRemoteDeps.db.$source 2>/dev/null
RemoteURL="$(getRemoteURL $source)"
echo -e "Connecting to $source..."
neterror=$($WGET -O $NetpkgDir/$source.TXT.gz $RemoteURL/PACKAGES.TXT.gz 2>&1 | grep -E 'failed:|Not Found')
if [[ "$neterror" ]] ; then
neterror=$($WGET -O $NetpkgDir/$source.TXT $RemoteURL/PACKAGES.TXT 2>&1 | grep -E 'failed:|Not Found')
if [[ "$neterror" ]] ; then
echo "Unable to connect to $RemoteURL, please check the network or choose another source"
return 1
fi
else
echo 'Uncompressing meta information'
if ! gunzip -f $NetpkgDir/$source.TXT.gz 2>/dev/null ; then
echo 'Unable to extract meta information, please check the network or choose another source'
return 1
fi
fi
echo 'Downloading MD5 information'
neterror=$($WGET -O $NetpkgDir/$source.md5.gz $RemoteURL/CHECKSUMS.md5.gz 2>&1 | grep -E 'failed:|Not Found')
if [[ "$neterror" ]] ; then
neterror=$($WGET -O $NetpkgDir/$source.md5 $RemoteURL/CHECKSUMS.md5 2>&1 | grep -E 'failed:|Not Found')
if [[ "$neterror" ]] ; then
echo "Unable to connect to $RemoteURL, please check the network or choose another source"
return 1
fi
else
echo 'Uncompressing MD5 information'
if ! gunzip -f $NetpkgDir/$source.md5.gz 2>/dev/null ; then
echo 'Unable to extract MD5 information, please check the network or choose another source'
return 1
fi
fi
# cleaning PACKAGE SOURCE: lines
sed -i '/^PACKAGE MIRROR:.*/d' $NetpkgDir/$source.TXT
# Filter out blacklisted packages
echo 'Filtering out blacklisted packages'
blacklist="$(getRemoteBlacklist $source)"
for pkg in $blacklist ; do
grep -v -e "^PACKAGE NAME: *$pkg *" $NetpkgDir/$source.TXT > $NetpkgDir/.BUFFER1
cat $NetpkgDir/.BUFFER1 > $NetpkgDir/$source.TXT
done
rm -f $NetpkgDir/.BUFFER1
cat $NetpkgDir/$source.TXT >> $NetpkgDir/PACKAGES.TXT
if [[ "$ProcessDependencies" == 'yes' ]] ; then
echo 'Computing packages dependencies'
sed -n '
/^PACKAGE NAME:.*/{
N;N;N;N
s/,/ /g
s/^PACKAGE NAME:[[ \t]*\(.*\)-[^-]*-[^-]*-[^-]*\.t[glx]z[[ \t]*\n.*\nPACKAGE SIZE (compressed):[[ \t]*\(.*\)\nPACKAGE SIZE (uncompressed):[[ \t]*\(.*\)\nPACKAGE REQUIRED:[[ \t]*\(.*\)/\1:\4/p
}' \
$NetpkgDir/$source.TXT > $NetpkgDir/RemoteDeps.db.$source
cat $NetpkgDir/RemoteDeps.db.$source >> $NetpkgDir/RemoteDeps.db
fi
#echo 'Computing packages descriptions'
#sed -n '
#/^PACKAGE NAME:.*/{
#N;N;N;N;N;N;N;N
#s/^PACKAGE NAME:[[ \t]*\(.*\)-[^-]*-[^-]*-[^-]*\.t[glx]z[[ \t]*\n.*\nPACKAGE SIZE (compressed):[[ \t]*\(.*\)\nPACKAGE SIZE (uncompressed):[[ \t]*\(.*\)\nPACKAGE REQUIRED:[[ \t]*\(.*\)\n.*\n.*\n.*\n[^ \t]*:[[ \t]*\(.*\)/\1: Description : \5\n\1: Compressed : \2\n\1: Uncompressed : \3\n\1: ProcessDependencies : \4/p
#}' $NetpkgDir/PACKAGES.TXT > $NetpkgDir/descfile
echo 'Creating remote packages list'
sed -n "/^PACKAGE NAME:.*/{ N; s|^PACKAGE NAME:[[ \t]*\(.*\.t[glx]z\)[[ \t]*\nPACKAGE LOCATION:[[ \t]*\.\/\?\(.*\)|$RemoteURL\/\2 \1|p }" \
$NetpkgDir/$source.TXT > $NetpkgDir/RemotePkgs.db.$source
cat $NetpkgDir/RemotePkgs.db.$source >> $NetpkgDir/RemotePkgs.db
done
else
echo '' > $NetpkgDir/RemotePkgs.db
fi
echo 'Creating local packages list'
buildLocalPkgsDb
# Generating a report
echo -e 'Synchronization successful'
if [[ "$ProcessDependencies" == 'yes' ]] ; then
echo -e 'Dependency processing enabled'
else
echo -e 'Dependency processing disabled'
fi
echo ''
}
# get URL for source $1
function getRemoteURL(){
sed -n 's/^[[ \t]*URL[[ \t]*=[[ \t]*\(.*\)$/\1/p' $RemotesDir/$1
}
# get Blacklist for source $1
function getRemoteBlacklist(){
sed -n 's/^[[ \t]*Black_list[[ \t]*=[[ \t]*\(.*\)$/\1/p' $RemotesDir/$1
}
# take a look at local packages list to see which version of the $1 package installed
function getLocalPkgByShortName(){
sed -n "s/^\($1-[^\-]*-[^\-]*-[^\-]*\.t[glx]z\)$/\1/p" $NetpkgDir/LocalPkgs.db
}
# Download ($package)
function downloadPkg(){
for package in $* ; do
location=$(getLocationForPkg $package)
if [[ -e $LocalPkgDir/$package && \
"$(md5sum $LocalPkgDir/$package | cut -d" " -f1)" == "$(cut -d" " -f1 $LocalPkgDir/${package%.*}.md5)" ]]; then
echo -e "Using $package from local cache"
return
else
rm -f $LocalPkgDir/$package 2>/dev/null
rm -f $LocalPkgDir/${package%.*}.md5 2>/dev/null
$WGET -q --show-progress -c -O $LocalPkgDir/$package "$location/$package" 2>&1
if [[ ! -e $LocalPkgDir/$package ]] ; then
echo "Unable to download $location/$package"
return 1
fi
grep "/$package$" "$NetpkgDir/$(getSourceForPkg $package).md5" > $LocalPkgDir/${package%.*}.md5
fi
done
}
# Checks for configuration files to update.
function checkDotNew(){
local actionlist='yes skip diff remove exit'
echo 'Checking for configuration files to update...'
dotnewlist="$(find /etc -name "*.new")"
if [[ "$dotnewlist" ]] ; then
for file in $dotnewlist ; do
origfile="$(echo $file|sed -e 's/.new//')"
[[ "$(echo $untouchable | grep $origfile)" ]] && continue
echo "Should we move $file to $origfile ?"
select action in $actionlist ; do
case $action in
'yes')
cp -f $file $origfile
rm -f $file
break
;;
'skip')
break
;;
'diff')
if [[ -e $origfile ]] ; then
diff -dU 1 $origfile $file | most
else
echo "$file is the only one, no $origfile yet"
fi
;;
'remove')
rm -f $file
break
;;
'exit')
exit
esac
done
done
else
echo 'No \".new\" configuration files on the system'
fi
}
# Disable source
function disableRemote(){
if [[ "$sourceslist" ]] ; then
echo 'Which remote do you want to disable? :'
select source in $sourceslist exit ;do
[[ "$source" == 'exit' ]] && exit 0
mv -f $RemotesDir/$source $RemotesDir/.$source
break
done
echo -e "${BOLDGREEN}$source${COLOR_RESET} has been disabled"
fi
}
# Enable remote
function enableRemote(){
local disabledsourceslist=$(ls -d $RemotesDir/.??* | sed -e "s|$RemotesDir/\(.*\)|\1|" )
if [[ "$disabledsourceslist" ]] ; then
echo 'Which remote do you want to enable? :'
select source in $disabledsourceslist exit ;do
[[ "$source" == 'exit' ]] && exit 0
mv -f $RemotesDir/$source $RemotesDir/${source#.}
break
done
echo -e "${BOLDGREEN}${source#.}${COLOR_RESET} has been enabled"
fi
}
# Add remote for URL "$1"
function addRemote(){
local source="$(getHostFromURL $1)"
if [[ "$1" ]] ; then
if [[ -e $RemotesDir/$source ]]; then
echo -e "${BOLDWHITE}$source already exists in $RemotesDir ${COLOR_RESET}"
elif [[ ! "$(echo $1 | egrep 'http://|ftp://|https://')" ]] ; then
echo -e "${BOLDWHITE}Bad syntax in URL $1 ${COLOR_RESET}"
else
source="$(getHostFromURL $1)"
echo "URL = $1" > $RemotesDir/$source
echo "Black_list = " >> $RemotesDir/$source
# sourcehash="$(echo "$2" | md5sum | cut -d " " -f 1)"
echo -e "${BOLDGREEN}$source${COLOR_RESET} has been configured"
fi
else
echo 'you forgot the URL... ;)'
exit
fi
}
function printPkgList(){
local status=$1
local color=$2
# Get package details
local ShortName=${package%-*}; ShortName=${ShortName%-*}; ShortName=${ShortName%-*}
local ShortNameDotDot="" ; [[ ${#ShortName} -gt 20 ]] && ShortNameDotDot=".."
local LocalPkg="$(getLocalPkgByShortName $ShortName)"
local LocalPkgDotDot="" ; [[ ${#LocalPkg} -gt 30 ]] && LocalPkgDotDot=".."
local LocalVersion=${LocalPkg%-*}; LocalVersion=${LocalVersion%-*}; LocalVersion=${LocalVersion##*-}
local LocalVersionDotDot="" ; [[ ${#LocalVersion} -gt 10 ]] && LocalVersionDotDot=".."
local BuildVersion=${package%.*}; BuildVersion=${BuildVersion##*-}
local BuildVersionDotDot="" ; [[ ${#BuildVersion} -gt 10 ]] && BuildVersionDotDot=".."
local Version=${package%-*}; Version=${Version%-*}; Version=${Version##*-}
local VersionDotDot="" ; [[ ${#Version} -gt 10 ]] && VersionDotDot=".."
local Desc="$(sed -n "s/$ShortName:.*(\(.*\))/\1/p" $NetpkgDir/PACKAGES.TXT | head -n1)"
[[ ${#Desc} -lt 1 ]] && Desc="$ShortName package for Slackware & Zenwalk"
local location=$(getLocationForPkg $package)
local RemoteURI="$(echo -n $location | cut -d'/' -f3)"
echo -ne "${color}${status}${COLOR_RESET} "
printf "%-24s %-16s %-16s %-22s %-32s\n" \
"${ShortName:0:20}$ShortNameDotDot" \
"${Version:0:14}$VersionDotDot" \
"${BuildVersion:0:14}$BuildVersionDotDot" \
"${RemoteURI:0:18}.." \
"${Desc:0:30}.."
}
# list packages from Remote #########################
function listPkg() {
truncate -s0 $NetpkgDir/.BUFFER1
for pattern in $* ; do
if [[ -n "$(echo "$PkgStatus" | grep 'U')" || -n "$(echo "$PkgStatus" | grep 'D')" || -n "$(echo "$PkgStatus" | grep 'R')" ]]; then
getRemotePkgs | grep "$pattern" >> $NetpkgDir/.BUFFER1
fi
if [[ -n "$(echo "$PkgStatus" | grep 'I')" ]]; then
getLocalPkgs | grep "$pattern" >> $NetpkgDir/.BUFFER1
fi
done
local ShortName=${package%-*}; ShortName=${ShortName%-*}; ShortName=${ShortName%-*}
local LocalPkg="$(getLocalPkgByShortName $ShortName)"
tput bold ; printf "%-26s %-16s %-16s %-22s %-5s\n" " Name" "Version" "Build" "Remote" "Info" ; tput sgr0
for package in $(sort $NetpkgDir/.BUFFER1 2>/dev/null | uniq) ; do
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
# it's installed
if [[ -n "$LocalPkg" ]]; then
if [[ "$LocalPkg" == "$package" ]]; then
if [[ -n "$(echo "$PkgStatus" | grep 'I')" ]]; then
printPkgList "I" ${BOLDBLUE}
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -u -q $package)" ]]; then
if [[ -n "$(echo "$PkgStatus" | grep 'U')" ]]; then
printPkgList "U" ${BOLDRED}
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -d -q $package)" ]]; then
echo $package | grep "netpkg" && echo $LocalPkg
if [[ -n "$(echo "$PkgStatus" | grep 'D')" ]]; then
printPkgList "D" ${BOLDYELLOW}
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
fi
# not installed
else
if [[ -n "$(echo "$PkgStatus" | grep 'R')" ]]; then
printPkgList "R" ${BOLDCYAN}
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
fi
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
done
echo 'Search done.'
}
function printSearchResult(){
local status=$1
local color=$2
# Get package details
local ShortName=${package%-*}; ShortName=${ShortName%-*}; ShortName=${ShortName%-*}
local ShortNameDotDot="" ; [[ ${#ShortName} -gt 20 ]] && ShortNameDotDot=".."
local LocalPkg="$(getLocalPkgByShortName $ShortName)"
local LocalPkgDotDot="" ; [[ ${#LocalPkg} -gt 30 ]] && LocalPkgDotDot=".."
local Desc="$(sed -n "s/$ShortName:.*(\(.*\))/\1/p" $NetpkgDir/PACKAGES.TXT | head -n1)"
[[ ${#Desc} -lt 1 ]] && Desc="$ShortName package for Slackware & Zenwalk"
local location=$(getLocationForPkg $package)
local RemoteURI="$(echo -n $location | cut -d'/' -f3)"
if [[ "$RemoteURI" == "local" ]]; then
RemotePkg="-----"
else
RemotePkg="$package"
RemotePkgDotDot="" ; [[ ${#package} -gt 30 ]] && RemotePkgDotDot=".."
fi
tput bold ; printf "%-36s %-34s %-7s\n" " Installed Pkg" "Remote Pkg" "Remote"; tput sgr0
echo -ne "${color}${status}${COLOR_RESET} "
printf "%-34s %-34s %-22s\n" \
"${LocalPkg:0:30}$LocalPkgDotDot" "${RemotePkg:0:30}$RemotePkgDotDot" "${RemoteURI:0:18}.."
echo "(${Desc:0:50}..)"
}
# prompt for action (install / upgrade / reinstall / download) for a list of packages ($*) #########################
function searchAndPrompt(){
truncate -s0 $NetpkgDir/.BUFFER1
for pattern in $* ; do
if [[ -n "$(echo "$PkgStatus" | grep 'U')" || -n "$(echo "$PkgStatus" | grep 'D')" || -n "$(echo "$PkgStatus" | grep 'R')" ]]; then
getRemotePkgs | grep "$pattern" >> $NetpkgDir/.BUFFER1
fi
if [[ -n "$(echo "$PkgStatus" | grep 'I')" ]] || [[ -n "$(echo "$PkgStatus" | grep 'X')" ]]; then
getLocalPkgs | grep "$pattern" >> $NetpkgDir/.BUFFER1
fi
done
for package in $(sort $NetpkgDir/.BUFFER1 2>/dev/null | uniq) ; do
local ShortName=${package%-*}; ShortName=${ShortName%-*}; ShortName=${ShortName%-*}
local LocalPkg="$(getLocalPkgByShortName $ShortName)"
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
actionlist=''
# it's installed
if [[ -n "$LocalPkg" ]]; then
if [[ "$LocalPkg" == "$package" ]] ; then
[[ -n "$(echo "$PkgStatus" | grep 'U')" ]] && continue
if [[ -n "$(echo "$PkgStatus" | grep 'I')" ]]; then
printSearchResult "I" ${BOLDBLUE}
local actionlist='reinstall skip exit download remove'
elif [[ -n "$(echo "$PkgStatus" | grep 'X')" ]]; then
printSearchResult "I" ${BOLDBLUE}
local actionlist='remove skip exit'
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -u -q $package)" ]]; then
if [[ -n "$(echo "$PkgStatus" | grep 'U')" ]]; then
printSearchResult "U" ${BOLDRED}
local actionlist='install skip exit download remove'
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -d -q $package)" ]]; then
if [[ -n "$(echo "$PkgStatus" | grep 'D')" ]]; then
printSearchResult "D" ${BOLDYELLOW}
local actionlist='install skip exit download remove'
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
fi
# not installed
else
LocalPkg="-----"
if [[ -n "$(echo "$PkgStatus" | grep 'R')" ]]; then
printSearchResult "R" ${BOLDCYAN}
local actionlist='install skip exit download'
else
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
fi
fi
if [[ "$actionlist " != " " ]] ; then
echo ' what should I do ?'
select action in $actionlist ; do
case $action in
'install')
processPkgs $package
break
;;
'reinstall')
processPkgs $package
break
;;
'remove')
removepkg $LocalPkg
break
;;
'skip')
echo "Skipping [$(echo -n $location | cut -d'/' -f3)] $package"
echo
break
;;
'download')
downloadPkg $package
break
;;
'exit')
exit
esac
sed -i "/^$ShortName-[^\-]*-[^\-]*-[^\-]*\.t[glx]z$/d" $NetpkgDir/.BUFFER1
done
fi
done
echo "Search done."
}
# Check deps , then Install / Reinstall / Upgrade a list of packages
# The full and exact package name MUST be provided
function processPkgs() {
if [[ "$*" ]]; then
finallist=''
PkgNumber=0
# We need an up to date list of installed packages
buildLocalPkgsDb
for package in $* ; do
# the software name
local ShortName="$(getShortNameFromPkg $package)"
finallist="${finallist} $package"
let "PkgNumber++"
if [[ "$ProcessDependencies" == 'yes' ]] ; then
deps="$(grep "^$ShortName:.*$" $NetpkgDir/RemoteDeps.db | cut -s -d ":" -f 2-)"
[[ ! "$deps" ]] && deps="$(grep "^$ShortName:.*$" $NetpkgDir/DepsDB | cut -s -d ":" -f 2-)"
[[ ! "$deps" ]] && continue
for dep in $deps ; do
# We need to find package for $dep ($dep is the short name)
deppackage="$(sed -n "s/^.*[[ \t]\($dep-[^\-]*-[^\-]*-[^\-]*.t[glx]z\)[[ \t]*$/\1/p" $NetpkgDir/RemotePkgs.db | head -n1)"
[[ ! "$deppackage" ]] && continue
# If it's already in the list, then skip
[[ "$(echo $finallist | grep "$deppackage" )" ]] && continue
# Do we have it installed in ANY version ? then skip
[[ "$(getLocalPkgByShortName $dep)" ]] && continue
finallist="${finallist} $deppackage"
let "PkgNumber++"
prompt='yes'
done
fi
done
if [[ "$prompt" == 'yes' ]] ; then
prompt=''
echo "We are about to install $PkgNumber package(s) : continue ?"
echo -e "-> ${BOLDRED}${finallist}${COLOR_RESET}"
select action in 'run Forest!' 'abort' ; do
case $action in
'run Forest!')
break
;;
'abort')
finallist=''
break
esac
done
fi
PkgCount=0
for package in $finallist ; do
let "PkgCount++"
echo -e "${BOLDWHITE}Downloading $package ($PkgCount/$PkgNumber) ...${COLOR_RESET}"
# download the package if needed
downloadPkg $package
done
PkgCount=0
for package in $finallist ; do
let "PkgCount++"
# the software name
ShortName="$(getShortNameFromPkg $package)"
# do we have this package installed in ANY version ?
LocalPkg="$(getLocalPkgByShortName $ShortName)"
# We need to find the location for $package
location="$(getLocationForPkg $package)"
# We need to find the source for $package, as information
source="$(getSourceForPkg $package)"
[[ ! -e $LocalPkgDir/$package ]] && downloadPkg $package
if [[ "$(md5sum $LocalPkgDir/$package | cut -d" " -f1)" == "$(cut -d" " -f1 $LocalPkgDir/${package%.*}.md5)" ]]; then
echo -e "Checksum is OK for $package :)"
else
echo -e "${BOLDRED}Package corrupted or unable to get checksum for $package : skipping !${COLOR_RESET}"
mv "$LocalPkgDir/$package" "$LocalPkgDir/_BAD_$package"
mv "$LocalPkgDir/${package%.*}.md5" "$LocalPkgDir/_BAD_${package%.*}.md5"
continue
fi
if [[ -e $packagelogs/${package%.*} ]]; then
echo -ne "${BOLDWHITE}($PkgCount/$PkgNumber) Reinstalling"
echo -e "${BOLDBLUE} [$source] $package${COLOR_RESET}"
[[ -e $LocalPkgDir/$package ]] && upgradepkg --reinstall $LocalPkgDir/$package | grep -v -E "^$|#|DESCRIPTION:|\+=|\|" && echo "[U] $location/$package $(date)" >> $logfile
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -u -q $package)" ]]; then
echo -ne "${BOLDWHITE}($PkgCount/$PkgNumber) Upgrading"
echo -e "${BOLDRED} [$source] $package${COLOR_RESET}"
[[ -e $LocalPkgDir/$package ]] && upgradepkg $LocalPkgDir/$package | grep -v -E "^$|#|DESCRIPTION:|\+=|\|" && echo "[U] $location/$package $(date)" >> $logfile
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -d -q $package)" ]]; then
echo -ne "${BOLDWHITE}($PkgCount/$PkgNumber) Downgrading"
echo -e "${BOLDYELLOW} [$source] $package${COLOR_RESET}"
[[ -e $LocalPkgDir/$package ]] && upgradepkg $LocalPkgDir/$package | grep -v -E "^$|#|DESCRIPTION:|\+=|\|" && echo "[U] $location/$package $(date)" >> $logfile
else
echo -ne "${BOLDWHITE}($PkgCount/$PkgNumber) Installing"
echo -e "${BOLDCYAN} [$source] $package${COLOR_RESET}"
[[ -e $LocalPkgDir/$package ]] && installpkg $LocalPkgDir/$package | grep -v -E "^$|#|DESCRIPTION:|\+=|\|" && echo "[I] $location/$package $(date)" >> $logfile
fi
# remove package if we don't want to keep it
if [[ ! "$keepit" = 'yes' ]]; then
rm -f $LocalPkgDir/$package 2>/dev/null
fi
# We need an up to date list of installed packages
buildLocalPkgsDb
done
fi
}
# downloadAll ###############################################
function downloadAll() {
if [[ "$prompt" == 'yes' ]] ; then
prompt=''
echo 'We are about to download all packages from remotes : continue ?'
select action in 'run Forest!' 'abort' ; do
case $action in
'run Forest!')
break
;;
'abort')
return
esac
done
fi
for package in $(getRemotePkgs) ; do
if [[ "$package" ]]; then
downloadPkg $package
fi
done
}
# upgrade the whole system ###############################################
function updateAll() {
truncate -s0 $NetpkgDir/.BUFFER1
finallist=""
getRemotePkgs > $NetpkgDir/.BUFFER1
for package in $(<$NetpkgDir/.BUFFER1) ; do
local ShortName=${package%-*}; ShortName=${ShortName%-*}; ShortName=${ShortName%-*}
local LocalPkg="$(getLocalPkgByShortName $ShortName)"
if [[ "$LocalPkg" == "$package" ]]; then
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
continue
elif [[ -n "$LocalPkg" ]]; then
if [[ "$(echo "x | $package | $LocalPkg" | vfilter -u -q $package)" && -n "$(echo "$PkgStatus" | grep 'U')" ]]; then
echo "Found updated $package"
finallist="${finallist} $package"
elif [[ "$(echo "x | $package | $LocalPkg" | vfilter -d -q $package)" && -n "$(echo "$PkgStatus" | grep 'D')" ]]; then
echo "Found downgraded $package"
finallist="${finallist} $package"
fi
elif [[ -n "$(echo "$PkgStatus" | grep 'R')" ]]; then
local location=$(getLocationForPkg $package)
local category=${location##*/}
if [[ "$category" == "l" || "$category" == "a" || "$category" == "z" ]]; then
echo "Found missing $package"
finallist="${finallist} $package"
fi
fi
[[ "${mill[$pos]}" != '' ]] && printf "\e[31m%s\e[0m\b" "${mill[$pos]}" ; ((rotation++)) ; pos=$(( $rotation % 64 ))
done
processPkgs "$finallist"
echo 'Search done.'
}
# File Protector function ######################################
function Protect() {
# we backup all critical files as .old
for protectedFile in $untouchable ; do
[[ -e $protectedFile ]] && cp -f $protectedFile $protectedFile.old 2>/dev/null
done
# lock running // netpkg
if [[ -e "$NetpkgDir/.lock" ]]; then
echo "Another netpkg process is running, exiting ($(<$NetpkgDir/.lock)) !"
exit 1
fi
echo "$$" > "$NetpkgDir/.lock"
}
# File unProtector function
function unProtect() {
rm -f "$NetpkgDir/.lock"
# delete backuped files in case it's the same OR no newer was added
for protectedFile in $untouchable ; do
if [[ -e $protectedFile ]]; then
newFile="$protectedFile"
oldFile="$newFile.old"
# clean up the redundant copy
if [[ "$(md5sum "$oldFile" 2>/dev/null | cut -f1 -d" ")" == "$(md5sum "$newFile" 2>/dev/null | cut -f1 -d" ")" ]]; then
rm -f "$oldFile" 2>/dev/null
fi
fi
# Otherwise, we leave the .old copy for root to consider...
done
}
# Final cleanup ######################################
function cleanup(){
# We delete unchanged Protected files or keep the old backuped one
unProtect
echo -e "Bye ${BOLDRED};)${COLOR_RESET}"
echo
exit 0
}
# remove the NetpkgDired files on any type of exit
trap 'cleanup' TERM INT EXIT
# main( :) #########################
# We always need an up to date list of installed packages
buildLocalPkgsDb
# Some fun :)
function baneer() {
echo -en "${BOLDRED}"
echo " _ __ __ __ ";
echo " / |/ /___ / /_ ___ / /__ ___ ";
echo " / // -_)/ __// _ \ / '_// _ \ ";
echo " /_/|_/ \__/ \__// .__//_/\_\ \_, / ";
echo " /_/ /___/ ";
echo -e "${COLOR_RESET}"
}
# CLI howto ###############################################
function usage() {
echo -e "Basic usage : "
echo -e " ${BOLDWHITE}netpkg update${COLOR_RESET}"
echo -e " ${BOLDWHITE}netpkg upgrade${COLOR_RESET}"
echo -e " ${BOLDWHITE}netpkg install pattern1 [pattern2 ...]${COLOR_RESET}"
echo -e " ${BOLDWHITE}netpkg search [pattern1 pattern2 ...]${COLOR_RESET}"
echo -e " ${BOLDWHITE}netpkg remove pattern1 [pattern2 ...]${COLOR_RESET}"
echo -e " ${BOLDWHITE}netpkg help|-h${COLOR_RESET}"
}
function uusage() {
echo
echo -e "Output symbols and colors : "
echo -en "${BOLDBLUE} [I]${COLOR_RESET}"
echo -e " means \"Installed\" : remote version is the same as installed package"
echo -en "${BOLDRED} [U]${COLOR_RESET}"
echo -e " means \"Updated\" : remote version is higher than installed package"
echo -en "${BOLDYELLOW} [D]${COLOR_RESET}"
echo -e " means \"Downgraded\" : remote version is lower than installed package"
echo -en "${BOLDCYAN} [R]${COLOR_RESET}"
echo -e " means \"Remote\" : remote package is not installed yet"
echo
echo -e "System upgrade options : "
echo -e " ${BOLDWHITE}netpkg update|-u ${COLOR_RESET} : reload remotes packages lists"
echo -e " ${BOLDWHITE}netpkg upgrade|-U ${COLOR_RESET} : Automatic install of all \"updatable\" packages"
echo
echo -e "Setup commands :"
echo -e " ${BOLDWHITE}netpkg remotes|-rl ${COLOR_RESET} : list all remotes URL in netpkg.d"
echo -e " ${BOLDWHITE}netpkg remote-add|-ra \"URL\"${COLOR_RESET} : add Remote URL to netpkg.d"
echo -e " ${BOLDWHITE}netpkg remote-disable|-rd ${COLOR_RESET} : disable Remote URL in netpkg.d"
echo -e " ${BOLDWHITE}netpkg remote-enable|-re ${COLOR_RESET} : enable Remote URL in netpkg.d"
echo -e " ${BOLDWHITE}netpkg remote-disable|-rd ${COLOR_RESET} : disable dependency processing in netpkg.conf"
echo -e " ${BOLDWHITE}netpkg enable-deps|-ed ${COLOR_RESET} : enable dependency processing in netpkg.conf"
echo -e " ${BOLDWHITE}netpkg mrclean|-mc ${COLOR_RESET} : clean the local packages cache"
echo -e " ${BOLDWHITE}netpkg dotnew|-z ${COLOR_RESET} : search for .new config files in the suystem and prompt for action"
echo -en ${COLOR_RESET}
echo -e "Copyright Jean-Philippe Guillemin <h1p8r10n@yandex.com> GNU GPLv2"
echo
}
# Local options
if [[ "$1" ]]; then
case "$1" in
'--help' | '-help' | 'help' | '-h')
usage
uusage
exit 1
;;
'mrclean' | '-mc')
rm -f $LocalPkgDir/*z
fetchRemotes
exit 0
;;
'remotes' | 'remotes-list' | '-rl' | 'list-sources' | '-ls')
echo "Available remotes are :"
ls -A $RemotesDir/
echo "(dotted remotes are disabled)"
exit 0
;;
'dotnew' | '-z')
checkDotNew
exit 1
;;
'remote-add' | '-ra' | 'add-source' | '-as')
shift
addRemote $1
exit 0
;;
'remote-disable' | '-rd' | 'disable-source' | '-ds')
disableRemote
fetchRemotes
exit 0
;;
'remote-enable' | '-re' | 'enable-source' | '-es')
enableRemote
fetchRemotes
exit 0
;;
'deps-disable' | '-dd' | 'disable-deps')
disableDependencies
exit 0
;;
'deps-enable' | '-de'| 'enable-deps' | '-ed')
enableDependencies
exit 0
;;
esac
else
baneer
usage
exit 1
fi
checkDbAge() {
if [[ -e $NetpkgDir/RemotePkgs.db ]] ; then
# How old is the DB ?
DbAge=$(stat -c %Z $NetpkgDir/RemotePkgs.db )
if [[ $(($(date +%s) - $DbAge)) -ge 3600 ]] ; then
echo -en 'Current DB is older than 1 hour ...'
echo 'Keep it ? ("1" : reload, default : keep)'
echo -n "> "
while read answer ; do
case $answer in
1)