/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var sys = require('sys');
var fs = require('fs');
var dotfiles = require('util/client_dotfiles');
var async = require('extern/async');

exports.config = {
  short_description: 'Add a remote',
  long_description: 'Add a reference to a remote cast-agent',
  required_arguments: [
    ['name', 'A name for the remote'],
    ['url', 'The URL to the remote']
  ],
  optional_arguments: [],
  switches: []
};

exports.handle_command = function(args) {
  async.waterfall([
    // Make sure ~/.cast exists
    function(callback) {
      dotfiles.ensure_dot_cast(callback);
    },

    // Get or create the current remotes object
    function(callback) {
      dotfiles.get_remotes(callback);
    },

    // Insert the new remote
    function(remotes, callback) {
      if (remotes[args.name]) {
        return callback(new Error("Remote with name '" + args.name + "' already exists"));
      }
      remotes[args.name] = {url: args.url};
      return callback(null, remotes);
    },

    // Save the remotes
    function(remotes, callback) {
      var fstream = fs.createWriteStream(dotfiles.dot_cast_remotes_path);

      fstream.write(JSON.stringify(remotes, null, 4));

      fstream.on('end', function() {
        return callback();
      });

      fstream.on('error', function(err) {
        fstream.removeAllListeners('end');
        return callback(new Error('Error writing remotes file: ' + err));
      });
    }
  ],
  function(err) {
    if (err) {
      sys.puts('Error: ' + err.message);
    }
    else {
      sys.puts('Remote added');
    }
  });
};

@@ -824,27 +824,92 @@
 var 
-fs = require('fs');
+url = require('url');%0Avar fs = require('fs');%0A%0Avar async = require('extern/async');%0A
 %0Avar
@@ -953,29 +953,28 @@
 iles');%0Avar 
-async
+http
  = require('
@@ -969,36 +969,33 @@
  = require('
-extern/async
+util/http
 ');%0A%0Aexports
@@ -1547,37 +1547,8 @@
 %7D,%0A%0A
-    // Insert the new remote%0A
     
@@ -1587,174 +1587,894 @@
     
-if (remotes%5Bargs.name%5D) %7B%0A        return callback(new Error(%22Remote with name '%22 + args.name + %22' already exists%22));%0A      %7D%0A      remotes%5Bargs.name%5D = %7Burl: args.url
+// Fetch the server SSL certificate information%0A      http.get_server_ssl_cert_info(args.url, function (err, cert_info) %7B%0A        if (err) %7B%0A          callback(err);%0A          return;%0A        %7D%0A%0A        callback(null, remotes, cert_info);%0A      %7D);%0A    %7D,%0A%0A    // Insert the new remote%0A    function(remotes, cert_info, callback) %7B%0A      var url_object, hostname, port, fingerprint;%0A%0A      // @TODO: Show user certificate information and ask before accepting%0A      if (remotes%5Bargs.name%5D) %7B%0A        callback(new Error(%22Remote with name '%22 + args.name + %22' already exists%22));%0A        return;%0A      %7D%0A%0A      url_object = url.parse(args.url);%0A%0A      hostname = url_object.hostname;%0A      port = url_object.port %7C%7C 443;%0A      fingerprint = cert_info.fingerprint;%0A%0A      remotes%5Bargs.name%5D = %7Burl: args.url, hostname: hostname, port: port,%0A                            fingerprint: fingerprint
 %7D;%0A 

Save server hostname, port and SSL cert fingerprint when adding a remote.