"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: iso.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 ExportIso extends core_1.Command {
    async run() {
        const { flags } = await this.parse(ExportIso);
        utils_1.default.titles(this.id + ' ' + this.argv);
        utils_1.default.warning(ExportIso.description);
        const Tu = new tools_1.default();
        await Tu.loadSettings();
        const echo = utils_1.default.setEcho(flags.verbose);
        const rmount = `/tmp/eggs-${(Math.random() + 1).toString(36).slice(7)}`;
        let cmd = `rm -f ${rmount}\n`;
        let filters = ['*.iso', '*.md5', '*.sha256'];
        cmd += `mkdir ${rmount}\n`;
        cmd += `sshfs ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.remotePathIso} ${rmount}\n`;
        if (flags.clean) {
            cmd += `rm -f ${rmount}/${Tu.snapshot_name}*\n`;
        }
        cmd += `cp ${Tu.snapshot_dir}${Tu.snapshot_name}${filters[0]} ${rmount}\n`;
        if (flags.checksum) {
            cmd += `cp ${Tu.snapshot_dir}${Tu.snapshot_name}${filters[1]} ${rmount}\n`;
            cmd += `cp ${Tu.snapshot_dir}${Tu.snapshot_name}${filters[2]} ${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.remotePathIso}${Tu.snapshot_name}${filters[0]}`);
            }
            if (flags.checksum) {
                console.log(`export  ${Tu.config.localPathIso}/${Tu.snapshot_name}${filters[1]}/${filters[2]} to ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.remotePathIso}`);
            }
            console.log(`scp     ${Tu.config.localPathIso}/${Tu.snapshot_name}${filters[0]} ${Tu.config.remoteUser}@${Tu.config.remoteHost}:${Tu.config.remotePathIso}`);
        }
        await (0, utils_2.exec)(cmd, echo);
    }
}
ExportIso.flags = {
    clean: core_1.Flags.boolean({ char: 'c', description: 'delete old ISOs before to copy' }),
    checksum: core_1.Flags.boolean({ char: 'C', description: 'export checksums md5 and sha256' }),
    help: core_1.Flags.help({ char: 'h' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
};
ExportIso.description = 'export iso in the destination host';
ExportIso.examples = [
    'eggs export iso',
    'eggs export iso --clean',
];
exports.default = ExportIso;
