summaryrefslogtreecommitdiffstats
path: root/bin/modpkg
blob: 284923a396de81c827af1ae2810bf8ca7cd55c7c (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
99
100
101
102
#!/usr/bin/env tclsh

set prog modpkg
set pbfields {pkgname pkgver pkgrel pkgdesc pkgbase
	epoch url license install changelog source noextract
	md5sums sha1sums sha256sums sha384sums sha512sums
	groups arch backup
	depends makedepends checkdepends optdepends
	conflicts provides replaces
	options}
set pbfuncs {build check package}
set dotfuncs {pre_install post_install
	pre_upgrade post_upgrade
	pre_remove post_remove}

proc scanfields {inchan} {
	global pkgdata
	seek $inchan 0

	set fld {}
	while {[gets $inchan line] >= 0} {
		if {$fld eq {}} {
			set fld $line
		} elseif {$line eq {}} {
			set pkgdata($fld) $vals
			set fld {}
			set vals {}
		} else {
			lappend vals $line
		}
	}
	if {$fld != {}} { set pkgdata($fld) $vals }
}

proc printfields {outchan} {
	global pkgdata

	foreach {fld vals} [array get pkgdata] {
		puts $outchan [join [concat $fld $vals] "\n"]
		puts ""
	}
}

if {$argc != 1} {
	puts stderr "usage: $prog \[path to modifier script\] < PKGDATA > PKGDATA.new"
	exit 2
}

set mod [lindex $argv 0]
if {! [file exists $mod]} {
	puts stderr "$prog: error: mod file does not exist"
	exit 1
}
set modch [open $mod]

scanfields stdin

set modi [interp create -safe $mod]
foreach {name vals} [array get pkgdata] {
	if {$name in $pbfuncs} {
		$modi eval set $name $val
	}
}

$modi eval {
	proc trimbash {code} {
		set code [string trim $code]
		set lines [split $code "\n"]
		for {set i 0} {$i < [llength $lines]} {incr $i} {
			set ln [string trim [lindex $lines $i]]
			set ln [concat [string repeat " " 2] $ln "\n"]
			linsert $lines $i $ln
		}
		return [join $lines ""]
	}

	proc fappend {name code} {
		if {! $name in $pbfuncs} {
			puts stderr "$prog: error: $name is not a permitted PKGBUILD func"
			exit 2
		}
		exec "putpkgtree PKGBUILD $name body" << [trimbash $code]
	}

	proc fprepend {name code} {
		if {! $name in $pbfuncs} {
			puts stderr "$prog: error: $name is not a permitted PKGBUILD func"
			exit 2
		}
		exec "putpkgtree PKGBUILD $name beg" << [trimbash $code]
	}
}

$modi eval [read $modch]

foreach name $pbfields {
	if {[$modi eval info exists $name]} {
		set pkgdata($name) [$modi eval set $name]
	}
}

printfields stdout