#Backfill-Script v2.02

var dest_sites       = site1;site2
var secs_to_backfill = ebook;mp3;0day;divx;x264;xxx;pda;games;dox;utils
var source_sites_to_skip = badsite1;badsite2

var source_sites_to_skip_local = nodlsite1;nodlsite2

var skip_dirs = *NUKED*

#days to backfill...either by commandline or if none then we use 5
var days_to_backfill = $paramstr(1)
if $days_to_backfill = 
  var days_to_backfill = 5
end

var tmp = $paramstr(2)
if $tmp != 
  var secs_to_backfill = $tmp
end

var tmp = $paramstr(3)
if $tmp != 
  var dest_sites = $tmp
end


######################################################################################
var $days_to_backfill_chk=$days_to_backfill+1

for dname in $dest_sites

  select $dname
  if $dname.connected = false
    connect $dname
  end
  if $dname.connected = false
    writeln couldn't connect to destination site
    goto ende
  end


    var source_sites = ACTIVESITELIST SPEED $dname

    for sec in $secs_to_backfill


      for sname in $source_sites
  
        if $sname = $dname
          goto site_done
        end

        if $sname = $source_sites_to_skip
          goto site_done
        end
        if $dname = int_local
          if $sname = $source_sites_to_skip_local
            goto site_done
          end
        end


        var i=0
        :backfill_start


        var dest_dirs=$dname.sectionpath($sec,$i)
        if $dest_dirs =
          goto site_done
        end

        var source_dirs=$sname.sectionpath($sec,$i)
        if $source_dirs =
          goto site_done
        end

        var sdir_temp=$sname.sectionpath($sec,455)

        for ddir in $dest_dirs

          var parsed_pathes=
          var dir_count=0
          for sdir in $source_dirs

            #did we have this one parsed already?
            if $sdir = $parsed_pathes
              #writeln [-] already parsed $sdir ($parsed_pathes)
              goto dir_done
            end

            if $parsed_pathes<>
              var parsed_pathes=$parsed_pathes|
            end
            var parsed_pathes=$parsed_pathes$sdir
          

            #create check-dir
            var dir_count=$dir_count+1
            var sdir_chk=GETPART $sdir_temp $dir_count ";"
  
            #check if its a dated dir or not....
            var dated_dir=1
            if $sdir_chk=$sdir
              var dated_dir=0
            end

            #connect + cwd + refresh
            select $dname
            cwd $ddir
            if $dname.ok = false
              goto dir_done
            end
            refresh

            select $sname
            if $sname.connected = false
              connect $sname
            end
            if $sname.connected = false
              goto dir_done
            end
  
            cwd $sdir
            if $sname.ok = false
              goto dir_done
            end

            refresh
            compare $dname
            if $dated_dir=0
              LIMITSELECTION DAY-$days_to_backfill
            end
            if $dated_dir=1
              if $sec=ebook
                LIMITSELECTION DAY-$days_to_backfill_chk
              end
              if $sec=pda
                LIMITSELECTION DAY-$days_to_backfill_chk
              end
            end

            FILTER_WITH_SITERULE $dname $sec

            var que=SELECTION

            var beauty_i=$i+1
            writeln [$sec] %bold$sname:%bold$sdir >> %bold$dname:%bold$ddir
            writeln [$sec] Dated: $dated_dir, Day-X: $beauty_i/$days_to_backfill
           

            for item in $que
              if $item <> $skip_dirs
                if $dname = int_local
                  TRANSFER_WITH_SITERULE $sname$sdir/$item $ddir/$item $sec
                end
                if $dname <> int_local
		  if $dated_dir=1
		    select $dname
	 	    var dupe_rls=FTPCMD site dupe $item
		    select $sname
		    if $dupe_rls=*0 of 0 dupes listed*
                      TRANSFER_WITH_SITERULE $sname$sdir/$item $dname$ddir/$item $sec
  		    end
		  end
		  if $dated_dir!=1
		    TRANSFER_WITH_SITERULE $sname$sdir/$item $dname$ddir/$item $sec
		  end
                end
              end
            end

            :dir_done

          end
        end

        if $source_dirs!=$sdir_temp
          var dated_dir=1
        end

        if $dated_dir=1
          var i=$i+1
          if $i != $days_to_backfill
            goto backfill_start
          end
        end
        :site_done

      end
      :section_done
    end
  end
end