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

var cmd = length(args) > 0 ? args[0] : null
var target = null
var core_dir = null

if (cmd == 'link') {
    if (length(args) < 2) {
        log.console("Usage: cell upgrade link <core_dir>")
        $stop()
    }
    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()) {
        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()
