#!/usr/bin/perl # Copyright (c) Triffid_Hunter, 2005 # This software is licensed under the GNU GPL # available at http://www.gnu.org/copyleft/gpl.html use strict; my ($useFlagPlaceholder, $useDescAdded, $useDescRemoved) = ( "#__USE_FLAG_PLACEHOLDER___", "# USE FLAGS - SELECTED (modified by adduseflags)", "# USE FLAGS - DESELECTED (modified by adduseflags)", ); my $E = "\033"; if (@ARGV) { my @makeLines = (); my %useFlags = (); open MAKECONF, "< /etc/make.conf" or die "cannot open /etc/make.conf: $!"; while () { chomp; next if $_ eq $useDescAdded; next if $_ eq $useDescRemoved; if (/^\s*USE\s*=\s*"(?:\$\{USE\}\s+)?([\s\+\-a-z\_\d]+)/i) { my @flagList = split /\s+/, $1; print scalar(@flagList)." flags in make.conf line $.\n"; for my $flag (@flagList) { $flag =~ /^([\+\-]?)([a-z\d][a-z\d\-\_]*)$/i && do { print "duplicate flag in source file: $2\n" if exists $useFlags{$2}; $useFlags{$2} = ($1 eq '-')?0:1; next; }; print stderr "UNRECOGNISED USE FLAG: $flag\n"; } #push @makeLines, $useFlagPlaceholder; } else { push @makeLines, $_; } } close MAKECONF; for (@ARGV) { /^([\+\-\_]?)([a-z\d][a-z\d\-\_]*)$/i && do { my ($todo,$flag) = ($1,$2); $useFlags{$flag} = 1 if $todo eq '+' || $todo eq ''; $useFlags{$flag} = 0 if $todo eq '-'; delete $useFlags{$flag} if $todo eq '_' && exists $useFlags{$flag}; next; }; print "UNRECOGNISED COMMAND LINE OPTION: $_\n"; } my (@added,@removed); for (keys %useFlags) { $useFlags{$_} && do { push @added, $_; 1 } || do { push @removed, $_; }; } print scalar(keys %useFlags)." use flags in new set\n"; (unlink "/etc/make.conf~" or die "cannot remove old backup: $!") if -e '/etc/make.conf~'; (rename "/etc/make.conf", "/etc/make.conf~" or die "couldn't back up /etc/make.conf: $!") if -e '/etc/make.conf'; open MAKEFILE, "> /etc/make.conf" or die "couldn't open /etc/make.conf for writing: $!"; for (@makeLines) { if ($_ eq $useFlagPlaceholder) { } else { print MAKEFILE $_,"\n"; } } if (scalar @added) { print MAKEFILE $useDescAdded,"\n"; print MAKEFILE 'USE="'.join(' ',sort {lc($a) cmp lc($b)} @added)."\"\n"; } if (scalar @removed) { print MAKEFILE $useDescRemoved,"\n"; print MAKEFILE 'USE="${USE} -'.join(' -',sort {lc($a) cmp lc($b)} @removed)."\"\n"; } close MAKEFILE; } else { print < flags: one or more use flags, separated by spaces, optionally prepended with a "-", "+" or "_" if a use flag isn't in the list, it will be added, be it +flag or -flag. if a use flag is in the list, and its state isn't changed, no change happens. if a use flag is in the list, and its state is changed (either from +flag to -flag or vice versa), the original flag is replaced by the new one this should make the use intuitive, whereby adduseflag option will "set" a flag, and adduseflags -flag will "unset" it. to remove a flag from the list altogether, restoring its effective state to default, prepend it with a "_", as in adduseflags _ipv6 ENDHELP }