1#!/usr/bin/perl
2#
3# $Id: fb-filelist.pl,v 4.7 2004/08/22 20:19:09 n0ll Exp $
4#
5# Generate file list
6#
7# Copyright (C) 1990-2002
8#  _____ _____
9# |     |___  |   Martin Junius             <mj.at.n0ll.dot.net>
10# | | | |   | |   Radiumstr. 18
11# |_|_|_|@home|   D-51069 Koeln, Germany
12#
13$LIBDIR="<LIBDIR>";
14
15
16##### Options ################################################################
17require "getopts.pl";
18
19&Getopts('e:p:n:f:hv');
20
21if($opt_h) {
22    print
23	"usage:   fb-filelist [-vh] [-e EPILOG.TXT] [-p PROLOG.TXT]\n",
24	"                     [-n NEW] [-f FILEAREA.CTL]\n\n",
25	"options: -e EPILOG.TXT    set epilog for filelist\n",
26	"         -p PROLOG.TXT    set prolog for filelist\n",
27	"         -n NEW           only files of last NEW days\n",
28	"         -f FILEAREA.CTL  use Maximus FILEAREA.CTL area list\n",
29        "         -v               more verbose\n",
30	"         -h               this help\n";
31    exit 1;
32}
33
34if($opt_n) {
35    # -n days
36    $time_new = time() - $opt_n*60*60*24;
37}
38
39$PROLOG   = $opt_p ? $opt_p : "$LIBDIR/prolog.txt";
40$EPILOG   = $opt_e ? $opt_e : "$LIBDIR/epilog.txt";
41
42$FILEAREA = $opt_f ? $opt_f : "$LIBDIR/fareas.bbs";
43
44
45
46##### Configuration ##########################################################
47
48# Missing text
49$MISSING  = "--- file description missing ---";
50
51# Path translation: MSDOS drives -> UNIX path names
52%dirs = (
53    'c:', 'c:',
54    'd:', 'd:',
55    'e:', 'e:',
56    'f:', 'f:',
57    'g:', 'g:',
58    'h:', '/home',
59    'i:', '/var/spool',
60    'p:', '/u1',
61    'q:', '/u2',
62);
63
64
65
66$total_global = 0;
67
68
69# Generate listing for one directory
70
71sub list_dir {
72    local($dir) = @_;
73    local($first,$file,$desc,@files,%files_dir);
74    local($total) = 0;
75
76    # Read contents of directory
77
78    if(! opendir(DIR, $dir)) {
79	print "--- area missing ---\r\n\r\n\r\n";
80	return 1;
81    }
82
83    @files = readdir(DIR);
84    closedir(DIR);
85
86    for $file (@files) {
87	# ignore directories
88	if( -d $file ) {
89	    next;
90	}
91	# ignore .*
92	if( $file =~ /^\./ ) {
93	    next;
94	}
95	# ignore files.{bbs,bak,dat,dmp,idx}
96	if( $file =~ /^files\.(bbs|bak|dat|dmp|idx|tr)$/ ) {
97	    next;
98	}
99	# ignore index.{dir,pag}
100	if( $file =~ /^index\.(dir,pag)$/ ) {
101	    next;
102	}
103	$files_dir{$file} = 1;
104    }
105
106    # Read and print contents of files.bbs
107
108    if(open(FILES, "$dir/files.bbs")) {
109	while(<FILES>) {
110	    s/\cM?\cJ$//;
111	    # File entry
112	    if( /^[a-zA-Z0-9]/ ) {
113		($file, $desc) = split(' ', $_, 2);
114		$file  =~ tr+A-Z\\+a-z/+;
115		$files_dir{$file} = 2;
116		$total += &list_file($dir, $file, $desc);
117	    }
118	    elsif( /^-:crlf/ ) {
119		next;
120	    }
121	    elsif(!$time_new) {
122		print $_,"\r\n";
123	    }
124	}
125	close(FILES);
126    }
127    else {
128	print "--- no files.bbs ---\r\n";
129    }
130
131    # Print files missing in files.bbs / add to files.bbs
132    open(FILES, ">>$dir/files.bbs")
133	|| die "filelist: can't open $dir/files.bbs\n";
134    for $file (sort keys(%files_dir)) {
135	if($files_dir{$file} == 1) {
136	    $total += &list_file($dir, $file, $MISSING);
137	    printf FILES "%-12s  %s\r\n", $file, $MISSING;
138	}
139    }
140    close(FILES);
141
142    $total_global += $total;
143    print "\r\n    File area total:  ",&ksize($total),"\r\n";
144
145    print "\r\n\r\n";
146
147}
148
149
150
151sub list_file {
152    local($dir,$file,$desc) = @_;
153    local($x,$size,$time);
154
155    ($x,$x,$x,$x,$x,$x,$x, $size ,$x, $time ,$x,$x,$x) = stat("$dir/$file");
156
157    if($time_new && $time<$time_new) {
158	return 0;
159    }
160
161    printf "%-12s  %s %s  %s\r\n", $file, &asctime($time), &ksize($size),
162                                   $desc;
163
164    return $size;
165}
166
167
168
169sub asctime {
170    local($time) = @_;
171
172    if($time eq "") {
173	return "        ";
174    }
175    else {
176	local($yr, $mn, $dy, $h, $m, $s, $xx);
177
178	($s,$m,$h,$dy,$mn,$yr,$xx,$xx,$xx) = localtime($time);
179
180	return sprintf("%02d.%02d.%02d", $dy,$mn+1,$yr, $h,$m);
181    }
182}
183
184
185
186sub ksize{
187    local($size) = @_;
188
189    local($k);
190
191    if($size eq "") {
192	return "   N/A";
193    }
194    else {
195	if($size == 0) {
196	    $k = 0;
197	}
198	elsif($size <= 1024) {
199	    $k = 1;
200	}
201	else {
202	    $k = $size / 1024;
203	}
204	return sprintf("%6dK", $k);
205    }
206}
207
208
209
210sub dump_file {
211    local($file) = @_;
212
213    open(F, "$file") || die "filelist: can't open $file\n";
214    while(<F>) {
215	s/\cM?\cJ$//;
216	next if( /^\cZ/ );
217	print $_,"\r\n";
218    }
219    close(F);
220}
221
222
223
224##### Main #####
225
226if(-f $PROLOG) {
227    &dump_file($PROLOG);
228}
229
230if($opt_n) {
231    print "******** Listing of all files newer than ";
232    print $opt_n, " days ******************************\r\n\r\n";
233}
234
235
236# Read Maximus filearea.ctl
237
238open(F, "$FILEAREA") || die "filelist: can't open $FILEAREA\n";
239
240<F> if(!$opt_f);	# Ignore 1st line of fareas.bbs
241
242while(<F>) {
243    s/\cM?\cJ$//;
244
245    if($opt_f) {
246	# Maximus filearea.ctl
247	s/^\s*//;
248	s/\s*$//;
249	next if( /^%/ );
250	next if( /^$/ );
251
252	($keyw,$args) = split(' ', $_, 2);
253	$keyw =~ tr/[A-Z]/[a-z]/;
254
255	if   ($keyw eq "area"    ) {
256	    $area = $args;
257	}
258	elsif($keyw eq "fileinfo") {
259	    $info = $args;
260	}
261	elsif($keyw eq "download") {
262	    $dir  =  $args;
263	    $dir  =~ tr/[A-Z\\]/[a-z\/]/;
264	    $drv  =  substr($dir, 0, 2);
265	    $dir  =  substr($dir, 2, length($dir)-2);
266	    $dir  =  $dirs{$drv}.$dir;
267	}
268	elsif($keyw eq "end"     ) {
269	    $args =~ tr/[A-Z]/[a-z]/;
270	    if($args eq "area") {
271		$length = 50;
272		if(length($info) > $length) {
273		    $info = substr($info, 0, $length);
274		}
275		$length -= length($info);
276		printf "### MAX file area \#%-3d ### ", $area;
277		    print $info, " ", "#" x $length, "\r\n";
278		print "\r\n";
279		&list_dir($dir);
280	    }
281	}
282    }
283    else {
284	# FIDOGATE fareas.bbs
285	($dir,$area) = split(' ', $_);
286	$dir =~ s/^\#//;
287
288	if(! $dir_visited{$dir}) {
289	    $dir_visited{$dir} = 1;
290	    $header = "### File area $area ###";
291	    $length = 78 - length($header);
292	    print "$header", "#" x $length, "\r\n\r\n";
293	    &list_dir($dir);
294	}
295    }
296
297}
298
299close(F);
300
301print "########################################",
302      "######################################\r\n";
303print "\r\n      Listing total:  ",&ksize($total_global),"\r\n\r\n";
304print "########################################",
305      "######################################\r\n";
306
307print "\r\n";
308print "This list generated by:\r\n";
309print '$Id: fb-filelist.pl,v 4.7 2004/08/22 20:19:09 n0ll Exp $', "\r\n";
310
311if(-f $EPILOG) {
312    &dump_file($EPILOG);
313}
314