summarylogtreecommitdiffstats
path: root/generator.pl
blob: f7c8217ad53eaa4231f8db5a99118a8a90c909fa (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
#!/usr/bin/perl
use v5.10;
use experimental 'smartmatch';
use Tie::File;
use Template;
use LWP::UserAgent;
use Data::Dumper;

use constant {
    SOURCE => "http://www.visual-paradigm.com/downloads/vpce/Visual_Paradigm_CE_Linux64_InstallFree.tar.gz",
    BASE   => 'https://eu7.visual-paradigm.com/visual-paradigm/vpce',
};

sub _get_latest_version {
    my ($ua) = @_;

    my $req = HTTP::Request->new(HEAD => SOURCE);
    my $res = $ua->request($req);
    my $uri;

    if ( $res->is_success ) {
        $uri = $res->base;
        $uri =~ /Visual_Paradigm_CE_([0-9_]{4})_([0-9]{8})_Linux([2-6]{2})_InstallFree.tar.gz/;
        my $info = {
            base => $1,
            date => $2,
        };
        $info->{base} =~ s/_/./g;
        return $info if $info->{base} && $info->{date};
    }
    print "Cannot get response on HEAD request about latest tarball";
    exit 1;
}

sub _get_checksum_lines {
    my ($ua, $version) = @_;
    #HASHSUMS => 'https://eu7.visual-paradigm.com/visual-paradigm/vpce13.2/20160812/checksum.html'
    my $uri = BASE . $version->{base} .'/' . $version->{date} . '/checksum.html';
    my $req = HTTP::Request->new(GET => $uri);
    my $res = $ua->request($req);
    if ( $res->is_success ) {
        my @lines = split '\n', $res->content;

        return @lines;
    }
    print "Cannot get response on GET request about checksum";
    exit 2;
}

my $ua = LWP::UserAgent->new;

my $version = _get_latest_version($ua);
my @lines = _get_checksum_lines($ua, $version);

# Visual_Paradigm_CE_14_2_20180101_Linux64_InstallFree.tar.gz
my @versions = grep { $_ ~~ /Visual_Paradigm_CE_$version->{base}_$version->{date}_Linux[2-6]{2}_InstallFree.tar.gz/ } @lines;

die "Cannot get versions" unless @versions;

my %data = ();

for my $line ( @versions ) {
    $line =~ /_CE_([0-9_]+)_Linux([2-6]{2}).*sha256:\s+([0-9a-f]+).*md5:\s+([0-9a-f]+)/;
    my $info = {
        full_version => $1,
        arch         => $2,
        sha256       => $3,
        md5          => $4,

    };
    if ($info->{full_version} ~~ /(?<version>\d+_\d+)_(?<date>\d+)/) {
        $info->{$_} = $+{$_} for keys %+;
        $info->{version_} = $info->{version};
        $info->{version}  =~ s/_/./g;
    }
    my $arch = $info->{arch} eq 32 ? 'i686' : 'x86_64';
    $data{$arch} = $info;
}
if ( $data{x86_64}->{version} ) {
    my $t = Template->new(RELATIVE => 1);

    $t->process('PKGBUILD.template', $data{x86_64}, 'PKGBUILD') || die $tt->error();
}