blob: 540fbc9321c4ff0fabca40ea810a7c1b25739a36 (
plain)
1 #!/bin/bash
2 #
3 # prtwash - a simple bash script for cleaning the port tree
4 # of the CRUX Linux distribution.
5 #
6 # Copyright (c) 2003 by Simone Rota <sip@varlock.com>
7 #
8 # *************************************************************************
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 as published by *
12 # * the Free Software Foundation; either version 2 of the License, or *
13 # * (at your option) any later version. *
14 # * *
15 # *************************************************************************
16 #
17 # Took some inspiration and code from Martin Opel's prtsweep v1.6 script
18 #
19 # **** USE AT YOUR OWN RISK ****
20 #
21 # TODO
22 # - a summary of files / bytes removed after the
23 # execution would be useful.
24 #
25
26 VERSION="0.7"
27 CONFIGFILE="/etc/prt-get.conf"
28
29 usage() {
30 echo "Usage: prtwash [-p] [-s] [-d] [-t] [-a] <path> [<path> ...]"
31 exit 1
32 }
33
34 showversion() {
35 echo "prtwash" $VERSION
36 echo "(c) 2003 by Simone Rota"
37 echo "This program is distributed under the GNU GPL license"
38 }
39
40 interrupted() {
41 echo "=======> operation interrupted."
42 exit 1
43 }
44
45 checkparams() {
46 # Do some test on given parameters.
47 if [ "$auto" = "0" ]; then
48
49 if [ -z "$prtdirs" ]; then
50 usage
51 exit -1
52 fi
53
54 for (( p=0 ; p<=${#prtdirs[@]} ; p++ )); do
55
56 if [ -z "${prtdirs[$p]}" ]; then
57 unset prtdirs[$p]
58 continue
59 fi
60
61 if [ ! -d "${prtdirs[$p]}" ]; then
62 echo "WARN: directory '${prtdirs[$p]}' not found. Skipping."
63 unset prtdirs[$p]
64 continue
65 fi
66
67 if [ ! -f "${prtdirs[$p]}"/Pkgfile ]; then
68 echo "WARN: no Pkgfile found in '${prtdirs[$p]}'. Skipping."
69 unset prtdirs[$p]
70 fi
71 done
72 fi
73 }
74
75 getoptions () {
76 # Name says it all.
77 while getopts psdathv opt; do
78 case "$opt" in
79 p) removepackage=1;;
80 s) removesources=1;;
81 d) removeaddonfiles=1;;
82 t) test=1;;
83 a) auto=1;;
84 h) usage
85 exit 0
86 ;;
87 v) showversion
88 exit 0
89 ;;
90 \?) usage
91 exit -1
92 ;;
93 esac
94 done
95 shift $(($OPTIND - 1))
96 prtdirs=("$@")
97 }
98
99 getdirs() {
100 # scans /etc/prt-get.conf for port dirs to process
101 current=""
102 for s in `sed 's/^[ \t]*//;s/[ \t]*$//' \
103 $CONFIGFILE|grep '^prtdir.*:'|sed 's/:/ /;s/,/ /;s/prtdir//;s/#.*//'`
104 do
105 if [ "`echo $s|grep '/'`" != "" ]; then
106 current=$s
107 else
108 if [ "$current" != "" ]; then
109 singledirs=(${singledirs[*]} $current/$s)
110 fi
111 fi
112 done
113
114 for s in `sed 's/^[ \t]*//;s/[ \t]*$//;/:/d' \
115 $CONFIGFILE|grep '^prtdir.*'|sed 's/prtdir//;s/#.*//'`
116 do
117 basedirs=(${basedirs[*]} $s)
118 done
119
120 }
121
122
123 remove() {
124 # Removes a file/directory
125 if [ "$test" = 1 ]; then
126 echo "+ (t) removing" $1
127 else
128 echo "+ removing" $1
129 rm -r $1
130 fi
131 }
132
133 wash() {
134 # Does the actual removal work.
135 if [ ! -f "$1"/Pkgfile ]; then
136 # tested before, the test here is for
137 # auto (-a) command
138 echo "WARN: no Pkgfile found in $1. Skipping."
139 else
140 tosave=() # array of files not to be deleted
141 . "$1"/Pkgfile
142 PKGMK_COMPRESSION_MODE="gz"
143 [ -f /etc/pkgmk.conf ] && . /etc/pkgmk.conf
144 packagename="$name"#"$version"-"$release"".pkg.tar.$PKGMK_COMPRESSION_MODE"
145
146 if [ ! "$removesources" = 1 ]; then
147 # keep sources
148 for src in "${source[@]}"; do
149 tosave=( "${tosave[@]}" "`basename "$src"`" )
150 done
151 fi
152
153 if [ ! "$removepackage" = 1 ]; then
154 # keep package
155 tosave=( "${tosave[@]}" "$packagename" )
156 fi
157
158 if [ ! "$removeaddonfiles" = 1 ]; then
159 # keep additional and dot files
160 tosave=( "${tosave[@]}" ".md5sum" )
161 tosave=( "${tosave[@]}" ".32bit" )
162 tosave=( "${tosave[@]}" ".footprint" )
163 tosave=( "${tosave[@]}" ".nostrip" )
164 tosave=( "${tosave[@]}" "README" )
165 tosave=( "${tosave[@]}" "FAQ" )
166 tosave=( "${tosave[@]}" "pre-install" )
167 tosave=( "${tosave[@]}" "post-install" )
168 fi
169
170 # keep Pkgfile. We always want to keep this nice funny file.
171 tosave=( "${tosave[@]}" "Pkgfile" )
172
173 # new in version 0.3 we (try to) always save files that are
174 # retrieved by cvs (non http(s) or ftp in source)
175 # ie patches, etc. This can be deleted with -d (extra files)
176 # option
177 if [ ! "$removeaddonfiles" = 1 ]; then
178 for src in "${source[@]}"; do
179 #echo $src
180 if [ "${src:0:7}" != "http://" \
181 -a "${src:0:8}" != "https://" \
182 -a "${src:0:6}" != "ftp://" ]
183 then
184 #echo "`basename "$src"`" "will NOT be deleted"
185 tosave=( "${tosave[@]}" "`basename "$src"`" )
186 fi
187 done
188 fi
189
190 for f in "$1"/{*,.footprint,.md5sum,.32bit}; do
191 if ! keep $f; then
192 remove $f
193 fi
194 done
195 fi
196 }
197
198 keep() {
199 # Chechs if filename item is in tosave[] array
200 # return 1 if present, 0 if not.
201 item=$1
202 for i in "${tosave[@]}"; do
203 if [ "$i" = `basename "$item"` ]; then
204 return 0
205 fi
206 done
207
208 return 1
209 }
210
211 printparams() {
212 # used for testing.
213 echo removepackage: $removepackage
214 echo removesources: $removesources
215 echo removeaddonfiles: $removeaddonfiles
216 echo auto: $auto
217 echo test: $test
218 }
219
220
221 ###################################################
222 # Main
223 ###################################################
224
225 trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
226
227 if [ $# -lt 1 ]; then
228 usage
229 exit -1
230 fi
231
232
233 removepackage=0
234 removesources=0
235 removeaddonfiles=0
236 test=0
237 auto=0
238 basedirs=()
239 singledirs=()
240 prtdirs=()
241
242
243 getoptions $@
244 checkparams $@
245
246
247 if [ "$auto" = 0 ]; then
248
249 for p in ${prtdirs[@]}; do
250 wash $p
251 done
252
253 else
254
255 if [ ! -f "$CONFIGFILE" ]; then
256 echo "ERROR: cannot find configuration file '$CONFIGFILE'"
257 exit -1
258 fi
259 echo "Analyzing port tree..."
260
261 getdirs
262
263 # wash base dirs
264 for d in ${basedirs[*]}; do
265 if [ -d "$d" ]; then
266 for p in "$d"/*; do
267 if [ -d "$p" ]; then
268 wash $p
269 fi
270 done
271 else
272 echo "ERROR: directory '$d' not found, check your configuration file!"
273 fi
274 done
275
276 # wash custom dirs (see prt-get.conf)
277 for p in ${singledirs[*]}; do
278 if [ -d "$p" ]; then
279 wash $p
280 else
281 echo "ERROR: directory '$p' not found, check your configuration file!"
282 fi
283 done
284 fi
285
286 exit 0
|