"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: deb.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const tools_1 = tslib_1.__importDefault(require("../../classes/tools"));
const utils_1 = tslib_1.__importDefault(require("../../classes/utils"));
const utils_2 = require("../../lib/utils");
class ExportDeb extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(ExportDeb);
        utils_1.default.titles(this.id + ' ' + this.argv);
        const Tu = new tools_1.default();
        utils_1.default.warning(ExportDeb.description);
        await Tu.loadSettings();
        const echo = utils_1.default.setEcho(flags.verbose);
        let script = '';
        let arch = utils_1.default.uefiArch();
        if (flags.all) {
            arch = '*';
        }
        if (flags.clean) {
            arch += '.deb';
            script = String(script);
        }
        const rmount = `/tmp/eggs-${(Math.random() + 1).toString(36).slice(7)}`;
        let cmd = `rm -f ${rmount}\n`;
        cmd += `mkdir ${rmount}\n`;
        cmd += `sshfs ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.remotePathDeb} ${rmount}\n`;
        if (flags.clean) {
            cmd += `rm -f ${rmount}/${Tu.config.filterDeb}${arch}\n`;
        }
        cmd += `cp ${Tu.config.localPathDeb}${Tu.config.filterDeb}${arch}  ${rmount}\n`;
        cmd += 'sync\n';
        cmd += `umount ${rmount}\n`;
        cmd += `rm -f ${rmount}\m`;
        if (!flags.verbose) {
            if (flags.clean) {
                console.log(`remove: ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.filterDeb}${arch}`);
            }
            console.log(`copy: ${Tu.config.localPathDeb}${Tu.config.filterDeb}${arch} to ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.remotePathDeb}`);
        }
        await (0, utils_2.exec)(cmd, echo);
    }
}
ExportDeb.flags = {
    all: core_1.Flags.boolean({ char: 'a', description: 'export all archs' }),
    clean: core_1.Flags.boolean({ char: 'c', description: 'remove old .deb before to copy' }),
    help: core_1.Flags.help({ char: 'h' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
};
ExportDeb.description = 'export deb/docs/iso to the destination host';
ExportDeb.examples = [
    'eggs export deb',
    'eggs export deb --clean',
    'eggs export deb --all',
];
exports.default = ExportDeb;
