#!/bin/ksh
###########################################################################
#  Title : file_check.sh 
#
#  Description: It will check the Informix file distribution list against 
#               actual files to verify that all have the correct ownerships 
#               and permissions.
#
#  Note  : The script will create two output files in the current directory.
#           1) file_check.sh.out  :   Listing of all files have problem with 
#                                     permission, ownership or missing. 
#           2) file_check.sh.update : A script file with UNIX commands to fix 
#                                     the permission or ownership problem. 
#                                     Execute the "file_check.sh.update" to 
#                                     fix the problem.
#
#  Usage : ./file_check.sh -f filename [-d directory]
#               -f     name of file distribution list (required)
#                                   example:
#                                   onlinefiles in 5.x
#                                   IDSfiles in 7.x
#                                   IIFfiles in 9.x, 10.x & 11.x
#
#               -d     the directory where Informix product installed
#                      (default INFORMIXDIR environment variable will use)
#
#
###########################################################################

#set +x
usage(){
$ECHO "\nUSAGE:\t$1 -f filename [-d directory]\n\n";
$ECHO "-f   name of the file distribution list (required)";              
$ECHO "          example:";              
$ECHO "              onlinefiles in 5.x";              
$ECHO "              IDSfiles in 7.x";              
$ECHO "              IIFfiles in 9.x, 10.x & 11.x\n";              
$ECHO "-d   the directory where Informix product installed" 
$ECHO "     (default INFORMIXDIR environment variable will use)\n";
cleanup;
}

# Check files on disk #
files_on_disk() {
  file=$1
  owner=$2
  group=$3
  permission=$4
  real_owner=`ls -ld $1|$AWK '{print $3}'`
  real_group=`ls -ld $1|$AWK '{print $4}'`
  filemode=`ls -ld $1|$AWK '{print $1}'|sed 's/^d/-/'`
 
  ## Check Owner
  if [ $owner != $real_owner ];then
     $ECHO "\t$file" >> bad_owner.$$
     $ECHO "chown $owner $file" >> $UPDATE_FILE
  fi

  ## Check Group
  if [ $group != $real_group ];then
     $ECHO "\t$file" >> bad_group.$$
     $ECHO "chgrp $group $file" >> $UPDATE_FILE
  fi

  ## Check Permission
  real_permission=`file_mode $file`
  if [ $permission -ne $real_permission ];then
     $ECHO "\t$file" >> bad_permission.$$
     $ECHO "chmod $permission $file" >> $UPDATE_FILE
  fi
}


# File Permission Definition #
file_mode(){
ls -ld $1 | $AWK '{
     c1=substr($1,1,1)
     c2=substr($1,2,1)
     c3=substr($1,3,1)
     c4=substr($1,4,1)
     c5=substr($1,5,1)
     c6=substr($1,6,1)
     c7=substr($1,7,1)
     c8=substr($1,8,1)
     c9=substr($1,9,1)
     c0=substr($1,10,1)
    
     up=0
     gp=0
     op=0
     sp=0
    
     if (c2=="r") {up+=4}
     if (c3=="w") {up+=2}
     if (c4=="x") {up+=1}
     if (c4=="s" || c4=="S") {sp+=4
               up+=1}
     if (c5=="r") {gp+=4}
     if (c6=="w") {gp+=2}
     if (c7=="x") {gp+=1}
     if (c7=="s" || c7=="S") {sp+=2
               gp+=1}
     if (c8=="r") {op+=4}
     if (c9=="w") {op+=2}
     if (c0=="x") {op+=1}
     if (c0=="t" || c0=="T") {sp+=1
               op+=1}
     printf("%1d%1d%1d%1d\n", sp, up, gp, op) }'
}

cleanup(){
if [ -f file1.$$ ];then
 rm file*.$$
fi

if [ -f bad_owner.$$ ];then
 rm bad_*.$$
fi
}

# Main Program #
osname=`uname -s`
if [ $osname = "SunOS" ]; then
   AWK=nawk
else
   AWK=awk
fi

if [  $osname = "Linux" ]; then
   ECHO='echo -e'
else
   ECHO=echo
fi

INFORMIXDIR=`env|egrep INFORMIXDIR|$AWK -F\= '{print $2}'`
OUTPUT_FILE=$0.out
UPDATE_FILE=$0.update
>$OUTPUT_FILE
>$UPDATE_FILE;chmod 777 $UPDATE_FILE
$ECHO "\n\nMissing File(s)">bad_file.$$
$ECHO "\n\nWrong Owner (Execute $UPDATE_FILE for fix)">bad_owner.$$
$ECHO "\n\nWrong Group (Execute $UPDATE_FILE for fix)">bad_group.$$
$ECHO "\n\nWrong File Permission (Execute $UPDATE_FILE for fix)"\
     >bad_permission.$$

##Parse the arguments
set -- `getopt f:d: $*`
if [ $? -ne 0 ];then
   usage `basename $0`
   exit 2
fi

if [ $# -eq 1 ] || [ $1 != "-f" -a $1 != "-d" ];then 
   usage `basename $0`
   exit 2
fi

for i in $*
do
  case $i in 
   -d) informixdir=$2
       shift 2;;
   -f) listfile=$2
       shift 2;;
   --) shift;break;usage `basename $0`;;
  esac
done

# Do some checking

if [ ! -n "$informixdir" ];then
   informixdir=`env|egrep INFORMIXDIR|$AWK -F\= '{print $2}'`
fi

if [ ! -d $informixdir ];then
   $ECHO "Invalid Directory: $informixdir"
   exit 1
fi

if [ ! -r $informixdir/etc/$listfile ];then
   $ECHO "Invalid File: $informixdir/etc/$listfile"
   exit 1
fi

$AWK '{if ((substr($0,1,1) != "#") && (substr($0,1,1) != " ") && \
          (length > 1) && ($1 != "PRODUCT") && ($1 != "NAME")) \
       print infxdir"/"$1"|"$2"|"$3"|"$4}' \
          infxdir=$informixdir $informixdir/etc/$listfile > file1.$$

for line in `cat file1.$$`
do
  file=`$ECHO $line | $AWK -F\| '{print $1}'`
  owner=`$ECHO $line | $AWK -F\| '{print $2}'`
  group=`$ECHO $line | $AWK -F\| '{print $3}'`
  permission=`$ECHO $line | $AWK -F\| '{print $4}'`

  ## Checking all files are exist 
  if [ ! -r $file ];then
     $ECHO "\t$file" >> bad_file.$$
  else
     files_on_disk $file $owner $group $permission
  fi
done

for file in `ls bad_file.$$ bad_owner.$$ bad_group.$$ bad_permission.$$`
do
  if [ `wc -l $file|$AWK '{print $1}'` -gt 3 ];then
    cat $file >> $OUTPUT_FILE
  else
    cat $file >> $OUTPUT_FILE
    $ECHO "\tNo File Found" >> $OUTPUT_FILE
  fi
done
cleanup

# End of the script
