#!/usr/bin/eperl -w
###
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# Exit codes (on failure error message goes to stderr):
#  0 - success
#  1 - failure
# 
# Boolean arguments on the command line are given as 0 or 1. All the
# command line arguments are encoded to avoid problems with escapes.
#
# All functions defined here either return an error message if an
# error occured and "" if everything went well or allways return a
# valid value, but exit (with code 1) printing an error message if an
# error occurs. This second type of functions have _e appended to
# their name. Functions are allowed to print results onto stdout, but
# errors are printed only in the main program.
# ----------------------------------------------------------------------
# Usage: wp_enable_quota <directory_name>
# 
# This script would take a single argument, directory name, and make 
# sure that quota option is enabled on the devices that this directory
# lives. It would print out the mount point to STDOUT
# ----------------------------------------------------------------------

use strict;

use File::Copy;
use POSIX qw (uname tmpnam);

use constant MTAB_FILE => "/etc/mtab";
use constant FSTAB_FILE => "/etc/fstab";

###
# This method would return the mount point for the given path.
###
sub getMountPoint ($) {
    my $path = shift;

    # find the mount point for the path using /mnt/mtab
    open MOUNT_LIST, MTAB_FILE;

    my $buf;
    my %mountPoints = ();
    while ($buf = <MOUNT_LIST>) {
        my @fsInfo = split /\s/, $buf;

        # gather the mount points
        $mountPoints{$fsInfo[1]} = $fsInfo[3];
    }
    close MOUNT_LIST;

    # now, we need to do the longest prefix match
    my $matched = '';
    foreach my $mp (keys %mountPoints) {
        if (($path =~ /^$mp/) &&
            (length($mp) > length($matched))) {
            $matched = $mp;
        }
    }

    my %devProp = ();

    map { $devProp{$_} = 1; } 
    split /,/, $mountPoints{$matched};

    return ($matched, \%devProp);
}

sub addQuotaToTab ($$$) {
    my $mountPoint = shift;
    my $filein     = shift;
    my $sep        = shift;

    my $tmpfile = POSIX::tmpnam;
    open MTAB_R, $filein;
    open MTAB_W, ">$tmpfile";

    my $buf;
    my $fileChanged = 0;
    while ($buf = <MTAB_R>) {
        if ($buf =~ /^\S+\s+$mountPoint\s+/ &&
            $buf !~ /^#/) {
            my @line = split /\s+/, $buf;

            # parse the property
            my %devProp = ();
            
            map { $devProp{$_} = 1; } 
            split /,/, $line[3];


            if (!$devProp{usrquota})  {
                # add usrquota
                $fileChanged = 1;
                $line[3] .= ",usrquota";
            }

            if (!$devProp{grpquota}) {
                # add grpquota
                $fileChanged = 1;
                $line[3] .= ",grpquota";
            }
            
            $buf = join "$sep", @line;
            $buf .= "\n";
        }

        print MTAB_W $buf;
    }
    
    close MTAB_W;
    close MTAB_R;

    # copy the file over if there's change
    if ($fileChanged) {
        print STDERR "updating $filein\n";
        copy $tmpfile, $filein;
    }
}

my $usage = "set_tab_quota <directory name>\n";

my $dir = shift || die "$usage";

# find out what is mount point of /home
my ($mountPoint, $devProp) = getMountPoint ($dir);
print $mountPoint;

# check if quota is eabled, if not edit /etc/mtab
if (!($devProp->{usrquota} && $devProp->{grpquota})) {
    addQuotaToTab ($mountPoint, MTAB_FILE, ' ');
}

# check if quota is listed in /etc/fstab, add if not
addQuotaToTab ($mountPoint, FSTAB_FILE, "\t");
