import java.io.*;
import java.util.concurrent.*;


/**
 * A utility application that repeatedly saves a snapshot of the files in the
 * current directory.
 *
 * This version uses a DirectoryLister and a ScheduledExecutorService.

 * @author  Prof. David Bernstein, James Madison University
 * @version 2
 */
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;
        
        // Process the command-line arguments
        delay = 1000;
        if ((args != null) && (args.length > 0))
        {
            delay = Long.parseLong(args[0]);
        }
        
        // Construct and start the DirectoryLister
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(new DirectoryLister(), 0, delay,
                                      TimeUnit.MILLISECONDS);
        
        // The command-line "user interface" (such as it is)
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Press [Enter] to stop.");
        in.readLine();

        scheduler.shutdownNow();
    }
}
