#!/usr/bin/env perl
#-*-perl-*-

# Users account mannager. Designed to be architecture and distribution independent.
#
# Copyright (C) 2000 JP Rosevear
#
# Authors: JP Rosevear <jpr@helixcode.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

# Best viewed with 100 columns of width.

# --- Usage text --- #

my $Usage =<<"End_of_Usage;";
Usage: gbf-am-build <--build TARGET | --help | --version> [--verbose] project-dir

       Major operations (specify one of these):

       -b --build     Prints the current project sources to standard
                         output, as as standalone XML document.

       -h --help     Prints this page to standard output. That\'s all.

          --version  Prints version information to standard output.

       Modifiers (specify any combination of these):

       -v --verbose  Turns on diagnostic messages to standard error.

End_of_Usage;

$version = "0.0.1";

# --- Internal variables --- #
$target = "";

# --- Constants --- #
$make_file = "Makefile.am";

# --- Utility stuff --- #

# --- Build stuff --- #
sub build
{
    if ($verbose) { print STDERR "Building\n"; }
    my ($file, $line, $dir, $curr_dir, @desc);
    
    foreach $_ (`make --keep-going --directory=$project_dir $target 2>&1`) {
#	print $_;
	chomp;
	if (/Entering directory \`([^\']*)\'/) {
	    $curr_dir = $1;
	    $curr_dir =~ s/$project_dir//;
	}
	if (/^(^\S*):(\d*): (.*)/) {
	    if (($file ne "" && $file ne $1) || ($line ne "" && $line ne $2)) {
		print STDERR "ERROR: $dir $file $line @desc\n";
		@desc = qw ();
	    }
	    $file=$1;
	    $line=$2;
	    $dir=$curr_dir;
	    @desc=(@desc, $3);
	} elsif (/-c (.*)/) {
	    print "FILE: $curr_dir $1\n";
	}
    }
    if ($file ne "" && $line ne "") {
	print "ERROR: $dir $file $line @desc\n";
    }
}

# --- Usage and version --- #

sub usage 
{
    print $Usage; 
    exit(0);
}

sub version
{
    print "$version\n";
    exit (0);
}

# --- Main --- #

# Process options.

while (@ARGV)
{
  if ($ARGV[0] eq "--build"   || $ARGV[0] eq "-b") { 
      $operation = "build";
      $target = $ARGV[1];
      if ($target eq "") {
        print STDERR "Error: You must specify an argument to the --build option.\n\n";
	print STDERR $Usage; exit(1);
      }

      shift @ARGV;
  } elsif ($ARGV[0] eq "--verbose" || $ARGV[0] eq "-v") { $verbose = 1; }
  elsif ($ARGV[0] eq "--help"    || $ARGV[0] eq "-h") { $operation = "usage"; }
  elsif ($ARGV[0] eq "--version")                     { $operation = "version" }
  else {
    if ($project_dir ne "") {
	print STDERR "Error: You may specify only one project directory.\n\n";
	print STDERR $Usage; exit(1);
    }

    $project_dir = $ARGV[0];
    if (!-e $project_dir || !-d $project_dir) {
	print STDERR "Error: project directory does not exist\n\n";
	print STDERR $Usage; exit(1);
    }

    if (!-e "$project_dir/$make_file") {
	print STDERR "Error: project directory does not contain appropriate files\n\n";
	print STDERR $Usage; exit(1);
    }
  }

  shift @ARGV;
}


# Do our thing.
if ($project_dir eq "") {
    print STDERR "Error: No project directory specified.\n\n";
    print STDERR $Usage; exit(1);
}

if    ($operation eq "build")    { &build; }
elsif ($operation eq "usage") { &usage; }
elsif ($operation eq "version") { &version; }
else {
  print STDERR "Error: No operation specified.\n\n";
  print STDERR $Usage; exit(1);
}


