// cell unlink <origin>
// Unlinks <origin> from its <target> from a previous link.

var link = use('link')
var shop = use('internal/shop')

if (length(args) < 1) {
  log.console("Usage: cell unlink <origin>")
  log.console("Removes a link and restores the original package.")
  $stop()
}

var origin = args[0]

var _restore = null
if (link.remove(origin)) {
  log.console("Removed link for " + origin)

  // Try to restore the original package
  log.console("Restoring " + origin + "...")
  _restore = function() {
    shop.fetch(origin)
    shop.extract(origin)
    log.console("Restored " + origin)
  } disruption {
    log.console("Could not restore")
    log.console("Run 'cell update " + origin + "' to restore")
  }
  _restore()
} else {
  log.console("No link found for " + origin)
}

$stop()
