#!/usr/bin/perl -w -I/usr/sausalito/perl
# 
# $Id: cobalt_restorequotas,v 1.2 2001/04/06 22:15:21 bservies Exp $
#
# Copyright 2000 Cobalt Networks, Inc., All Rights Reserved
#

#
# This script reads group and user information from the cobalt postgres
# database and resets the OS disk quotas based on this information.
#
use vars qw (
	@errors
	$err
	$version
	$product
	$locale
);


sub determine_features {
        my ($version, $product, $locale);
 
        #
        # Determine which product we are running on for product-specific features
        #
        open (IFILE, "< /etc/build");
        while (<IFILE>) {
                chop;
                next if (/^\s*$/o); # skip blank lines (there shouldn't be any)
                next if (/^#.*$/o); # skip comments (there shouldn't be any)
                $build = $_;
        }
        close (IFILE);
 
        # Parse the information read from the build file
        ($product) = ($build =~ /([0-9]{4}[A-Z]{0,2})/);
        ($version) = ($build =~ /([\d]+\.[\d]+)/);
        ($locale) = ($build =~ /(\S+)$/);
 
        if ($product =~ /35[09]{2}/) {
                # RaQ XTR or equivalent build (monterey)
                $has_ui_lock = 1;
                $has_cce = 1;
                $has_postgres = 1;
 
        } elsif ($product =~ /^4[01][01]0/) {
                # Qube3 (carmel)
                $has_cce = 1;
                $has_ui_lock = 0;
                $has_postgres = 0;
 
        } else {
                # Unknown
                if (-d "/usr/sausalito/codb") {
                        $has_cce = 1;
                }
                if (-d "/var/lib/pgsql/data/base/cobalt") {
                        $has_postgres = 1;
                }
                $has_ui_lock = 0;
        }
}



sub fix_site_quotas {

	my (@vsites, $vsite, %sitehash, $rc, $msg);

	# Get a list of virtual sites from the cobalt postgres database.
	@vsites = Cobalt::Meta::query('keys' => [ 'name', 'quota' ], 'type' => 'vsite');
	foreach $vsite (@vsites) {
		%sitehash = %{$vsite};

		# Skip the default group: setting the quota will fail.
		next if ($sitehash{"name"} eq "default");

		#
		# Adjust the quota information to be in blocks.  The
		# database stores quota information in megabytes, while the
		# routines to update the quota take absolute values.
		#
		$sitehash{"quota"} *= 1024;

		# Update the quota for this group
		$rc = Cobalt::Group::group_set_quota($sitehash{"name"},
			$sitehash{"quota"});
		if ($rc !~ /^2999 /) {
			#
			# An error has occured setting this groups quota.  Post a
			# warning and collect the message. 
			#
			$msg = $0 . " " . scalar localtime(time) .
			    ": Failed to set quota for group " . $sitehash{name} .
			    " of " . $sitehash{quota} . " with reason: " . $rc . "\n";
			warn $msg;
			push @errors, ($msg);
		}
	}
}


	#
	# Now update the user quota.  First, get the list of users and
	# their quota values from the database.
	#
sub fix_user_quotas {

	my (@users, $user, %userhash);
	my ($rc, $msg);

	# Query the database for all user names and quotas
	@users = Cobalt::Meta::query('keys' => [ 'name', 'quota' ], 'type' => 'users');
	foreach $user (@users) {
		%userhash = %{$user};

		# Skip the default user: setting the quota will fail.
		next if ($userhash{"name"} eq "default");

		#
		# Adjust the quota information to be in blocks.  The
		# database stores quota information in megabytes, while the
		# routines to update the quota take absolute values.
		#
		$userhash{"quota"} *= 1024;

		# Update the users' quota
		$rc = Cobalt::User::user_mod($userhash{"name"}, 0, 0,
		    $userhash{"quota"}, 0, 0);
		if ($rc !~ /^2999 /) {
			#
			# An error has occured setting this users quota.  Post a
			# warning and collect the message. 
			#
			$msg = $0 . " " . scalar localtime(time) .
			    ": Failed to set quota for user " . $userhash{name} .
			    " of " . $userhash{quota} . " with reason: " . $rc . "\n";
			warn $msg;
			push @errors, ($msg);
		}
	}
}

($version, $product, $locale) = determine_features();
if (! $has_postgres) {
	# No failure, just nothing to do.
	exit 0;
}

#
# Load the appropriate modules.  These will only be available on machines with
# postgres, which is why they are loaded after checking for that feature.
#
require Cobalt::Group; 
require Cobalt::Meta;
require Cobalt::Vsite;
require Cobalt::Util;

# Clear out the error list
@errors = ();

# Fix the site quotas
fix_site_quotas();

# Fix the user quotas
fix_user_quotas();

if (scalar(@errors) > 0) {
	#
	# There were errors during processing, so print them to stdout now.
	# This allows the caller to capture stderr output to a log file and
	# process stdout text differently in the event of an error.
	#
	foreach $err (@errors) {
		print STDOUT "$err";
	}

	exit 1;
}

exit 0;

