/**
* Hercule
* A simple markdown transclusion tool
* Author: James Ramsay
*/

import fs from 'fs';
import path from 'path';
import meow from 'meow';
import { TranscludeStream } from './hercule';

const cli = meow(
  [
    'Usage:',
    '  $ hercule [<input> ...]',
    '',
    'Options:',
    '  --stdin                    Specifies input to be read from stdin.',
    '  --output, -o path          Specifies the name and location of the output file.  If not specified, stdout is used.',
    '  --syntax, -s syntax_name   Specifies which transclusion link syntax (e.g. hercule, aglio, marked, multimarkdown).',
    '                             If not specifed, hercule is used.',
    '  --relative, -r path        Specifies the path to which links in input are relative',
    '  --sourcemap, -m            Specifies a sourcemap should be gnerated. Only used if output is specified.',
    '',
    'Examples:',
    '  $ hercule foo.md',
    '    Processes the file foo.md and prints to stdout',
    '  $ cat foo.md | hercule - --output bar.md',
    '    Processes the input from stdin and writes output to bar.md',
  ],
  {
    string: ['_', 'output', 'syntax', 'relative'],
    boolean: ['sourcemap', 'stdin'],
    default: {
      syntax: 'hercule',
      relative: '',
    },
    alias: {
      o: 'output',
      s: 'syntax',
      r: 'relative',
      m: 'sourcemap',
      h: 'help',
    },
  }
);

if (cli.input.length === 0 && !cli.flags.stdin) {
  process.stderr.write('\nNo input specified.\n');
  cli.showHelp();
}

let inputStream;
let source;
const options = { transclusionSyntax: cli.flags.syntax };
const input = cli.input[0];

if (input) {
  // Reading input from file
  source = path.normalize(input);
  inputStream = fs.createReadStream(source, { encoding: 'utf8' });
} else {
  // Reading from stdin
  source = path.join(cli.flags.relative, 'stdin');
  inputStream = process.stdin;
}

const outputStream = cli.flags.output
  ? fs.createWriteStream(cli.flags.output, { encoding: 'utf8' })
  : process.stdout;
options.outputFile = cli.flags.output || 'stdout';

const transclude = new TranscludeStream(source, options);

inputStream.on('error', err => {
  process.stderr.write(`\n\n${err.message} (${err.path})\n`);
  process.exit(1);
});

transclude.on('error', err => {
  process.stderr.write(`\n\nERROR: ${err.message} (${err.path})\n`);
  process.exit(1);
});

transclude.on('sourcemap', sourcemap => {
  const sourcemapFilepath = `${cli.flags.output}.map`;
  if (cli.flags.sourcemap && cli.flags.output)
    fs.writeFileSync(sourcemapFilepath, `${JSON.stringify(sourcemap)}\n`);
});

inputStream.pipe(transclude).pipe(outputStream);

@@ -214,29 +214,16 @@
 (%0A  
-%5B%0A    '
+%60
 Usage:
-',
 %0A
-    '
   $ 
@@ -247,40 +247,19 @@
 ...%5D
-',%0A    '',%0A    'Options:',%0A    '
+%0A%0AOptions:%0A
   --
@@ -321,24 +321,17 @@
 m stdin.
-',
 %0A
-    '
   --outp
@@ -433,32 +433,25 @@
 out is used.
-',
 %0A
-    '
   --syntax, 
@@ -553,24 +553,19 @@
 rkdown).
-',
 %0A  
-  '
         
@@ -583,18 +583,16 @@
         
-  
 If not s
@@ -616,24 +616,17 @@
 is used.
-',%0A    '
+%0A
   --rela
@@ -701,24 +701,18 @@
 relative
-',%0A    '
+.%0A
   --sour
@@ -811,24 +811,10 @@
 ied.
-',%0A    '',%0A    '
+%0A%0A
 Exam
@@ -818,24 +818,17 @@
 xamples:
-',%0A    '
+%0A
   $ herc
@@ -837,24 +837,17 @@
 e foo.md
-',
 %0A
-    '
     Proc
@@ -888,24 +888,17 @@
 o stdout
-',%0A    '
+%0A
   $ cat 
@@ -935,16 +935,9 @@
 r.md
-',
 %0A
-    '
     
@@ -998,14 +998,12 @@
 r.md
-',
 %0A  
-%5D
+%60
 ,%0A  
@@ -1012,119 +1012,161 @@
     
-string: %5B'_', 'output', 'syntax', 'relative'%5D,%0A    boolean: %5B'sourcemap',
+flags: %7B%0A      output: %7B%0A        type: 'string',%0A        alias: 'o',%0A      %7D,%0A      syntax: %7B%0A        type:
  'st
-d
+r
 in
+g
 '
-%5D
 ,%0A    
-default: %7B%0A      syntax
+    alias: 's',%0A        default
 : 'h
@@ -1170,24 +1170,33 @@
  'hercule',%0A
+      %7D,%0A
       relati
@@ -1203,19 +1203,38 @@
 ve: 
-'
+%7B%0A        type: 'string
 ',%0A    
-%7D,%0A
     
@@ -1244,106 +1244,163 @@
 as: 
-%7B
+'r',
 %0A      
-o: 'output',%0A      s: 'syntax',%0A      r: 'relative',%0A      m: 'sourcemap',%0A      h: 'help'
+  default: '',%0A      %7D,%0A      sourcemap: %7B%0A        type: 'boolean',%0A        alias: 'm',%0A      %7D,%0A      stdin: %7B%0A        type: 'boolean',%0A      %7D
 ,%0A  

use new minimist options structure