11cb0ef41Sopenharmony_ci#!/usr/bin/env node
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci/*!
41cb0ef41Sopenharmony_ci * Module dependencies.
51cb0ef41Sopenharmony_ci */
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_civar qrcode = require('../lib/main'),
81cb0ef41Sopenharmony_ci    path = require('path'),
91cb0ef41Sopenharmony_ci    fs = require('fs');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci/*!
121cb0ef41Sopenharmony_ci * Parse the process name
131cb0ef41Sopenharmony_ci */
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_civar name = process.argv[1].replace(/^.*[\\\/]/, '').replace('.js', '');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci/*!
181cb0ef41Sopenharmony_ci * Parse the input
191cb0ef41Sopenharmony_ci */
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciif (process.stdin.isTTY) {
221cb0ef41Sopenharmony_ci    // called with input as argument, e.g.:
231cb0ef41Sopenharmony_ci    // ./qrcode-terminal.js "INPUT"
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    var input = process.argv[2];
261cb0ef41Sopenharmony_ci    handleInput(input);
271cb0ef41Sopenharmony_ci} else {
281cb0ef41Sopenharmony_ci    // called with piped input, e.g.:
291cb0ef41Sopenharmony_ci    // echo "INPUT" | ./qrcode-terminal.js
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    var readline = require('readline');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    var interface = readline.createInterface({
341cb0ef41Sopenharmony_ci        input: process.stdin,
351cb0ef41Sopenharmony_ci        output: process.stdout,
361cb0ef41Sopenharmony_ci        terminal: false
371cb0ef41Sopenharmony_ci    });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    interface.on('line', function(line) {
401cb0ef41Sopenharmony_ci        handleInput(line);
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci/*!
451cb0ef41Sopenharmony_ci * Process the input
461cb0ef41Sopenharmony_ci */
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cifunction handleInput(input) {
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    /*!
511cb0ef41Sopenharmony_ci     * Display help
521cb0ef41Sopenharmony_ci     */
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    if (!input || input === '-h' || input === '--help') {
551cb0ef41Sopenharmony_ci        help();
561cb0ef41Sopenharmony_ci        process.exit();
571cb0ef41Sopenharmony_ci    }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    /*!
601cb0ef41Sopenharmony_ci     * Display version
611cb0ef41Sopenharmony_ci     */
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    if (input === '-v' || input === '--version') {
641cb0ef41Sopenharmony_ci        version();
651cb0ef41Sopenharmony_ci        process.exit();
661cb0ef41Sopenharmony_ci    }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    /*!
691cb0ef41Sopenharmony_ci     * Render the QR Code
701cb0ef41Sopenharmony_ci     */
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    qrcode.generate(input);
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci/*!
761cb0ef41Sopenharmony_ci * Helper functions
771cb0ef41Sopenharmony_ci */
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cifunction help() {
801cb0ef41Sopenharmony_ci    console.log([
811cb0ef41Sopenharmony_ci        '',
821cb0ef41Sopenharmony_ci        'Usage: ' + name + ' <message>',
831cb0ef41Sopenharmony_ci        '',
841cb0ef41Sopenharmony_ci        'Options:',
851cb0ef41Sopenharmony_ci        '  -h, --help           output usage information',
861cb0ef41Sopenharmony_ci        '  -v, --version        output version number',
871cb0ef41Sopenharmony_ci        '',
881cb0ef41Sopenharmony_ci        'Examples:',
891cb0ef41Sopenharmony_ci        '',
901cb0ef41Sopenharmony_ci        '  $ ' + name + ' hello',
911cb0ef41Sopenharmony_ci        '  $ ' + name + ' "hello world"',
921cb0ef41Sopenharmony_ci        ''
931cb0ef41Sopenharmony_ci    ].join('\n'));
941cb0ef41Sopenharmony_ci}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_cifunction version() {
971cb0ef41Sopenharmony_ci    var packagePath = path.join(__dirname, '..', 'package.json'),
981cb0ef41Sopenharmony_ci        packageJSON = JSON.parse(fs.readFileSync(packagePath), 'utf8');
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci    console.log(packageJSON.version);
1011cb0ef41Sopenharmony_ci}
102