tinydns/concatenate_dns_datafiles.pl
2017-02-21 02:35:57 +01:00

170 lines
3.7 KiB
Perl
Executable File

#!/usr/bin/perl
use strict;
use Symbol;
use Getopt::Std;
use Cwd;
use vars qw/$opt_e $opt_h $opt_o $opt_q $opt_r $opt_R $opt_X/;
my $zonefile_suffixi_default = "file";
my $outfile_default = "data_dns";
my ( @lines );
my ( $extension, $dirdepth, $recursiv, $excl_dir, $outfile );
my $i ;
getopts('e:ho:qrR:X:') || &usage();
&usage if defined $opt_h ;
if (! defined $opt_q) {
print "\n command invoked: $0";
foreach $i (@ARGV) {
print " $i";
}
print "\n\n";
}
exit ( &main(@ARGV) );
## - main method
##
sub main {
my $rootdir = shift || return usage();
$rootdir =~ s#/$## ;
## - like to work with the realpath
## -
$rootdir = Cwd::abs_path($rootdir);
## - look at the programm parameters
## -
$extension = defined($opt_e) ? $opt_e : $zonefile_suffixi_default;
$outfile = defined($opt_o) ? $opt_o : $outfile_default;
$dirdepth = defined($opt_R) ? $opt_R : -1;
if ($dirdepth > 0 ) {
$recursiv = 1 ;
} else {
$recursiv = defined($opt_r) ? 1 : 0;
}
$excl_dir = defined($opt_X) ? $opt_X : "";
&traverse_dir($rootdir,0);
&write_file("data_dns",@lines);
exit (0)
}
## - traverse_dir
## -
sub traverse_dir {
my $dir = shift ;
my $dircount = shift ;
my $dirh = gensym ;
my $filename ;
my @dir ;
opendir($dirh , $dir) || die "can't open $dir";
@dir=readdir($dirh);
@dir = sort(@dir);
closedir($dirh);
#while ( defined ( $filename = readdir($dirh) ) ) {
foreach ( @dir ) {
$filename = $_;
next if $filename eq "." || $filename eq ".." ;
## - no system files
next if ( $filename =~ /^\./ ) ;
my $pathname = $dir . "/" . $filename ;
next if -l $pathname ;
next if ( -d $pathname && $filename eq $excl_dir ) ;
if ( -d $pathname && $recursiv
&& ($dirdepth == -1 || $dircount < $dirdepth)
) {
&traverse_dir($pathname, ($dircount + 1 )) ;
next ;
}
next if not -T $pathname ;
next unless -f $pathname;
if ($extension ne "") {
next if not ( $pathname =~ /$extension$/ ) ;
}
open (CFG, $pathname ) or die "open file $pathname: $!\n";
while ( <CFG> ) {
push(@lines , $_);
}
close CFG;
push (@lines, "\n\n");
}
## - write out concatinated file
## -
&write_file($outfile, @lines);
return 0;
} # ende traverse_dir
# Datei schreiben
#
sub write_file {
my $outfile = shift ;
my @lines = @_ ;
open ( FH , "> $outfile") or die "can't open $! for write" ;
print FH @lines ;
close (FH);
}
#
# - usage
#
sub usage {
my $file = $0;
$file =~ s#.*/([^/]+)$#$1# ;
print <<ENDE;
Usage: $file [OPTIONS] <zone-file-directory>
This script concatinates all zonefiles with a certain fileextension
of a given directory.
Control Options:
-e <ext> concatinate files with extension ext
defaults to "$zonefile_suffixi_default"
-h prints this helpcontents
-o <outfile> name of outputfile. defaults to "$outfile_default" in the
working directory
-q silent output
-r recursivly
-R NUM concatinate files recursivly with directorydepth = "NUM"
-X dir excluding replacing directories named dir
ENDE
exit 1 ;
}