"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * command: ppa.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const core_1 = require("@oclif/core");
const distro_1 = tslib_1.__importDefault(require("../../classes/distro"));
const utils_1 = tslib_1.__importDefault(require("../../classes/utils"));
const utils_2 = require("../../lib/utils");
const fs_1 = tslib_1.__importDefault(require("fs"));
const fkey = '/etc/apt/trusted.gpg.d/penguins-eggs-key.gpg';
const flist = '/etc/apt/sources.list.d/penguins-eggs-ppa.list';
/**
 *
 */
class Ppa extends core_1.Command {
    /**
     *
     */
    async run() {
        const { flags } = await this.parse(Ppa);
        utils_1.default.titles(this.id + ' ' + this.argv);
        let verbose = false;
        if (flags.verbose) {
            verbose = true;
        }
        const nointeractive = flags.nointeractive;
        if (utils_1.default.isRoot()) {
            const distro = new distro_1.default();
            /**
             * Debian
             */
            if (distro.familyId === 'debian') {
                if (flags.add) {
                    utils_1.default.warning(`Are you sure to add ${flist} to your repositories?`);
                    if (nointeractive || await utils_1.default.customConfirm('Select yes to continue...')) {
                        await debianAdd();
                    }
                }
                else if (flags.remove) {
                    utils_1.default.warning(`Are you sure to remove ${flist} to your repositories?`);
                    if (nointeractive || await utils_1.default.customConfirm('Select yes to continue...')) {
                        await debianRemove();
                    }
                }
                /**
                 * archlinux
                 */
            }
            if (distro.familyId === 'archlinux') {
                if (flags.add) {
                    if (distro.distroId !== 'ManjaroLinux') {
                        utils_1.default.warning(`Are you sure to add chaotic-aur to your repositories?`);
                        if (await utils_1.default.customConfirm('Select yes to continue...')) {
                            await archAdd();
                        }
                    }
                }
                else if (flags.remove) {
                    await archRemove();
                }
            }
            else {
                /**
                 * Others
                 */
                utils_1.default.warning(`Distro> ${distro.distroId}/${distro.codenameId}, cannot use this command here!`);
            }
        }
    }
}
Ppa.flags = {
    add: core_1.Flags.boolean({ char: 'a', description: 'add penguins-eggs PPA repository' }),
    help: core_1.Flags.help({ char: 'h' }),
    nointeractive: core_1.Flags.boolean({ char: 'n', description: 'no user interaction' }),
    remove: core_1.Flags.boolean({ char: 'r', description: 'remove penguins-eggs PPA repository' }),
    verbose: core_1.Flags.boolean({ char: 'v', description: 'verbose' }),
};
Ppa.description = 'add/remove repo';
Ppa.examples = [
    'sudo eggs tools ppa --add',
    'sudo eggs tools ppa --remove',
];
exports.default = Ppa;
/**
 * archAdd
 */
async function archAdd() {
    const path = "/var/cache/pacman/pkg/";
    const keyring = "chaotic-keyring.pkg.tar.zst";
    const mirrorlist = "chaotic-mirrorlist.pkg.tar.zst";
    const echo = utils_1.default.setEcho(true);
    if (fs_1.default.existsSync(path + keyring) && (fs_1.default.existsSync(path + mirrorlist))) {
        console.log("repository chaotic-aur, already present!");
        await archRemove();
        process.exit();
    }
    await (0, utils_2.exec)('pacman-key --recv-key FBA220DFC880C036 --keyserver keyserver.ubuntu.com', echo);
    await (0, utils_2.exec)('pacman-key --lsign-key FBA220DFC880C036', echo);
    await (0, utils_2.exec)("pacman -U 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst' 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'", echo);
    // Append to /etc/pacman.conf
    const chaoticAppend = "\n[chaotic-aur]\nInclude = /etc/pacman.d/chaotic-mirrorlist\n\n";
    fs_1.default.appendFileSync('/etc/pacman.conf', chaoticAppend);
}
/**
 * archRemove
 */
async function archRemove() {
    const path = "/var/cache/pacman/pkg/";
    const keyring = "chaotic-keyring.pkg.tar.zst";
    const mirrorlist = "chaotic-mirrorlist.pkg.tar.zst";
    const echo = utils_1.default.setEcho(true);
    if (fs_1.default.existsSync(path + keyring) && (fs_1.default.existsSync(path + mirrorlist))) {
        console.log('to remove chaotic-aur:\n');
        console.log(`sudo rm ${path}${keyring}`);
        console.log(`sudo rm ${path}${mirrorlist}`);
        console.log(`sudo nano /etc/pacman.conf`);
        console.log(`remove at the end:`);
        console.log(`[chaotic-aur]`);
        console.log(`Include = /etc/pacman.d/chaotic-mirrorlist`);
    }
}
/**
 * debianAdd
 */
async function debianAdd() {
    await (0, utils_2.exec)(`curl -sS https://pieroproietti.github.io/penguins-eggs-ppa/KEY.gpg| gpg --dearmor | sudo tee ${fkey} > /dev/null`);
    const content = `deb [signed-by=${fkey}] https://pieroproietti.github.io/penguins-eggs-ppa ./\n`;
    fs_1.default.writeFileSync(flist, content);
    await (0, utils_2.exec)('apt-get update');
}
/**
 * debianRemove
 */
async function debianRemove() {
    await (0, utils_2.exec)('rm -f /etc/apt/trusted.gpg.d/penguins-eggs*');
    await (0, utils_2.exec)('rm -f /etc/apt/sources.list.d/penguins-eggs*');
    await (0, utils_2.exec)('rm -f /usr/share/keyrings/penguins-eggs*');
    await (0, utils_2.exec)('apt-get update');
}
