#!/usr/bin/perl -T --
# -*- Mode: perl; indent-tabs-mode: nil -*-
#

# rmlogs - remove the log files which are older then the number of
#          days set in TinderConfig.  This program should be 
# 	   run from cron daily.

# $Revision: 1.10 $ 
# $Date: 2003/08/17 00:44:05 $ 
# $Author: kestes%walrus.com $ 
# $Source: /cvsroot/mozilla/webtools/tinderbox2/src/bin/rmlogs,v $ 
# $Name:  $ 

# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Tinderbox build tool.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#

# complete rewrite by Ken Estes for contact info see the
#     mozilla/webtools/tinderbox2/Contact file.
# Contributor(s): 



# Standard perl libraries
use File::Find;

# Tinderbox libraries

use lib '/usr/share/tinderbox/lib', '/etc/tinderbox';

use TinderConfig;
use TreeData;
use FileStructure;
use Utils;




$BRIEF_TRIM = $TinderConfig::BRIEF_LOG_TRIM_DAYS || 1;
$FULL_TRIM = $TinderConfig::FULL_LOG_TRIM_DAYS || 7;





# If the argument to this function is a log file which is older then
# $old_days, we remove the file.

sub rm_logfile {
  my ($old_days, $dir, $file) = @_;
  my $full_path = join('/', $dir, $file);

  ($full_path =~ m/\.html\.gz$/) ||
    return 1;

  # untaint path
  $full_path = main::extract_safe_filename($full_path);

  # save stat info for the file, incase we need it in the future.

  my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($full_path);

  # There may be other files in the directory besides our logs, skip
  # them.  If our file name is not a file skip it as well (security
  # issues).

  ( -f _ ) ||
    return 1;

  # Remove files older than 7 days
  if ( -M _ > $old_days) {
    unlink($full_path) || 
      die("Could not remove file: $full_path\n;");
  }

  return 1;
}


# search all the log file directories for files which need to be
# removed.

sub rm_old_logs {
  my ($dir_type, $old_days) = @_;

  my (@trees) = TreeData::get_all_trees();
  my (%dirs) = ();

  # find the set of directories to search.  We want to traverse each
  # directory only once even if it is used for multiple trees.

  foreach $tree (@trees) {
    my ($dir_full) = FileStructure::get_filename($tree, $dir_type);
    $dirs{$dir_full} = 1;
  } 

  # Traverse each directory and remove log files which are old.

  foreach $dir (keys %dirs) {

    opendir(DIR, $dir) ||
      die("Could not open: $dir. $!\n");

    while (defined ($file = readdir(DIR))) {
      rm_logfile($old_days, $dir, $file);
    }
    
    closedir(DIR) ||
      die("Could not close: $dir. $!\n");
    
  }

  return 1;
}



# --------------------main-------------------------
{
  set_static_vars();
  get_env();
  chk_security();

  rm_old_logs('full-log', $FULL_TRIM);
  rm_old_logs('brief-log', $BRIEF_TRIM);

  exit 0;
}
