#!/usr/local/bin/perl -w

use strict;
use Getopt::Std;
use Carp;

#------------------------------------------------------------------
# Global Variables:
#------------------------------------------------------------------

use vars qw(@REJECT @ACCEPT %DEF);

#------------------------------------------------------------------
# Set the defaults. This might have to be updated.
#------------------------------------------------------------------
{
  
  my($f, @tmp);

  #--- Build tar file:
  @tmp = split(/\//, $ENV{'CODASHOME'});
  $f   = pop(@tmp);

  #--- Set defaults:
  $DEF{'tar_file'} = $f . '.tar';
  $DEF{'tar_list'} = $f . '.lst';
  $DEF{'log_file'} = "$ENV{'CODASHOME'}/gfi/tar/$f.log";
  $DEF{'find_cmd'} = "find $f/";
  $DEF{'tar_cmd'}  = "tar cvf $DEF{'tar_file'} -h -T $DEF{'tar_list'}";
  $DEF{'dir'}      = "$ENV{'CODASHOME'}/..";
  $DEF{'zip_cmd'}  = "gzip -f $DEF{'tar_file'}";
}

#------------------------------------------------------------------
# File Variables:
#------------------------------------------------------------------

#------------------------------------------------------------------
# Options
#------------------------------------------------------------------
{
  use vars qw(%OPTS);

  my($opt_str, $tmp_str, $c);

  $opt_str = 'n:w:';

  ## 1999/09/20 Pierre Jaccard
  if($] < 5.004){
    my($tmp_str);
    
    $tmp_str = $opt_str;
    while($tmp_str){
      $c = chop($tmp_str);
      next if($c !~ /\w/);
      eval( 'use vars qw($opt_' . $c . '); $opt_' . $c . ' = 0;' );
    }
    getopts($opt_str) || die("\n\n");
    $tmp_str = $opt_str;
    while($tmp_str){
      $c = chop($tmp_str);
      next if($c !~ /\w/);
      eval( '$OPTS{' . $c . '} = $opt_' . $c . ' if($opt_' . $c .');' );
    }
  }
  else{
    getopts($opt_str, \%OPTS) || die("\n\n");
  }
    
}

#--- Default options:

#------------------------------------------------------------------
# List of directories and files that should NOT be included within 
# the tar file. These must be a list of regular expressions that
# will be matched against each file name returned by the find
# command. This list will be modified by the specified options.
#------------------------------------------------------------------

@REJECT = ( 
           '\/tmp\/', 
            '\/bin\/', 
            '\/lib\/', 
            '\/old\/',
            '\.o$ ', 
            '^\#', 
            '\.upd$ ', 
            '\.m4$ ', 
            '\.sgml$ ',
            '\.tmp$ ', 
            '\.bak$ ', 
            '\.rsp$ ', 
            '\.old$ ', 
            '\~$ ', 
            'make\.log$ '
          );

#------------------------------------------------------------------
# List of directories and files that should be accepted. Nothing
# has to specified here, since every file will be accepted if
# empty.
#------------------------------------------------------------------

@ACCEPT = (
          '\/bin\/csh\/',
          '\/bin\/perl\/',
          '\/lib\/perl\/',
          '\/bin\/[^\/]*\.prl$ \/',
          );

#------------------------------------------------------------------
# Update the list of directories and files that should be 
# accepted or rejected.
#------------------------------------------------------------------
{
  
  @ACCEPT = ( @ACCEPT, split(/,/, $OPTS{'w'}) ) if($OPTS{'w'});
  @REJECT = ( @REJECT, split(/,/, $OPTS{'n'}) ) if($OPTS{'n'});

}

#------------------------------------------------------------------
# Trim spaces from regular expressions:
#------------------------------------------------------------------
{
  my($i);

  for($i=0; $i<(@REJECT); $i++){
    $REJECT[$i] =~ s/^\s*(.*?)\s*$/$1/g;
  }

  for($i=0; $i<(@ACCEPT); $i++){
    $ACCEPT[$i] =~ s/^\s*(.*?)\s*$/$1/g;
  }
}

#------------------------------------------------------------------
# MAIN:
#------------------------------------------------------------------
{
  my($r, $a, $reg);

  #--- Go to adoc directory:
  chdir($DEF{'dir'}) 
    || confess("\nMoving to directory \"$DEF{'dir'}\"\n");

  #--- Opens a pipe to the list of files:
  open(IN, "$DEF{'find_cmd'} |") 
    || confess("\nOpening pipe from find command \"$DEF{'find_cmd'}\"\n");
  
  #--- Open list of files:
  open(OUT, "> $DEF{'tar_list'}")
    || confess("\nOpening file list \"$DEF{'tar_list'}\"\n");
  
  #--- Loop through files:
  while(<IN>){
    chomp;

    #--- Jump over empty lines:
    next if(/^\s*$/);
    
    #--- Jump over directories:
    next if(-d);
    
    #---
    # To be rejected, a file must match something in REJECT but not in
    # ACCEPT. All other results will be taken in the tar file.
    #---
    $r = $a = 0;
    foreach $reg (@ACCEPT){
      $a++ if(/$reg/);
    }
    foreach $reg (@REJECT){
      $r++ if(/$reg/);
    }
    next if(!$a && $r);

    #--- Keep the file:
    print OUT "$_\n";
  }
  close(IN);
  close(OUT);

  #--- Execute the tar command:
  system($DEF{'tar_cmd'}) 
    && confess("\nExecuting tar command \"$DEF{'tar_cmd'}\"\n");

  #--- Zip the tar file:
  system($DEF{'zip_cmd'}) 
    && confess("\nExecuting zip command \"$DEF{'zip_cmd'}\"\n");
  
}

exit(0);  
  


  
  
