#!/usr/bin/env node

var shell = require('../../lib/spawn').shell;
require('../../lib/prototype');
var processArgs = require('../../lib/args');
var fsp = require('fs').promises;
var path = require('path');

console.error = function () { }

var argMap = processArgs(process.argv.slice(2));
var cwd = process.cwd();
var cmd = path.resolve(__dirname, '../../bin/7za.exe');
var output = path.resolve(cwd, new Date().Format('yyyy-MM-dd HH-mm-ss.7z'))
var input = "";

(async () => {
    for (var k in argMap) {
        switch (k) {
            case '-o':
                output = path.resolve(cwd, argMap[k])
                break;
            default:
                input = k;
                break;
        }
    }

    var zipDir = "";
    var zipDirShow = "";
    if (!input) zipDir = './'
    else zipDir = input

    if (['./', ''].includes(zipDir)) {
        zipDirShow = path.resolve(cwd, zipDir)
    } else {
        if (!path.isAbsolute(zipDir)) {
            zipDirShow = path.resolve(cwd, zipDir)
        }
        zipDirShow = zipDir;
    }

    console.log(`压缩目录：\r\n\t${zipDirShow}`)
    console.log(`输出文件：\r\n\t${output}`)

    var opts = { cwd }
    var args = ['a', output, '-mx9', zipDir];

    var r = await shell({ cmd, args, opts })
    console.log(r.stdout)

})();