summaryrefslogtreecommitdiff
path: root/include/genzshrc
blob: 21a022a01a65c713de93aeaa7aa502bba14e8f06 (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
#!/usr/bin/env perl
use strict;
use warnings;

my $zshrc = "$ENV{HOME}/.zshrc";
my $zsource = "$ENV{HOME}/packages/zsh/etc/rc";
my %subst = (
	ZDIR => "$ENV{HOME}/packages/zsh/etc",
	HOME => $ENV{HOME},
);
my ($command, $args);

sub substitute($) {
	my $string = shift;
	my ($param, $value);
	while (($param, $value) = each(%subst)) {
		$string =~ s/\$($param|\{$param\})/$value/g;
	}
	return($string);
}

sub parse($) {
	my $file = shift;
	my $fh;
	open($fh, '<', $file) or die("Can't open $file: $!");
	while(<$fh>) {
		chomp;
		s/^\s*//;
		if (/^#/ or $_ eq '') {
			next
		}
		if (/^(\S+)(?:\s+(.+))?$/ and deploy($1, $2)) {
			next;
		}
		print;
		print "\n";
	}
	close($fh);
}

sub deploy($;$) {
	my ($command, $args) = @_;
	my $plain_args = substitute($args || '');
	if ($command =~ /^x?source$/ and -r $plain_args) {
		print "## source $plain_args\n";
		parse($plain_args);
		return(1);
	}
	return(0);
}

parse($zsource);