#!/bin/sh
# Set permissions on ini files under $INITREE
#
if [ -d "$INITREE" ]; then
   chmod g+rx "$INITREE"
   chown root:hpsmh "$INITREE"
   fn=/tmp/files.tmp
   rm -f "$fn"
   /usr/bin/find "$INITREE" -name '*.ini' -print >> "$fn"
   # Did we find any files?
   l_c=$(/usr/bin/wc -l $fn | awk '{print $1}')
   if [[ $l_c != 0 ]]; then     
     count=0;
     for rl in $(seq 0 $l_c) 
     do
       read rline;
       thefile=${rline:0}
#       echo "$thefile"
       if [ -f "$thefile" ]; then
         chgrp hpsmh "$thefile"
         chmod g+r "$thefile"
         inifiles[$count]=$thefile
         count=`expr $count + 1`
       fi
     done <$fn
     #
     rm -f $fn
     element_count=${#inifiles[*]}
     #echo $element_count
     for ac in $(seq 0 $element_count)
     do
       if [[ ${#inifiles[ac]} != 0 ]]; then
#        echo "Reading " ${inifiles[ac]}
         echo -n '.'
         thefile=${inifiles[ac]}
         if [ ! -f "$thefile" ]; then
            echo ""
         else
          l_c=$(/usr/bin/wc -l $thefile | awk '{print $1}')
          for rl in $(seq 0 $l_c)
          do
           read -r iline;
           iline=$(echo $iline | sed "s/
$//g")
           iline=$(echo $iline | sed 's/\\/\//g')
           iline=$(echo $iline | sed 's/ /\\n/g')
           echo "$iline" | grep -i -E '^NavigationFile[     ]*=|^HelpContents[     ]*=|^HelpSearch[     ]*=|^HelpIndex[     ]*=|^HelpMap[       ]*=' >/dev/null 2>&1
           if [[ $? == 0 ]]; then
             url=`echo $iline | sed 's/^[^=]*=[    ]*//'`
             dname=`dirname ${inifiles[ac]}`
             fname=`basename $dname/$url`
             /usr/bin/find "$dname" -name "$fname" -print >> $fn       
           else
              # Create an empty
              touch $fn
           fi
          done <$thefile 
         fi 
       fi 
     done
     unset inifiles[*]
     l_c=$(/usr/bin/wc -l $fn | awk '{print $1}')
     for rl in $(seq 0 $l_c)
     do
       echo -n '.'
       read rline;
       if [[ $rline != '' ]]; then
         chgrp hpsmh "$rline"
         chmod g+r "$rline"
         dtree=`dirname $rline`
         while [[ $dtree != "$INITREE" && $dtree != '/opt' ]]
         do
#             echo "dtree=$dtree"
             chgrp hpsmh $dtree
             chmod g+x $dtree
             dtree=`dirname $dtree`
         done
       fi
     done <$fn
   fi
fi
