create-package-feed.pl: Polish file and directory handling
[privoxy.git] / utils / create-package-feed.pl
1 #!/usr/local/bin/perl
2 #< LICENSE: WTFPL >
3 use warnings;
4 use strict;
5 use Digest::SHA1;
6 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
7 my @days   = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
8
9 #< Config START >
10 my $scan_dir   = '/xxxxxxxxxxxxxxxxxxxxxx/sf-download/';
11 my $base_dlurl = 'https://www.privoxy.org/sf-download-mirror/';
12 my $save_rss_file ='/xxxxxxxxxxxxxxxxxxxxxx/release.xml'; # e.g., release.rss
13 my $maxlimit = 1000;
14 my $max_advertised_files = 100;
15
16 #< Config END >
17
18 my @Array = ();
19 my $i     = 0;
20 my $sec;
21 my $min;
22 my $hour;
23 my $mday;
24 my $mon;
25 my $year;
26 my $wday;
27 my $yday;
28 my $isdst;
29 my $target;
30 my $target_sha1;
31 my $target_uri;
32 my $target_time;
33 my $target_line;
34
35 #
36 # 1st & 2nd directory should NOT contain ANY 'FILES'. (expecting only 'Directory')
37 #
38 opendir(my $D1, $scan_dir) or die "Can't open 1st directory! /";
39 while (my $fi1 = readdir($D1)) {
40     next if ($fi1 =~ m/^\./);
41
42     next if ($fi1 eq 'OldFiles' or $fi1 eq 'pkgsrc');
43     opendir(my $D2, $scan_dir . $fi1 . '/')
44         or die "Can't open 2nd directory! /$fi1";
45     while (my $fi2 = readdir($D2)) {
46         next if ($fi2 =~ m/^\./);
47
48 ## start listing /OS/Version/FILE
49         opendir(my $D3, $scan_dir . $fi1 . '/' . $fi2 . '/')
50             or die "Can't open 3rd directory! /$fi1/$fi2";
51         while (my $fi3 = readdir($D3)) {
52             next if ($fi3 =~ m/^\./);
53             $target = $scan_dir . $fi1 . '/' . $fi2 . '/' . $fi3;
54             next if (!-e $target);    # skip if file is not exist
55
56             # Get SHA-1 hash
57             my $filedata;
58             open($filedata, "<", $target)
59                 or die "Can't open '$target' to generate checksum $!";
60             my $sha1 = Digest::SHA1->new;
61             $sha1->addfile($filedata);
62             close($filedata);
63             $target_sha1 = $sha1->hexdigest;
64
65             # URI and Time
66             $target_uri  = $fi1 . '/' . $fi2 . '/' . $fi3;
67             $target_time = (stat $target)[9];
68
69             # RSS line
70             $target_line =
71                 '<item><title><![CDATA[' . $target_uri . ']]></title>';
72             $target_line .=
73                   '<description><![CDATA['
74                 . $target_uri
75                 . ' (SHA-1: '
76                 . $target_sha1
77                 . ')]]></description>';
78             $target_line .=
79                   '<link>'
80                 . $base_dlurl
81                 . $target_uri
82                 . '</link><guid>'
83                 . $base_dlurl
84                 . $target_uri
85                 . '</guid>';
86             $target_line .= '<pubDate>';
87             ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
88                 gmtime($target_time);
89             $target_line .=
90                   "$days[$wday], $mday $months[$mon] "
91                 . ($year + 1900)
92                 . " $hour:$min:$sec GMT";
93             $target_line .= '</pubDate></item>';
94
95             # Add it to Array
96             $Array[$i] = ([$target_time, $target_line]);
97             $i++;
98             die "maxlimit $maxlimit reached!" unless ($i < $maxlimit);
99         }
100         closedir($D3);
101 ## end listing /OS/Version/FILE
102
103     }
104     closedir($D2);
105
106 }
107 closedir($D1);
108
109 # Result = Full XML Codes
110 my $result =
111     '<?xml version="1.0" encoding="utf-8"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"><channel><title>Privoxy Releases</title><link>https://www.privoxy.org/announce.txt</link><description><![CDATA[Privoxy Releases RSS feed]]></description><pubDate>';
112 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime();
113 $result .=
114       "$days[$wday], $mday $months[$mon] "
115     . ($year + 1900)
116     . " $hour:$min:$sec GMT";
117 $result .= '</pubDate>';
118
119 # Sort Array
120 my @resArray = sort { @$a[0] <=> @$b[0] } @Array;
121 $i--;
122 while ($max_advertised_files-- > 0 && $i >= 0) {
123     $result .= $resArray[$i][1];
124     $i--;
125 }
126 $result .= '</channel></rss>';
127
128 # Save it.
129 open(my $XMLF, ">", $save_rss_file) or die "Failed to write XML file";
130 print $XMLF $result;
131 close($XMLF);