import java.io.*;
import java.util.concurrent.*;


/**
 * A utility application that repeatedly saves a snapshot of the files in the
 * current directory.
 *
 * This version runs for a specific amount if time; it does not have
 * a UI that can be used to shut it down.

 * @author  Prof. David Bernstein, James Madison University
 * @version 4
 */
public class AutoDir
{
    /**
     * The entry point of the application.
     *
     * args[0] contains the delay between snapshots.
     *
     * @param args The command-line arguments.
     */
    public static void main(String[] args) throws IOException
    {
        long        delay, duration;
        
        // Process the command-line arguments
        delay    =   60000;  // Once per minute
        duration = 3600000;  // For an hour
        
        if ((args != null) && (args.length > 1))
        {
            delay    = Long.parseLong(args[0]);
            duration = Long.parseLong(args[1]);
        }
        
        // Construct the scheduler
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        // Construct and schedule the DirectoryLister
        Future<?> task;
        task = scheduler.scheduleAtFixedRate(new DirectoryLister(), 0, delay,
                                             TimeUnit.MILLISECONDS);
        
        // Construct and schedule the Terminator
        scheduler.schedule(new Terminator(task, scheduler), 
                           duration, TimeUnit.MILLISECONDS);
    }
}
