1package metadata;
2use base 'Exporter';
3use strict;
4use warnings;
5our @EXPORT = qw(%package %srcpackage %category %subdir %preconfig clear_packages parse_package_metadata get_multiline);
6
7our %package;
8our %preconfig;
9our %srcpackage;
10our %category;
11our %subdir;
12
13sub get_multiline {
14	my $fh = shift;
15	my $prefix = shift;
16	my $str;
17	while (<$fh>) {
18		last if /^@@/;
19		s/^\s*//g;
20		$str .= (($_ and $prefix) ? $prefix . $_ : $_);
21	}
22
23	return $str ? $str : "";
24}
25
26sub clear_packages() {
27	%subdir = ();
28	%preconfig = ();
29	%package = ();
30	%srcpackage = ();
31	%category = ();
32}
33
34sub parse_package_metadata($) {
35	my $file = shift;
36	my $pkg;
37	my $makefile;
38	my $preconfig;
39	my $subdir;
40	my $src;
41
42	open FILE, "<$file" or do {
43		warn "Cannot open '$file': $!\n";
44		return undef;
45	};
46	while (<FILE>) {
47		chomp;
48		/^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
49			$makefile = $1;
50			$subdir = $2;
51			$src = $3;
52			$subdir =~ s/^package\///;
53			$subdir{$src} = $subdir;
54			$srcpackage{$src} = [];
55			undef $pkg;
56		};
57		/^Package:\s*(.+?)\s*$/ and do {
58			$pkg = {};
59			$pkg->{src} = $src;
60			$pkg->{makefile} = $makefile;
61			$pkg->{name} = $1;
62			$pkg->{default} = "m if ALL";
63			$pkg->{depends} = [];
64			$pkg->{builddepends} = [];
65			$pkg->{subdir} = $subdir;
66			$pkg->{tristate} = 1;
67			$package{$1} = $pkg;
68			push @{$srcpackage{$src}}, $pkg;
69		};
70		/^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
71		/^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
72		/^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
73		/^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
74		/^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
75		/^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
76		/^Provides: \s*(.+)\s*$/ and do {
77			my @vpkg = split /\s+/, $1;
78			foreach my $vpkg (@vpkg) {
79				$package{$vpkg} or $package{$vpkg} = { vdepends => [] };
80				push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
81			}
82		};
83		/^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
84		/^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
85		/^Category: \s*(.+)\s*$/ and do {
86			$pkg->{category} = $1;
87			defined $category{$1} or $category{$1} = {};
88			defined $category{$1}->{$src} or $category{$1}->{$src} = [];
89			push @{$category{$1}->{$src}}, $pkg;
90		};
91		/^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
92		/^Type: \s*(.+)\s*$/ and do {
93			$pkg->{type} = [ split /\s+/, $1 ];
94			undef $pkg->{tristate};
95			foreach my $type (@{$pkg->{type}}) {
96				$type =~ /ipkg/ and $pkg->{tristate} = 1;
97			}
98		};
99		/^Config: \s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
100		/^Prereq-Check:/ and $pkg->{prereq} = 1;
101		/^Preconfig:\s*(.+)\s*$/ and do {
102			my $pkgname = $pkg->{name};
103			$preconfig{$pkgname} or $preconfig{$pkgname} = {};
104			if (exists $preconfig{$pkgname}->{$1}) {
105				$preconfig = $preconfig{$pkgname}->{$1};
106			} else {
107				$preconfig = {
108					id => $1
109				};
110				$preconfig{$pkgname}->{$1} = $preconfig;
111			}
112		};
113		/^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
114		/^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
115		/^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
116	}
117	close FILE;
118	return 1;
119}
120
1211;
122