#!/usr/bin/perl 
#===============================================================================
#
#         FILE:  epu.pl
#
#        USAGE:  ./epu.pl <package/name> <useflags>
#
#  DESCRIPTION:  This script is able to manage the /etc/portage/package.use file
#
#      OPTIONS:  <package/name> must be a valid portage package e.g. "net-im/gaim",
#      			 <useflag> must be a valid useflag e.g. "-perl" to disable
#      			 or "perl" to enable or add
#      			 (without quotes (" ") of course ;).
# REQUIREMENTS:  Gentoo (portage)
#         BUGS:  none reported
#        NOTES:  none
#       AUTHOR:  ViLeDa (Tristan Leo), <filecorpse@gmail.com>
#      COMPANY:  Schmerzerzeuger
#      VERSION:  1.1
#      CREATED:  07.09.2006 19:27:33 CEST
#     $Revision: 23 $
#     $LastChangedDate: 2006-09-17 02:16:20 +0200 (So, 17 Sep 2006) $
#
#===============================================================================

use strict;
use Tie::File;

#Check commandline
unless ( @ARGV > 1 && $ARGV[0] =~ m/\w+\/\w+/ ) {
    print "Syntax: <package> <useflags>
For example:
	to enable a flag:     epu net-im/gaim perl
	or to disable a flag: epu net-im/gaim -perl\n";
    die "More help informations soon.\n";
}

#check if the package really exist
my $eix = `eix -c $ARGV[0]` or die "eix failed\n";
my $wc = `echo \"$eix\" | wc -l`;
if ($eix =~ m/No matches found/) {
	print "No matches for package $ARGV[0]\n";
	exit 1;
}elsif(($wc - 1) > 1) {
	print "Too many matches for the package\n";
	exit 1;
}elsif ($eix !~ m/[ ]($ARGV[0])[ ]/)
{
	print "Did you mean: ";
	$eix =~ m/^\[.+\][ ]([\w\-\/]+)[ ].+/;
	print "$1 ?\n";
	exit 1;
}
#some declarations
my $package  = shift @ARGV;
my @useflags = sort @ARGV;
my $usefile  = './package.use'; #change this to package.use location, it should be /etc/portage/package.use
my $fuseflags;
my $fpackage;
my $haspackage;
my $i = 0;

#read the file into an array
tie my @uselines, 'Tie::File', $usefile;

#look if package already in there
for my $fuseline (@uselines) {

    #regexp for package match and useflag extraction
    if ( $fuseline =~ m/^($package)[ ](.*)$/sg )
    {    #package is already in the file
            #save useflags
        $fpackage   = $1;
        $fuseflags  = $2;
        $haspackage = 1;
        last;
    }
    $i++;
}

if(!$haspackage) {
	my $newline = $package . " " . join( " ", @useflags );
	$uselines[@uselines] = $newline; #write new line to file
	print "New line: $newline\n";
	untie @uselines;
	exit 0;
}

for my $flags (@useflags) {
    next if $fuseflags =~ m/(^($flags)|(\s$flags)|(\s$flags\s))/; #flag is already in the file
    if ( $fuseflags =~ s/([-]$flags)/$flags/ ) { #enabling a disabled flag
        print "Setting $1 to $flags : Enabling\n";
        next;
    }
    if ( $flags =~ m/([-](.+))/s and $fuseflags =~ m/($2)/s ) { #disable an enabled flag
        print "Setting $1 to $flags : Disabling\n";
        $fuseflags =~ s/$1/$flags/;
    }
    else { #add new flag
        print "Adding useflag: $flags \n";
        $fuseflags .= " " . $flags;
    }
}

#write new line
$uselines[$i] =
  $package . " " . join( " ", reverse sort split( " ", $fuseflags ) );

print "Altered line: ", $uselines[$i], "\n";

untie @uselines;

# vim:ts=8:sw=4:ft=perl
