"use strict";
/**
 * penguins-eggs
 * krill modules: m-keyboard.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 * https://stackoverflow.com/questions/23876782/how-do-i-split-a-typescript-class-into-multiple-files
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = tslib_1.__importDefault(require("../../classes/utils"));
const utils_2 = require("../../lib/utils");
const fs_1 = tslib_1.__importDefault(require("fs"));
/**
 *
 * @param this
 */
async function mKeyboard() {
    /**
      * influence: - /etc/default/keyboard (x11)
      *            - /etc/vconsole.conf (console)
      *            - /etc/X11/xorg.conf.d/00-keyboard.conf
      *
      * Problem: Actually don't update /etc/default/keyboard (x11)
      *          /etc/vconsole.conf is update in installed systems
      */
    // systemd as default
    let cmd = `chroot ${this.installTarget} localectl set-keymap ${this.keyboardLayout}`;
    if (!utils_1.default.isSystemd()) {
        cmd = `chroot ${this.installTarget} setupcon ${this.toNull}`;
    }
    try {
        await (0, utils_2.exec)(cmd, this.echo);
    }
    catch (error) {
        console.log(error);
        utils_1.default.pressKeyToExit(cmd, true);
    }
    /**
       * this must to be not necessary but...
       *
      * force change /etc/default/keyboard (x11)
      * force change /etc/vconsole.conf
      * force change /etc/X11/xorg.conf.d/00-keyboard.conf
      */
    let content = '# KEYBOARD CONFIGURATION FILE\n\n';
    content += '# Consult the keyboard(5) manual page.\n\n';
    content += '# See penguins-eggs/src/krill/modules/m-keyboard.ts\n\n';
    content += 'XKBMODEL="' + this.keyboardModel + '"\n';
    content += 'XKBLAYOUT="' + this.keyboardLayout + '"\n';
    content += 'XKBVARIANT="' + this.keyboardVariant + '"\n';
    content += 'XKBOPTIONS=""\n';
    content += '\n';
    content += 'BACKSPACE="guess"\n';
    utils_1.default.write(this.installTarget + '/etc/default/keyboard', content);
    /**
     * vconsole.conf: dovrebbe dipendere da systemd
     */
    if (utils_1.default.isSystemd()) {
        content = '# See penguins-eggs/src/krill/modules/set-keyboard.ts\n\n';
        content += 'KEYMAP="' + this.keyboardLayout + '"\n';
        content += 'FONT=\n';
        content += 'FONT_MAP=\n';
        utils_1.default.write(this.installTarget + '/etc/vconsole.conf', content);
    }
    /**
     * 00-keyboard.conf: dovrebbe dipendere da X
     */
    content = '# See penguins-eggs/src/krill/modules/set-keyboard.ts\n\n';
    content += '# Read and parsed by systemd-localed. It\'s probably wise not to edit this file\n';
    content += '# manually too freely.\n';
    content += 'Section "InputClass"\n';
    content += '        Identifier "system-keyboard"\n';
    content += '        MatchIsKeyboard "on"\n';
    content += '        Option "XkbLayout" "' + this.keyboardLayout + '"\n';
    content += 'EndSection\n';
    // Not always exist /etc/X11/xorg.conf.d
    if (fs_1.default.existsSync(this.installTarget + '/etc/X11/xorg.conf.d')) {
        utils_1.default.write(this.installTarget + '/etc/X11/xorg.conf.d/00-keyboard.conf', content);
    }
}
exports.default = mKeyboard;
