# FILE: maintain.pl
# DESCRIPTION: Maintenance subroutines
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### cleaning_jobs
###
### Cleans up old junk in directories (temp files, old search results, etc.)
### when invoked from resource-nonintensive activities
###

sub cleaning_jobs {
	my $set;
	if (!($set = shift @_)) {
		my $proc = $$;
		$proc =~ s/\D//g;
		$proc = int(rand(100)) if $proc == 0;
		$set = $proc % 3;
	}
	if ($set == 0) {
		cleaning_remove_old_files("$DCONF->{admin_dir}/msg_index/searches", '^\d+\.txt$', 2, 0);
		cleaning_remove_old_files("$DCONF->{admin_dir}/backups", '^\w+-\d+\.conf$', 14*24, 0);
		cleaning_remove_old_files("$DCONF->{admin_dir}/msg_index/temp", '^\d+\.TMP$', 48, 0);
		cleaning_remove_old_files("$DCONF->{admin_dir}/msg_index/uploads", '^\d+\.\w+\-?\d*$', 48, 0);
	}
	if ($set == 1) {
		cleaning_remove_old_files("$DCONF->{admin_dir}/backups", '\.TMP$', 3*24, 0);
		cleaning_remove_old_files("$DCONF->{admin_dir}/msg_index", '\.TMP$', 3, 0);
		cleaning_remove_old_files("$DCONF->{admin_dir}/msg_index/lim-accs", '^[\d\.]+$', 1, 0);
		cleaning_remove_old_files("$DCONF->{admin_dir}/data/tokens", '^[\d\-]+\.txt$', 3, 0);
		cleaning_set_permissions();
	}
	if ($set == 2 && $DCONF->{pro}) {
		dreq("fcn-user-PRO");
		user_do_password_mailing(1);
	}
}

###
### cleaning_remove_old_files
###
### General subroutine to remove files from a directory if they are old
###

sub cleaning_remove_old_files {
	my ($dir, $pattern, $mtime, $atime, $timecache) = @_;
	$timecache = time if $timecache <= 0;
	my $mcut = $timecache - $mtime*3600;
	my $acut = $timecache - $atime*3600;
	my $ctr1 = 0;
	my $ctr2 = 0;
	if (opendir(DIR, "$dir")) {
		while (my $d = readdir(DIR)) {
			my $fn = join("/", $dir, $d);
			next if ! -f $fn;
			next if $d =~ m|^\.+$|;
			next if ($pattern ne "" && $d !~ /$pattern/);
			my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$a_time,$m_time,$ctime,$blksize,$blocks) = stat $fn;
			if (($mtime == 0 || $m_time < $mcut) && ($atime == 0 || $a_time < $acut)) {
				$ctr1++;
				$ctr2 += unlink $fn;
			}
		}
		closedir(DIR);
	}
	return ($ctr1, $ctr2);
}

###
### cleaning_set_permissions
###
### Sets proper permissions on various files
###

sub cleaning_set_permissions {
	if ($^O ne "MSWin32") {
		opendir(DIR, "$DCONF->{admin_dir}");
		while (my $dir = readdir(DIR)) {
			chmod(oct($DCONF->{perms0666}),"$DCONF->{admin_dir}/$dir") if $dir =~ m|\.txt$|;
			next if $dir =~ m|^\.+$|;
			chmod(oct($DCONF->{perms0777}),"$DCONF->{admin_dir}/$dir") if (-d "$DCONF->{admin_dir}/$dir" && $dir ne "source");
		}
		closedir(DIR);
		opendir(DIR, "$DCONF->{message_dir}");
		while (my $dir = readdir(DIR)) {
			next if $dir =~ m|^\.+$|;
			chmod(oct($DCONF->{perms0777}),"$DCONF->{message_dir}/$dir") if -d "$DCONF->{message_dir}/$dir";
		}
		closedir(DIR);
		opendir(DIR, "$DCONF->{secdir}");
		while (my $dir = readdir(DIR)) {
			next if $dir =~ m|^\.+$|;
			chmod(oct($DCONF->{perms0777}),"$DCONF->{secdir}/$dir") if -d "$DCONF->{secdir}/$dir";
		}
		closedir(DIR);
		chmod(oct($DCONF->{perms0666}),"$DCONF->{message_dir}/$DCONF->{board_topics_file}") if -e "$DCONF->{message_dir}/$DCONF->{board_topics_file}";
		chmod(oct($DCONF->{perms0666}),"$DCONF->{admin_dir}/secure/$DCONF->{board_topics_file}") if -e "$DCONF->{admin_dir}/secure/$DCONF->{board_topics_file}";
	}
}

1;
