#!/bin/sh
# Shell to  scan used config values at $STTA *.cgi *.pl and *.sh
# Info is dumped in same structure as "dataconf" same file name
# NOTUSED file is created with 

orderlistfile=`echo $STTA/dataconf/ordercfgs`
dataconfdir=`echo $STTA/dataconf`
webmindataconf=/etc/webmin/stta/dataconf
notusedfile=NOTUSED

echo "WorkingDir will be `pwd`: "
read wkdir
[ $wkdir ] && [ ! -d $wkdir ] && mkdir $wkdir
[ $wkdir ] && [ -d $wkdir ] && cd $wkdir
[ $wkdir ] && echo "WorkingDir will be $wkdir "
read tec

pathdir=`pwd`
if [ "$pathdir" = "$dataconfdir" -o  "$pathdir" = "$webmindataconf"  ] ; then
    echo "ERROR  $patdir should not be used or will be overwrited !!! "
    exit 2
fi 

nowTime=`date +%H:%M:%S`
nowDate=`date +%d-%m-%y`
echo "Scann at  $nowDate - $nowTime ">$notusedfile
echo "">>$notusedfile
for linvar in ` cat $dataconfdir/ordercfgs | awk -F"=" '{ print $1 }'` 
do
  file=`echo $linvar | sed "s/ //g" `
  [ -n $file ] && {
     echo "Scann at  $nowDate - $nowTime ">$file
     echo "">>$file
     for lin in `cat $dataconfdir/$file | awk -F"=" '{ print $1 }' 2>/dev/null` 
     do 
        nomvar=`echo $lin | sed "s/ //g" `
        [ $nomvar ] && {
           fgrep -l $nomvar $STTA/*.cgi >l.cgi 
           fgrep -l $nomvar $STTA/*.pl >l.pl 
           fgrep -l $nomvar $STTA/*.sh >l.sh 
           cgi=`cat l.cgi | wc -l | sed "s/ //g" 2>/dev/null`
           pl=`cat l.pl | wc -l | sed "s/ //g" 2>/dev/null`
           sh=`cat l.sh | wc -l | sed "s/ //g" 2>/dev/null`
           echo "$nomvar: cgi($cgi) - pl($pl) - sh($sh) " >>$file 
        }
    done
    fgrep "cgi(0) - pl(0) - sh(0)" $file >>$notusedfile
    echo "$file done !!"
 }
done
rm l.cgi l.pl l.sh
