9 Usage: $0 <file> <command> [<arguments>]
12 add-hash <variable> <value>
13 fix-hash <variable> <value>
14 rename-var <variable> <name>
23 if ($var =~ /(.*):(.*)/) {
24 $state{template} = $1;
26 $state{related_var} = "URL";
29 $state{related_var} = "PKG_SOURCE";
37 $state{value} = $ARGV[1];
38 length($ARGV[1]) == 64 or die "Invalid hash value\n";
43 $state{value} = $ARGV[1];
44 $state{prev_value} = $ARGV[2];
46 length($ARGV[1]) == 64 or die "Invalid hash value\n";
50 $state{new_var} = $ARGV[1];
51 $state{new_var} =~ s/.*://g;
55 sub check_context($) {
57 return unless $state{template};
65 if (not $state{context}) {
66 $line =~ /^\s*define\s+$state{template}/ and $state{next} = 1;
68 $line =~ /^\s*endef/ and do {
70 undef $state{context};
79 return $line unless $state{context};
81 # skip existing hash variable
82 return "" if $line =~ /^(\s*)$state{var}(\s*):?=(\s*)(.*)\n/;
84 # insert md5sum after related variable
85 return $line unless $line =~ /^(\s*)$state{related_var}(\s*):?=(\s*)(.*)\n/;
86 return "$line$1$state{var}$2:=$3$state{value}\n";
91 return $line unless $state{context};
92 return $line unless $line =~ /^(\s*)$state{var}(\s*):?=(\s*)(.*)$state{prev_value}(.*)\n/;
95 warn "$state{var} contains a reference to another variable, can't fix automatically\n";
98 return "$1$state{var}$2:=$3$state{value}\n";
100 "rename-var" => sub {
102 check_context($line);
103 return $line unless $state{context};
104 return $line unless $line =~ /^(\s*)$state{var}(\s*:?=.*)\n/;
105 return "$1$state{new_var}$2\n";
109 my $file = shift @ARGV;
110 my $command = shift @ARGV;
112 ($file and $command and $check_command{$command}) or usage;
113 &{$check_command{$command}}();
115 -f $file or die "File $file not found\n";
117 open IN, "<${file}" or die "Cannot open input file\n";
118 open OUT, ">${file}.new" or die "Cannot open output file\n";
120 my $cmd = $commands{$command};
121 while (my $line = <IN>) {
122 $line = &$cmd($line) unless $state{done};
131 unlink "${file}.new";
135 rename "${file}.new", "$file";