#!/usr/bin/perl
if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
}
require "RSA.lib";


#### initialise parameters ####
$start_time = &RSAT::util::StartScript();


#### read arguments ####
foreach $a (0..$#ARGV) {

    if ($ARGV[0] eq "-h") {
	&PrintHelp();
    } elsif ($ARGV[0] eq "-help") {
	&PrintOptions();
    ### verbose ###
    } elsif ($ARGV[$a] eq "-v") {
	$verbose = 1;
    ### input file ###
    } elsif ($ARGV[$a] eq "-i") {
	$inputfile = $ARGV[$a+1];
    ### output file ###
    } elsif ($ARGV[$a] eq "-o") {
	$outputfile = $ARGV[$a+1];

    } elsif ($ARGV[$a] eq "-before") {
	$before = 1;

    }
}


#### check argument values ####



### open input file ###
($in, $input_dir) = &OpenInputFile($inputfile);

### open output file ###
$out = &OpenOutputFile($outputfile);

#### verbose ####
if ($verbose) {
    print $out ";add-linenb result\n";
    if ($inputfile ne "") {
	print $out ";Input file	$inputfile\n";
    }
    if ($outputfile ne "") {
	print $out ";Output file	$outputfile\n";
    }
}

################################################################
## Read input + write output
$line_counter = 0;
while (<$in>) {
  if ((/^;/)			## Comment line
      || (/^#/)			## Header line
     ) { 
    print $out $_;
  } else {
    $line_counter++;
    if ($before) {
      print $out $line_counter, "\t", $_;
    } else {
      chomp;
      print $out $_, "\t", $line_counter, "\n";
    }
  }
}


## close input file
close $in unless ($inputfile eq "");


################################################################
## Report execution time and close output stream
my $exec_time = &RSAT::util::ReportExecutionTime($start_time); ## This has to be exectuted by all scripts
print $main::out $exec_time if ($main::verbose >= 1); ## only report exec time if verbosity is specified
close $main::out if ($outputfile);


exit(0);


################################################################
#################### Subroutine definition #####################
################################################################

################################################################
#### display full help message #####
sub PrintHelp {
  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	add-linenb

        1998 by Jacques van Helden (jvanheld\@bigre.ulb.ac.be)
	
USAGE
        add-linenb [-i inputfile] [-o outputfile] [-v]

DESCRIPTION
	add a column to the input file indicating the line number

CATEGORY
	util	

OPTIONS
        -h      (must be first argument) display full help message
        -help   (must be first argument) display options
	-v	verbose
	-i inputfile
		if not specified, the standard input is used.
		This allows to place the command within a pipe.
	-o outputfile
		if not specified, the standard output is used.
		This allows to place the command within a pipe.
	-before	add line number before line content 
		(by default, line number is added at the end of the line)
	
INPUT FORMAT
      any text file	
      lines beginning with a semicolon (;) are considered as comments 
      and are returned without numbering
	
OUTPUT FORMAT
      the same file with one column added (separated by a tab) 
      indicating the line number 
	
EXAMPLES
       add-linenb -v -i mydata -o myresult
	
End_of_help
  close HELP;
  exit;
}

################################################################
#### display short help message #####
sub PrintOptions {
  open HELP, "| more";
  print HELP <<End_short_help;
add-linenb options
----------------
-h      (must be first argument) display full help message
-help   (must be first argument) display options
-i      input file
-o      output file
-v      verbose
-before	insert line number before line content
End_short_help
  close HELP;
  exit;
}
