blob: fdd3296130717d4e8cd5206b27f38705e2f1332b (
plain)
1 #!/bin/bash
2 #
3 # pkgsize - shows disk usage for CRUX packages/ports
4 # Check man 1 pkgsize for more details
5 #
6 # (c) Damir Saric <damir.saric@du.hinet.hr>
7 #
8 # May be redistributed and modified under the terms of the GPL
9 # only usable with CRUX Linux
10 #
11 # USE AT YOUR OWN RISK
12 #
13
14 function interrupted() {
15 echo ""
16 echo "Interrupted."
17 clean
18 exit 1
19 }
20
21 function clean() {
22 if [ -f $TMP_LOCAL ]; then
23 rm $TMP_LOCAL
24 fi
25 if [ -f $TMP_WGET ]; then
26 rm $TMP_WGET
27 fi
28 }
29
30 function error () {
31 echo "ERROR! --> $1"
32 }
33
34 function usage() {
35 echo usage: pkgsize [options] [portname]
36 echo options:
37 echo " -v, --verbose show more information (each file's size)"
38 echo " -w, --wget show size to download (default if package not installed)"
39 echo " -h, --help print help and exit"
40 exit
41 }
42
43 function sizetodownload() {
44 dirs="$(cat /etc/ports/*.rsync | sed -n '/destination=/s/.*=//p')"
45 dirs="$dirs $(cat /etc/ports/*.httpup | sed -n '/ROOT_DIR=/s/.*=//p')"
46 dir="$(find $dirs -iname $portname 2>/dev/null | head -1)"
47
48 if [ -z $dir ]; then
49 error "$portname not a port!"
50 exit 2
51 fi
52
53 . $dir/Pkgfile
54
55 nrbytes=0
56 for FILE in ${source[@]}; do
57 testhttp=${FILE:0:4}
58
59 sizeofdl=""
60 if [ "$testhttp" = "http" ]; then
61 wget $FILE -S --spider 2> $TMP_WGET
62 if [ $? -ne 0 ] && [ $nrbytes -eq 0 ]; then
63 error "Not online, or no wget installed. "
64 clean
65 exit 1
66 fi
67 sizeofdl=`cat $TMP_WGET | awk '/Content-Length/{print $2}'`
68 rm $TMP_WGET
69 fi
70
71 if [ -z "$sizeofdl" ]; then
72 sizeofdl=0
73 fi
74
75 ((nrbytes=nrbytes+sizeofdl))
76
77 if [ "$verbose" = "true" ]; then
78 printf "%12s %s\n" $sizeofdl $FILE
79 fi
80
81 done
82 echo "Total number of bytes to download for $portname is : $nrbytes"
83 }
84
85 function main() {
86
87 pkginfo -l $portname > $TMP_LOCAL 2>/dev/null
88
89 if [ $? -ne 0 ]; then
90 sizetodownload
91 fi
92
93 bytea=0
94 byteall=0
95
96 while read line
97 do
98 file="/${line/ /\\ }"
99
100 if [ -f "$file" ]; then
101 filesize=$(/usr/bin/du -k $file)
102 size=$(echo $filesize | awk '{print $1}')
103
104 if [ "$verbose" = "true" ]; then
105 printf "%10s %s\n" $size $file
106 fi
107
108 ((bytea=bytea+size))
109 fi
110 done <$TMP_LOCAL
111
112 echo "Total kilobytes for $portname :" $bytea
113 }
114
115
116 trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
117 TMP_LOCAL="/tmp/list.pkgsize"
118 TMP_WGET="/tmp/wget.pkgsize"
119
120 verbose="false"
121
122 while [ $1 ]
123 do
124 case $1 in
125 -v|-V)
126 verbose="true"
127 shift
128 ;;
129 -h|-H|--help)
130 usage
131 shift
132 ;;
133 -w|-W|--wget)
134 online="true"
135 shift
136 ;;
137 *)
138 portnametemp=$1
139 shift
140 ;;
141 esac
142 done
143
144 portname=$portnametemp
145
146 if [ -z $portname ]; then
147 while read -p "Enter port name: " x
148 do
149 portname=$x
150 if [ "$online" = "true" ]; then
151 sizetodownload
152 else
153 main
154 fi
155 done
156 else if [ "$online" = "true" ]; then
157 sizetodownload
158 else
159 main
160 fi
161 fi
|