# process a directory set file.
# set name in $setname.
# output files:  dirlist  and  findtests.

#process .set file.


grep -v "^ *$" /etc/tbackup/$setname.set | grep -v "^ *#" | \
gawk "NF == 1"  >dirlist

if [ ! -s dirlist ]; then
 echo "Error in /etc/tbackup/$setname.set: no directories specified."
 doabort
fi

if [ "`grep -v \"^ */\" dirlist`" != "" ]; then
 echo "Error in /etc/tbackup/$setname.set: all directory names must start with a / ."
 doabort
fi

#get setroot, if any.
gawk -f- /etc/tbackup/$setname.set >findtests <<-'WUXTA'
($1 == "setroot") \
  { if((NF!=2)||(sr++!=0)) { print "\nError in line",NR,":",$0; next; }
    if(($2!="/")&&(substr($2,length($2))=="/"))
             { print "\nError in line",NR,":",$0" : trailing / on setroot";
               next;
             }
    print $2 >"setroot";
    next;
  }
($1 == "incmethod") \
  { 
   print "\nError in line",NR,":",$0,": incmethod function no longer supported"
#  if((NF!=2)||(im++!=0)) { print "\nError in line",NR,":",$0; next; }
#    print $2 >"incmethod";
    next;
  }
WUXTA

if [ "`grep \"Error in\" findtests`" != "" ]; then
 echo "Errors in /etc/tbackup/$setname.set:"
 grep "Error in" findtests
 doabort
fi

#handle setroot 
export packhome=/
if [ -s setroot ]; then
 packhome=`cat setroot`
 rm setroot
 if [ ! -d $packhome ]; then
  echo "Error in /etc/tbackup/$setname.set: set root $packhome does not"
  echo "exist or is not a directory."
  doabort
 fi
fi
dea_rpars packhome

#handle incmethod
export incmethod=ctime
#if [ -s incmethod ]; then
# incmethod=`cat incmethod`
# rm incmethod
# if [ ! -f $bupbin/$incmethod\.ix ]; then
#  echo "Error in /etc/tbackup/$setname.set: the incmethod '$incmethod' does not exist."
#  doabort
# fi
#fi
dea_rpars incmethod



if [ $packhome != "/" ]; then
#snip leading / off file/dirnames in set.
 mv dirlist dirlist.org
 gawk '{ print substr($0,2); }' <dirlist.org >dirlist
 rm dirlist.org
fi

#check if directories/files in set exist
err=no
set -f
for i in `cat dirlist`; do
 if [ $packhome != "/" ]; then
  i=$packhome/$i
 fi
 if [ ! -e $i ]; then err=yes; 
  echo "Error in /etc/tbackup/$setname.set: file or directory $i not present.";
 fi
done
set +f

if [ $err = yes ]; then 
 doabort
fi

#handle not and findtest arguments
gawk -f- packhome=$packhome /etc/tbackup/$setname.set >findtests <<-'WUXTA'
($1 == "not") && ($2!="findtest") \
   { rs=0;
     if ($2=="name") { f="! -name '%s'"; } else
     if ($2=="fullname") { f="! -path '%s'"; rs=1; } else
     if ($2=="ext") { f="! -name '*%s'" } else
     if ($2=="dir") { f="! -path '%s/*'"; rs=1;
                      for(i=3; i<=NF; i++) 
                         printf f"\n",$i >>"optitests";
	              f2="! -path '%s'";
                      for(i=3; i<=NF; i++) 
                         printf f2"\n",$i >>"optitestsdir";
                    } else
      { print "\nError in line",NR,":",$0; next; }

     if ($3=="") { print "\nError in line",NR,": ",$0; next; }

     if((rs==1) && (packhome != "/"))
       for(i=3; i<=NF; i++) printf f"\n",substr($i,2);
     else
       for(i=3; i<=NF; i++) printf f"\n",$i;
     next;
    }
(($1 == "not") && ($2=="findtest")) || ($1 == "findtest") \
   {
    a=substr($0,index($0,"findtest")+9);
    if($1=="not") printf "! %s\n",a;
             else printf "%s\n",a;
    next;
   }
($1 == "setroot") { next; }
($1 == "incmethod") { next; }
(NF==0) { next; }
(NF==1) { next; }
(NF >= 1) && ( substr($1,1,1) == "#" ) { next; }
{ print "\nError in line",NR,": ",$0; }
WUXTA

if [ "`grep \"Error in\" findtests`" != "" ]; then
 echo "Syntax errors in /etc/tbackup/$setname.set:"
 grep "Error in" findtests
 doabort
fi

#optimize if packhome=/ and dirlist=/.

if [ $packhome = "/" -a "`cat dirlist`" = "/" ]; then

#find all non-excluded files and dirs under /
 eval find `cat dirlist` -mindepth 1 -maxdepth 1 `cat optitests optitestsdir` \
  >dirlist2

#and make them the dirlist.  This way, unwanted filesystems under /
#are not traversed by find.

 if [ -s dirlist2 ]; then
#dirlist2 not empty
  rm dirlist
  mv dirlist2 dirlist
 fi

fi
#optimize


