/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb; var __ = require("underscore");















var fs = require("fs");
var util = require("util");

var utils = require("../../../util/utils");
var certUtils = require("../../../util/certUtils");

var $ = utils.getLocaleString;

function VMOSProfile(cli, params) {
  this.cli = cli;
  this.params = params;};


__.extend(VMOSProfile.prototype, {
  generateOSProfile: function generateOSProfile__1(_) { var osProfile, __this = this; var __frame = { name: "generateOSProfile__1", line: 31 }; return __func(_, this, arguments, generateOSProfile__1, 0, __frame, function __$generateOSProfile__1() {
      return __this._parseOSProfileParams(__this.params, __cb(_, __frame, 1, 25, function ___(__0, __1) { osProfile = __1;
        return _(null, {
          profile: osProfile }); }, true)); }); },



  _parseOSProfileParams: function _parseOSProfileParams__2(params, _) { var requestProfile, osType, __this = this; var __frame = { name: "_parseOSProfileParams__2", line: 38 }; return __func(_, this, arguments, _parseOSProfileParams__2, 1, __frame, function __$_parseOSProfileParams__2() {
      requestProfile = {
        computerName: null,
        adminUsername: null,
        adminPassword: null,
        customData: null };


      if (utils.stringIsNullOrEmpty(params.computerName)) {
        if (utils.stringIsNullOrEmpty(params.vmName)) {
          return _(new Error($("Either computerName or vmName is required to prepare OS profile"))); } ;


        requestProfile.computerName = params.vmName; }
       else {
        requestProfile.computerName = params.computerName; } ; return (function __$_parseOSProfileParams__2(__then) {


        if (utils.stringIsNullOrEmpty(params.osType)) {
          return __this.cli.interaction.prompt($("Enter OS type: "), __cb(_, __frame, 19, 43, function ___(__0, __1) { params.osType = __1; __then(); }, true)); } else { __then(); } ; })(function __$_parseOSProfileParams__2() {


        osType = utils.verifyParamExistsInCollection(["Windows","Linux",], __this.params.osType, "osType"); return (function __$_parseOSProfileParams__2(__then) {
          if (((osType === "Windows"))) {
            return __this._parseWindowsConfiguration(params, __cb(_, __frame, 24, 49, function ___(__0, __2) { requestProfile.windowsConfiguration = __2; __then(); }, true)); } else {

            return __this._parseLinuxConfiguration(params, __cb(_, __frame, 26, 47, function ___(__0, __3) { requestProfile.linuxConfiguration = __3; __then(); }, true)); } ; })(function __$_parseOSProfileParams__2() {


          requestProfile.adminUsername = params.adminUsername;
          requestProfile.adminPassword = params.adminPassword;

          if (!utils.stringIsNullOrEmpty(params.customData)) {
            requestProfile.customData = __this._loadCustomData(params.customData); } ;


          return _(null, requestProfile); }); }); }); },


  _parseWindowsConfiguration: function _parseWindowsConfiguration__3(params, _) { var windowsConfiguration, __this = this; var __frame = { name: "_parseWindowsConfiguration__3", line: 77 }; return __func(_, this, arguments, _parseWindowsConfiguration__3, 1, __frame, function __$_parseWindowsConfiguration__3() {
      windowsConfiguration = {
        provisionVMAgent: true }; return (function __$_parseWindowsConfiguration__3(__then) {


        if (utils.stringIsNullOrEmpty(params.adminUsername)) {
          return __this.cli.interaction.promptIfNotGiven($("Enter username: "), params.adminUsername, __cb(_, __frame, 6, 50, function ___(__0, __1) { params.adminUsername = __1; __then(); }, true)); } else { __then(); } ; })(function __$_parseWindowsConfiguration__3() {


        if (utils.ignoreCaseEquals(params.adminUsername, "administrator")) {
          return _(new Error($("The value administrator for parameter adminUsername is not allowed"))); } ; return (function __$_parseWindowsConfiguration__3(__then) {


          if (utils.stringIsNullOrEmpty(params.adminPassword)) {
            return __this.cli.interaction.promptPassword(util.format($("Enter password for %s: "), params.adminUsername), __cb(_, __frame, 14, 50, function ___(__0, __2) { params.adminPassword = __2; __then(); }, true)); } else { __then(); } ; })(function __$_parseWindowsConfiguration__3() {


          __this._ensurePasswordComplexity(params.adminPassword);
          if (params.sshPublickeyFile) {
            __this.cli.output.warn($("The parameter sshPublickeyFile will be ignored when operating system type is Windows")); } ;


          return _(null, windowsConfiguration); }); }); }); },


  _parseLinuxConfiguration: function _parseLinuxConfiguration__4(params, _) { var linuxConfiguration, keys, publicKeyResult, publicKey, __this = this; var __frame = { name: "_parseLinuxConfiguration__4", line: 102 }; return __func(_, this, arguments, _parseLinuxConfiguration__4, 1, __frame, function __$_parseLinuxConfiguration__4() {
      linuxConfiguration = {
        disablePasswordAuthentication: false,
        ssh: null }; return (function __$_parseLinuxConfiguration__4(__then) {


        if (utils.stringIsNullOrEmpty(params.adminUsername)) {
          return __this.cli.interaction.promptIfNotGiven($("Enter username: "), params.adminUsername, __cb(_, __frame, 7, 50, function ___(__0, __1) { params.adminUsername = __1; __then(); }, true)); } else { __then(); } ; })(function __$_parseLinuxConfiguration__4() { return (function __$_parseLinuxConfiguration__4(__then) {


          if (utils.stringIsNullOrEmpty(params.adminPassword)) { return (function __$_parseLinuxConfiguration__4(__then) {
              if ((params.sshPublickeyFile || params.generateSshKeys)) {
                linuxConfiguration.disablePasswordAuthentication = true; __then(); } else {

                return __this.cli.interaction.promptPassword(util.format($("Enter password for %s: "), params.adminUsername), __cb(_, __frame, 14, 52, function ___(__0, __2) { params.adminPassword = __2; __then(); }, true)); } ; })(__then); } else { __then(); } ; })(function __$_parseLinuxConfiguration__4() { return (function __$_parseLinuxConfiguration__4(__then) {



            if (params.generateSshKeys) { return (function __$_parseLinuxConfiguration__4(__then) {
                if (params.sshPublickeyFile) {
                  __this.cli.output.info($("--ssh-publickey-file is specified, --generate-ssh-keys option will be ignored.")); __then(); } else {

                  __this.cli.output.verbose($("Generating SSH keys."));
                  return certUtils.generateSSHKeysIfNeeded(params.computerName, __cb(_, __frame, 23, 29, function ___(__0, __3) { keys = __3;
                    __this.cli.output.info(util.format($("You can use %s private key for SSH authentication."), keys.privateKeyPath));
                    params.sshPublickeyFile = keys.certPath; __then(); }, true)); } ; })(__then); } else { __then(); } ; })(function __$_parseLinuxConfiguration__4() {



            if (params.adminPassword) {
              __this._ensurePasswordComplexity(params.adminPassword); } ;


            if (params.sshPublickeyFile) {
              linuxConfiguration.ssh = {
                publicKeys: [] };


              publicKeyResult = __this._parseSSHPublicKeyFile(params);
              publicKey = {
                path: (("/home/" + params.adminUsername) + "/.ssh/authorized_keys"),
                keyData: publicKeyResult.sshPublickeyPemDataBase64 };


              linuxConfiguration.ssh.publicKeys.push(publicKey); } ;


            return _(null, linuxConfiguration); }); }); }); }); },


  _ensurePasswordComplexity: function(password) {
    if (utils.stringIsNullOrEmpty(password)) {
      throw new Error($("The parameters adminPassword is required")); } ;


    var passwordRegEx = new RegExp(/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\*!@#$%^&+=]).*$/);
    if (!password.match(passwordRegEx)) {
      throw new Error($("Parameter adminPassword must be at least 8 character in length, it must contain a lower case, an upper case, a number and a special character such as !@#$%^&+=")); } ; },



  _parseSSHPublicKeyFile: function(params) {
    this.cli.output.info(util.format($("Verifying the public key SSH file: %s"), params.sshPublickeyFile));
    var sshPublickey = fs.readFileSync(params.sshPublickeyFile);
    var sshPublickeyPemStr = sshPublickey.toString();

    if (certUtils.isOpenSshRSAPub(sshPublickeyPemStr)) {
      sshPublickeyPemStr = certUtils.openSshRSAPubToX509CertPEM(sshPublickeyPemStr); }
     else if (!certUtils.isX509CertPEM(sshPublickeyPemStr)) {
      throw new Error($("Specified SSH certificate is not in PEM or SSH RSA format")); }  ;


    return {
      sshPublickeyPemDataBase64: certUtils.extractBase64X509CertFromPEM(sshPublickeyPemStr) }; },



  _loadCustomData: function(customDataFilePath) {
    var stats = fs.statSync(customDataFilePath);
    var maxSize = 65535;

    if ((stats["size"] > maxSize)) {
      throw new Error($("--custom-data must be less than 64 KB")); } ;


    var customData = fs.readFileSync(customDataFilePath);
    return customData.toString("base64"); }});



module.exports = VMOSProfile;
