Listing 2
###################################################################
#                     PrnQt_lib.sh                                #
###################################################################
# This library provides functions that are needed to use          #
# the print quota.                                                #
#                                                                 #
#                                                                 #
# NOTE:                                                           #
#   The following variables must be defined before this           #
#   script is evoked.                                             #
#        $user                                                     #
#        $printer                                                  #
# --------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or   #
# modify it under the terms of the GNU General Public License     #
# as published by the Free Software Foundation; either version 2  #
# of the License, or (at your option) any later version.          #
#                                                                 #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of  #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   #
# GNU General Public License for more details.                    #
#                                                                 #
# You should have received a copy of the GNU General Public       #
# License along with this program; if not, write to the Free      #
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, #
# MA  02111-1307, USA.                                            #
###################################################################
############################
# Definitions of Variables #
############################ 
UserPrnQtFile=/PrnQt/usrinfo/$user
GenPrnQtFile=/PrnQt/prninfo/quota
GenQt=`cat $GenPrnQtFile`
PrnterListFile=/PrnQt/prninfo/printerlist
NumOfPrinter=`cat $PrnterListFile|wc -l`
UpdateUserQtFile=/PrnQt/bin/updatePrnQt
UserLeftQt=/PrnQt/usercredit/$user

###############
#  Functions  #
###############
#
# Usermatch() checks if user is a valid user who is allowed to print
#
Usermatch() {
nismatch $lowername passwd.org_dir
if [ $? -ne 0 -o $lowername = "root" ]
then 
  exit 0
fi
}
#
# InitUserPrnQtFile() initializes user's print quota database (UPQdB)
#
InitUserPrnQtFile() {
  if [ -r $UserLeftQt ]
  then 
    quotaleft=`cat $UserLeftQt`
    totalquota=`expr $quotaleft + $GenQt`
    TmpUserQt=`echo "$totalquota:0:0"`
    rm $UserLeftQt
  else  
    TmpUserQt=`echo "$GenQt:0:0"`
  fi  
  echo $NumOfPrinter > /tmp/debug
  while [ $NumOfPrinter -gt 0 ]
  do
    TmpUserQt=`echo "$TmpUserQt:0"`
    NumOfPrinter=`expr $NumOfPrinter - 1`
  done
  PRNQINFO=$TmpUserQt
  PRNQFILE=$UserPrnQtFile
  export PRNQINFO
  export PRNQFILE
  $UpdateUserQtFile
}
#
# ChkPrnQt() checks if there is a new printer and
#                   if user is overquota or not
#
ChkPrnQt() {
  ## Check if there is new printers in printer list file (PLF)
  field=`awk -F: '{print NF}' $UserPrnQtFile`
  NumOfPrinter=`expr $NumOfPrinter + 3 `
  FieldNumComp=`expr $NumOfPrinter - $field`
  TmpUserQt=`cat $UserPrnQtFile`
  while [ $FieldNumComp -gt 0 ]
  do
      TmpUserQt=`echo "$TmpUserQt:0"`
      FieldNumComp=`expr $FieldNumComp - 1`
  done   
  ## Check the Print Quota
  PrnQtLeft=`awk  -F: '{printf ("%d\n",$1-$2)}' $UserPrnQtFile`
  if [ $PrnQtLeft -lt 0 ]
  then
    /usr/bin/mail -s " Over printing quota notice." $user < /dev/null
    exit 99
  fi
}
#
# UpdatePrnQtFile() updates user print quota in UPQdB
#                   takes two arguments
#                   the first argument is actual pages printed
#                   the second argument is the deduction
# 
UpdatePrnQtFile() {
  RealPages=$1
  Multiple=$2
  ## Find the field to be updated
  FullLineInfo=`cat $PrnterListFile|nl -v4|grep $printer`
  FiledNum=`echo $FullLineInfo|cut -d" " -f1`
  NumOfPrinter=`expr $NumOfPrinter + 3`
  UserPrnQtInfo=`cat $UserPrnQtFile`
  UserPrnQtUpdateInfo="$UserPrnQtInfo:$FiledNum:$RealPages:$Multiple"
  UserPrnQtNewInfo=`echo "$UserPrnQtUpdateInfo"|awk -F: '{ i=NF; j=NF-1; \
                            k=NF-2; \
                            $($k) = $($k) + $j; \
                            $2=$2+$j*$i;$3=$3+$j;\
                            arr=$1; for(i=2;i<=NF-3;i++) arr=arr ":" $i; \
                            print arr }'`
  PRNQINFO=$UserPrnQtNewInfo
  PRNQFILE=$UserPrnQtFile
  export PRNQINFO
  export PRNQFILE
  $UpdateUserQtFile
}
