"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _fsExtra = _interopRequireDefault(require("fs-extra"));

var _which = _interopRequireDefault(require("which"));

var _backup = require("./backup");

var _yargs = require("yargs");

var _consts = require("./consts");

var exists = false;

try {
  _fsExtra["default"].lstatSync(_consts.DEST_SCRIPT);

  exists = true;
  console.log("".concat(_consts.DEST_SCRIPT, " already exists"));
} catch (e) {
  if (!e.toString().includes('ENOENT')) {
    // Some other error
    throw e;
  }
}

if (exists) {
  try {
    var link = _fsExtra["default"].readlinkSync(_consts.DEST_SCRIPT);

    if (link === _consts.SOURCE_SCRIPT) {
      console.log('already symlinked');
      process.exit(0);
    }
  } catch (_) {// DEST_SCRIPT is not a symlink, ignore.
  }
}

if (_which["default"].sync('ipfs', {
  nothrow: true
}) !== null) {
  exists = true;
}

if (exists) {
  try {
    (0, _backup.backup)(_yargs.argv.data, _consts.DEST_SCRIPT);
  } catch (e) {
    if (!e.toString().includes('ENOENT')) {
      // Some other error
      throw e;
    }
  }

  _fsExtra["default"].unlinkSync(_consts.DEST_SCRIPT);

  console.log("".concat(_consts.DEST_SCRIPT, " removed"));
}

_fsExtra["default"].ensureSymlinkSync(_consts.SOURCE_SCRIPT, _consts.DEST_SCRIPT);

console.log("".concat(_consts.DEST_SCRIPT, " symlinked to ").concat(_consts.SOURCE_SCRIPT));
//# sourceMappingURL=install.js.map