blob: a874f6a09971b80186d6ed240bf9fba21712cab8 (
plain)
1 #!/bin/bash
2 #
3 # $Id: prtsweep,v 1.2 2003/12/17 21:17:02 opel Exp $
4 # (c) 2002, 2003 by Martin Opel <martin@obbl-net.de>
5 #
6 # May be redistributed and modified under the terms of the GPL
7 # only usable with CRUX Linux, version 1.0 or higher
8 #
9 # USE AT YOUR OWN RISK
10 #
11 # This script scans your supfiles in /etc/ports to find out your port
12 # directories and then scans all ports for old files, which are not part
13 # of the source-array in the Pkgfile and are not pkgfiles (name#version...).
14 # All these old files are removed. For details see "man 1 prtsweep".
15 #
16
17 info() {
18 echo -e "=======> $@"
19 }
20
21 msg() {
22 echo -e "+ $@"
23 }
24
25 error() {
26 info "ERROR: $@"
27 }
28
29 interrupted() {
30 info "exiting..."
31 exit 1
32 }
33
34 getportdirs() {
35 local f
36 for f in /etc/ports/*.rsync /etc/ports/*.httpup; do
37 awk -F= '$1=="destination" || $1=="ROOT_DIR" {print $2}' $f
38 done
39 }
40
41 keepfiles() {
42 local f
43 for f in "${KEEP_FILES[@]}"; do
44 if [ "$1" = "$f" ]; then
45 return 0
46 fi
47 done
48 return 1
49 }
50
51 remove() {
52 item=$1
53 if [ -f $item ]; then
54 if [ "$dryrun" = "1" ]; then
55 msg "removing file $item (dryrun)"
56 else
57 msg "removing file $item"
58 rm $item
59 fi
60 elif [ -d $item ]; then
61 if [ "$dryrun" = "1" ]; then
62 msg "removing directory $item (dryrun)"
63 else
64 msg "removing directory $item"
65 rm -r $item
66 fi
67 fi
68 }
69
70 removeemptydir() {
71 if [ `find $1 | wc -l` -eq 1 ]; then
72 remove $1
73 else
74 info "cannot remove non-empty directory '$dir'"
75 fi
76 }
77
78 inlist() {
79 item=$1
80 shift
81 list="$@"
82
83 for i in $list; do
84 if [ "$i" = "$item" ]; then
85 return 0
86 fi
87 done
88
89 return 1
90 }
91
92 sweep() {
93 dir=$1
94 savedir=`pwd`
95 printedpwd="false"
96
97 if [ ! -d $dir ]; then
98 error "'`pwd`/$dir' is not a directory!"
99 usage
100 fi
101
102 if [ ! -f $dir/Pkgfile ]; then
103 info `pwd`/$dir
104 msg "no Pkgfile found, empty port '$dir'."
105 if [ "$delete" = "1" ]; then
106 removeemptydir $dir
107 else
108 msg "use '-d' to remove empty directories."
109 fi
110 return
111 fi
112
113 cd $dir
114 unset name version source pkgfiles
115 . Pkgfile
116
117 for f in ${source[@]}; do
118 pkgfiles=( `basename $f` ${pkgfiles[@]} )
119 done
120 for f in `ls -a`; do
121 if ! keepfiles "$f"; then
122 if ! echo $f | grep '.*#.*\.pkg\.tar\.[gbx]z*' >/dev/null; then
123 if ! inlist $f ${pkgfiles[@]}; then
124 if [ "$printedpwd" = "false" ]; then
125 printedpwd="true"
126 info `pwd`
127 fi
128 remove $f
129 fi
130 fi
131 fi
132 done
133 cd $savedir
134 }
135
136 getoptions() {
137 while [ "$1" = "-a" -o "$1" = "-d" -o "$1" = "-n" ]; do
138 if [ "$1" = "-n" ]; then
139 dryrun=1
140 shift
141 fi
142 if [ "$1" = "-d" ]; then
143 delete=1
144 shift
145 fi
146 if [ "$1" = "-a" ]; then
147 auto=1
148 shift
149 fi
150 done
151 ports="$@"
152 }
153
154 usage() {
155 echo
156 echo "Usage: prtsweep [-a] [-d] [-n] [PORTDIR ...]"
157 echo
158 exit 1
159 }
160
161 trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
162
163 getoptions $@
164
165 KEEP_FILES=(
166 "Pkgfile" "README" "pre-install" "post-install"
167 ".footprint" ".md5sum" ".signature" ".32bit" ".nostrip"
168 "." ".."
169 )
170
171
172 if [ -z "$ports" -a "$auto" = "1" ]; then
173 for portdir in `getportdirs`; do
174 savedir=`pwd`
175 cd $portdir
176 for dir in *; do
177 sweep $dir
178 done
179 cd $savedir
180 done
181 exit 0
182 elif [ ! -z "$ports" ]; then
183 for dir in $ports; do
184 sweep $dir
185 done
186 exit 0
187 fi
188
189 usage
190 exit 1
|