1#!/usr/bin/tclsh
2
3proc is-section line {
4	return [regexp {^[A-Z ]+$} $line]
5}
6
7# First argument is Manifest file, others are sections.
8set sections [lassign $argv maffile]
9
10if { $sections == "" } {
11	puts stderr "Usage: [file tail $argv0] <MAF file> <section name>"
12	exit 1
13}
14
15# NOTE: If the file doesn't exist, simply print nothing.
16# If there's no manifest file under this name, it means that
17# there are no files that satisfy given manifest and section.
18if { [catch {set fd [open $maffile r]}] } {
19	exit
20}
21
22set extracted ""
23set insection 0
24
25while { [gets $fd line] >= 0 } {
26	set oline [string trim $line]
27	if { $oline == "" } {
28		continue
29	}
30
31	if { [string index $oline 0] == "#" } {
32		continue
33	}
34
35	if { !$insection } {
36		# An opportunity to see if this is a section name
37		if { ![is-section $line] } {
38			continue
39		}
40
41		# If it is, then check if this is OUR section
42		if { $oline in $sections } {
43			set insection 1
44			continue
45		}
46	} else {
47		# We are inside the interesting section, so collect filenames
48		# Check if this is a next section name - if it is, stop reading.
49		if { [is-section $line] } {
50			continue
51		}
52
53		# Otherwise read the current filename
54		lappend extracted $oline
55	}
56}
57
58puts $extracted
59