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

var cmd = args.length > 0 ? args[0] : null

if (cmd == 'link') {
    if (args.length < 2) {
        log.console("Usage: cell upgrade link <core_dir>")
        return
    }
    var target = args[1]
    if (shop.link_core(target)) {
        log.console("Linked core -> " + fd.realpath(target))
    } else {
        log.console("Failed to link core to " + target)
    }
} else if (cmd == 'unlink') {
    if (shop.unlink_core()) {
        log.console("Unlinked core.")
    } else {
        log.console("Core was not linked.")
    }
    log.console("Fetching latest core...")
    shop.upgrade_core()
} else {
    // cell upgrade (no args)
    if (shop.is_core_linked()) {
        var core_dir = shop.get_core_dir()
        log.console("Core is linked to " + fd.readlink(core_dir))
        log.console("Unlink first to upgrade standard core.")
    } else {
        log.console("Fetching latest core...")
        shop.upgrade_core()
    }
}

$stop()