summaryrefslogtreecommitdiff
path: root/readlicense_oo/conv.pl
blob: 4f2780c9dc778e45f62fcba39ee757d3f22b3914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl

use strict;
use File::Find ();
use File::Basename ();
use File::Path ();
use Getopt::Std;
use Cwd;

my ($startdir,$outdir,$pfx);
my $verbose = 0;

sub usage() {
    print STDERR "usage: $0 -o <outdir> [-f <file>]\n"
}

$startdir=cwd();

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

if ( !getopts('o:f:') ) {
    usage();
    exit(1);
}

if ( defined($Getopt::Std::opt_o) ) {
    $outdir=$Getopt::Std::opt_o;
    $outdir=~s%\\%/%g;
} else {
    usage();
    exit(1);
}

if ( defined($Getopt::Std::opt_f) ) {
    convertfile($outdir,$Getopt::Std::opt_f);
} else {
    # Traverse desired filesystems
    $pfx="source";
    File::Find::find({wanted => \&wanted}, 'source');
}
exit;


sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (
    /^.*\.html\z/s
    ||
        /^license.*\.txt\z/s
        ||
        /^LICENSE.*\z/s
        ||
        /^readme\.txt\z/s
        ||
        /^README\z/s
    ) &&
    ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
    eval {&doconv};
}

sub doconv {
    my($dest);
    $dest=$dir;
    $dest=~s/^$pfx//g;
    $dest=$outdir.$dest;
    convertfile($dest,$name) if $name =~ /\/$ENV{GUI}\//i;
}

sub convertfile {
    my ($dest,$file,$destfile);
    $dest=shift;
    $file=shift;
    $dest=~s%^./%%g;
    $dest=$startdir . "/" . $dest;
    $file=$startdir . "/" . $file;

    $destfile=$dest . "/" . File::Basename::basename($file);

    File::Path::mkpath($dest,0,0775);

    open(IN,"<$file") || die "can not open $file";
    open(OUT,">$destfile") || die "can not open $destfile";
    print "converting: $file -> $destfile\n" if $verbose;
    while (<IN> ) {
        chop while ( /\n$/ || /\r$/ );
        print OUT "$_\n";
    }
    close(IN);
    close(OUT);
}