/*
 *  Copyright 2014 Jonny Heggheim
 *
 *  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 com.jonnyware.timetracker;

import com.jonnyware.timetracker.cli.PrintCommand;
import com.jonnyware.timetracker.cli.TotalDiffCommand;
import org.apache.commons.cli.*;
import org.joda.time.DateTime;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws FileNotFoundException, ParseException {
        String fileName = System.getenv("JTIME_DEFAULT_FILE");
        Option f = OptionBuilder.withArgName("file").withType(String.class).withLongOpt("file").hasArgs(1).create('f');
        Options options = new Options().addOption(f);

        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp( "jtime", options );

        CommandLineParser p = new PosixParser();
        CommandLine cmd = p.parse(options, args);

        String command = "print";

        if(!cmd.getArgList().isEmpty()) {
            command = (String) cmd.getArgList().get(0);
        }

        if(cmd.hasOption("f")) {
            fileName = cmd.getOptionValue("f");
        }
        if(fileName == null) {
            System.err.println("File is not specified!");
        }
        System.out.println("Command: " + command);
        System.out.println("Filename: " + fileName);

        File file = new File(fileName);

        Map<String, Object> parsed = (Map<String, Object>) new Yaml().load(new FileInputStream(file));
        TimeEntryParser parser = new TimeEntryParser(parsed, DateTime.now());

        if(command.equals("print")) {
            new PrintCommand().run(parsed, parser);
        } else if(command.equals("total-diff")) {
            new TotalDiffCommand().run(parsed, parser);
        } else {
            System.err.println("Unknown command: '" + command + "'");
        }
    }
}

@@ -1068,71 +1068,8 @@
 n %7B%0A
-        String fileName = System.getenv(%22JTIME_DEFAULT_FILE%22);%0A
     
@@ -1202,150 +1202,179 @@
 tion
-s options = new Options().addOption(f);%0A%0A        HelpFormatter formatter = new HelpFormatter();%0A        formatter.printHelp( %22jtime%22, o
+ h = OptionBuilder.withArgName(%22help%22).withLongOpt(%22help%22).create('h');%0A%0A        Options options = new Options();%0A        options.addOption(f);%0A        options.addO
 ption
-s 
+(h
 );%0A%0A
@@ -1469,24 +1469,200 @@
 ns, args);%0A%0A
+        if (cmd.hasOption(%22h%22)) %7B%0A            HelpFormatter formatter = new HelpFormatter();%0A            formatter.printHelp(%22jtime%22, options);%0A            return;%0A        %7D%0A%0A%0A
         Stri
@@ -1694,16 +1694,17 @@
       if
+ 
 (!cmd.ge
@@ -1802,18 +1802,82 @@
         
-if
+String fileName = System.getenv(%22JTIME_DEFAULT_FILE%22);%0A        if 
 (cmd.has
@@ -1959,16 +1959,17 @@
       if
+ 
 (fileNam
@@ -2387,16 +2387,17 @@
       if
+ 
 (command
@@ -2485,16 +2485,17 @@
  else if
+ 
 (command

Include the --help argument.