#!/usr/bin/env perl
use strict;

# File update_prjlist.pl
my $scriptName = "update_prjlist.pl";
my $scriptVersion = "1.0";

# authors: Olivier Crozier (OC)
#
# support: Olivier Crozier
#
# Revision history:
# -----------|---------|------------------------------------------
# date       | version | Description
# -----------|---------|------------------------------------------
# 2006-01-27 | 1.0, OC | Creation

###############################################################################
# Requires

###############################################################################
# Global Constants and Variables

#
# Don't use this section for more portability
#

###############################################################################
# Program start

# Use a main function for more portability
main(@ARGV);

# Program end

###############################################################################
# Sub routines

sub main
{
  # Get the parameters
  my (@Argv) = @_;

  # Local variables
  my $prjlist = "project/prjlist.txt";

  # Initialization
  my ($path) = CheckParameters($prjlist,@Argv);
  #CaptureSignal();

  # Update $prjlist
  BuildComps($prjlist,$path);
}

sub DisplayUsage
{
  # Get the parameters
  my ($prjlist) = @_;

print("
This command updates the following file with all comps found under <path>:

>> \${_TMROOT}/$prjlist

Prerequisits:
 \${_TMROOT} must be specified !!
 <path> must relative from \${_TMROOT} !!

The right syntax is:

\t$scriptName -h | -p:<path>

ex:\t$scriptName -h
ex:\t$scriptName -p:../pnx833x

");

}

sub CheckParameters
{
  # Get the parameters
  my ($prjlist,@Args) = @_;

  # Local variables
  my $path = "";

  # There must be at least one parameter
  if ($#Args < 0)
  {
    print("ERROR:\tInvalid number of parameters\n");
    DisplayUsage($prjlist);
    exit(0);
  }

  # Loop on each parameter
  foreach(@Args)
  {
    # Help
    if ($_ eq '-h')
    {
      DisplayUsage($prjlist);
      exit(0);
    }

    # Path
    elsif (/-p:/)
    {
      if ($' eq "")
      {
        print("ERROR:\tNo path specified\n");
      }
      else
      {
        $path = $';
      }
    }
  }

  # Every needed parameter must be set
  unless($path)
  {
    print("ERROR:\tWrong use of parameters\n");
    DisplayUsage($prjlist);
    exit(0);
  }

  # Display the context of the script use
  print("$scriptName script version $scriptVersion\n\n");
  print("Update '\${_TMROOT}/$prjlist' with every comps under:\n");
  print("       '\${_TMROOT}/$path'\n\n");

  # Return value
  return($path);
}

sub BuildComps
{
  # Get the parameters
  my ($prjlist,$path) = @_;

  # Open $prjlist
  my $file = "$ENV{_TMROOT}/$prjlist";
  open(PRJLIST,">>$file") || die("Can't open file $file\n");

  # Enter $path
  chdir("$ENV{_TMROOT}") || die("Can't chdir to $ENV{_TMROOT}\n");
  chdir("$path") || die("Can't chdir to $path\n");

  # Find every comps under $path
  print("Finding every 'comps'...");
  my @comps = `find . -name 'comps' -print`;
  print("\n");

  # Update $prjlist
  print("Writing to '$prjlist'...");
  foreach(@comps)
  {
    $_ =~ s/^/\${_TMROOT}\/$path\//g;
    $_ =~ s/\/\.\//\//g;
    $_ =~ s/\/\//\//g;
    $_ =~ s/\/comps//g;
    print PRJLIST ("$_");
  }
  print("\n");
}
