/*
 * Copyright 2014-2018 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.aeron.cluster;

import io.aeron.cluster.codecs.CncHeaderDecoder;

import java.io.File;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class ClusterTool
{
    private static final long TIMEOUT_MS = TimeUnit.SECONDS.toMillis(5);

    private static File clusterDir;

    public static void main(final String[] args)
    {
        if (args.length == 0 || args.length > 3)
        {
            printHelp();
            System.exit(-1);
        }

        clusterDir = new File(args[0]);
        if (!clusterDir.exists())
        {
            System.err.println("ERR: cluster folder not found: " + clusterDir.getAbsolutePath());
            printHelp();
            System.exit(-1);
        }

        if (args.length == 2 && args[1].equals("describe"))
        {
            try (ClusterCncFile cncFile = openCncFile(System.out::println))
            {
                final CncHeaderDecoder decoder = cncFile.decoder();

                printActivityTimestamp(decoder.activityTimestamp());
                System.out.println(decoder);
            }
        }
        else if (args.length == 2 && args[1].equals("pid"))
        {
            try (ClusterCncFile cncFile = openCncFile(null))
            {
                System.out.println(cncFile.decoder().pid());
            }
        }
    }

    private static ClusterCncFile openCncFile(final Consumer<String> logger)
    {
        return new ClusterCncFile(clusterDir, ClusterCncFile.FILENAME, System::currentTimeMillis, TIMEOUT_MS, logger);
    }

    private static void printActivityTimestamp(final long activityTimestamp)
    {
        System.out.format(
            "%1$tH:%1$tM:%1$tS (Catalog: %2$tH:%2$tM:%2$tS)%n", new Date(), new Date(activityTimestamp));
    }

    private static void printHelp()
    {
        System.out.println("Usage: <cluster-dir> <command> <optional args>");
        System.out.println("  describe: prints out all descriptors in the file. Optionally specify a recording id" +
            " to describe a single recording.");
        System.out.println("  pid: prints just PID of cluster.");
    }
}

@@ -668,16 +668,56 @@
 Decoder;
+%0Aimport io.aeron.cluster.codecs.CncType;
 %0A%0Aimport
@@ -1639,24 +1639,31 @@
        print
+TypeAnd
 ActivityTime
@@ -1668,16 +1668,36 @@
 mestamp(
+decoder.fileType(), 
 decoder.
@@ -2262,16 +2262,23 @@
 id print
+TypeAnd
 Activity
@@ -2289,24 +2289,47 @@
 stamp(final 
+CncType cncType, final 
 long activit
@@ -2338,32 +2338,78 @@
 imestamp)%0A    %7B%0A
+        System.out.print(%22Type: %22 + cncType);%0A
         System.o
@@ -2432,16 +2432,17 @@
        %22
+ 
 %251$tH:%251
@@ -2456,15 +2456,16 @@
 tS (
-Catalog
+activity
 : %252

clean up basic output.