#!/usr/local/bin/node

const fs = require('fs-extra')
const { join, parse } = require('path')

const meow = require('meow')
const { replaceAll, debug } = require('./')

const cli = meow(
  `
  Usage: $ tomify <image>

  Options:
    --output, -o Output Path, default: $PWD
    --resize, -r Resize Tom, default: 1 (Number)
    --debug, -d Debug Mode, default: false
    --gif, -g Gif Path, default: tom-wiggle.gif
    --help, -h Display Help
    --version Display Version

  Examples:
    $ tomify image.jpg
        # process single image and save to $PWD/image.gif

    $ tomify person1.jpg person2.jpg --output ./new-folder
        # process multiple images, and saves to ./new-folder
`,
  {
    alias: { h: 'help', o: 'output', d: 'debug', r: 'resize', g: 'gif' }
  }
)

/**
 * CLI
 */

process.env.TOMIFY_DEBUG = cli.flags.debug ? 'true' : 'false'
if (cli.input.length === 0 && !cli.flags.input) {
  console.error('Specify at least one path')
  process.exit(1)
}

if (!cli.flags.resize) cli.flags.resize = 1
else if (typeof cli.flags.resize !== 'number') {
  console.error('resize must be a number')
  process.exit(1)
}
const resize = cli.flags.resize

let dest = cli.flags.output || process.cwd()
fs.ensureDirSync(dest)
debug('Destination: %s', dest)

const gif = cli.flags.gif || join(__dirname, 'tom-wiggle.gif')

/**
 * Build Toms
 */

const toms = []
cli.input.map(file => {
  toms.push(new Promise((resolve, reject) => {
    dest = join(dest, `${parse(file).name}.gif`)
    return replaceAll({ file, dest, resize, gif }).then(resolve).catch(reject)
    })
  )
})

Promise.all(toms)
  .then(() => {
    debug('Done!')
    process.exit(0)
  })
  .catch(err => {
    if (err.stderr) console.error(err.stderr)
    else console.error(err)
    process.exit(err.code || 1)
  })


@@ -66,16 +66,25 @@
 n, parse
+, resolve
  %7D = req
@@ -1284,18 +1284,42 @@
 ags.gif 
-%7C%7C
+? resolve(cli.flags.gif) :
  join(__
@@ -1344,16 +1344,45 @@
 le.gif')
+%0Adebug('Gif in use: %25s', gif)
 %0A%0A/**%0A *

improve custom gif handling