"use strict";
// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed 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.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// cmdline platform
const Tp = __importStar(require("thingpedia"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const child_process = __importStar(require("child_process"));
const util = __importStar(require("util"));
const node_gettext_1 = __importDefault(require("node-gettext"));
const _unzipApi = {
    unzip(zipPath, dir) {
        const args = ['-uo', zipPath, '-d', dir];
        return util.promisify(child_process.execFile)('/usr/bin/unzip', args, {
            maxBuffer: 10 * 1024 * 1024
        }).then(({ stdout, stderr }) => {
            console.log('stdout', stdout);
            console.log('stderr', stderr);
        });
    }
};
const _contentApi = {
    getStream(url) {
        return new Promise((resolve, reject) => {
            if (url.startsWith('file:///')) {
                const path = url.substring('file://'.length);
                child_process.execFile('xdg-mime', ['query', 'filetype', path], (err, stdout, stderr) => {
                    const stream = fs.createReadStream(path);
                    if (err) {
                        // ignore error if we failed to query the content type (e.g. if xdg-mime is not installed)
                        stream.contentType = 'application/octet-stream';
                    }
                    else {
                        stream.contentType = String(stdout).trim();
                    }
                    resolve(stream);
                });
            }
            else {
                reject(new Error('Unsupported url ' + url));
            }
        });
    }
};
function safeMkdirSync(dir) {
    try {
        fs.mkdirSync(dir);
    }
    catch (e) {
        if (e.code !== 'EEXIST')
            throw e;
    }
}
class Platform extends Tp.BasePlatform {
    // Initialize the platform code
    // Will be called before instantiating the engine
    constructor(homedir, locale, thingpediaUrl) {
        super();
        this._locale = locale;
        this._gettext = new node_gettext_1.default();
        this._gettext.setLocale(this._locale);
        this._timezone = process.env.TZ || '';
        if (homedir) {
            this._filesDir = path.resolve(homedir);
            safeMkdirSync(this._filesDir);
            this._cacheDir = path.resolve(homedir, 'cache');
            safeMkdirSync(this._cacheDir);
        }
        else {
            this._filesDir = path.resolve(os.homedir(), '.config/genie-toolkit');
            safeMkdirSync(path.resolve(os.homedir(), '.config'));
            safeMkdirSync(this._filesDir);
            this._cacheDir = path.resolve(os.homedir(), '.cache/genie-toolkit');
            safeMkdirSync(path.resolve(os.homedir(), '.cache'));
            safeMkdirSync(this._cacheDir);
        }
        this._prefs = new Tp.Helpers.FilePreferences(this._filesDir + '/prefs.db');
        this._tpClient = new Tp.HttpClient(this, thingpediaUrl);
    }
    get type() {
        return 'cmdline';
    }
    get locale() {
        return this._locale;
    }
    get timezone() {
        return this._timezone;
    }
    getPlatformDevice() {
        return null;
    }
    // Check if we need to load and run the given thingengine-module on
    // this platform
    // (eg we don't need discovery on the cloud, and we don't need graphdb,
    // messaging or the apps on the phone client)
    hasFeature(feature) {
        return true;
    }
    // Check if this platform has the required capability
    // (eg. long running, big storage, reliable connectivity, server
    // connectivity, stable IP, local device discovery, bluetooth, etc.)
    //
    // Which capabilities are available affects which apps are allowed to run
    hasCapability(cap) {
        switch (cap) {
            case 'code-download':
            case 'thingpedia-client':
            case 'gettext':
            case 'content-api':
                return true;
            default:
                return false;
        }
    }
    // Retrieve an interface to an optional functionality provided by the
    // platform
    //
    // This will return null if hasCapability(cap) is false
    getCapability(cap) {
        switch (cap) {
            case 'code-download':
                return _unzipApi;
            case 'thingpedia-client':
                return this._tpClient;
            case 'gettext':
                return this._gettext;
            case 'content-api':
                return _contentApi;
            default:
                return null;
        }
    }
    // Obtain a shared preference store
    // Preferences are simple key/value store which is shared across all apps
    // but private to this instance (tier) of the platform
    // Preferences should be normally used only by the engine code, and a persistent
    // shared store such as DataVault should be used by regular apps
    getSharedPreferences() {
        return this._prefs;
    }
    // Get a directory that is guaranteed to be writable
    // (in the private data space for Android)
    getWritableDir() {
        return this._filesDir;
    }
    // Get a temporary directory
    // Also guaranteed to be writable, but not guaranteed
    // to persist across reboots or for long times
    // (ie, it could be periodically cleaned by the system)
    getTmpDir() {
        return os.tmpdir();
    }
    // Get a directory good for long term caching of code
    // and metadata
    getCacheDir() {
        return this._cacheDir;
    }
    // Get the filename of the sqlite database
    getSqliteDB() {
        return this._filesDir + '/sqlite.db';
    }
    getSqliteKey() {
        return null;
    }
    getGraphDB() {
        return this._filesDir + '/rdf.db';
    }
    // Get the Thingpedia developer key, if one is configured
    getDeveloperKey() {
        return (this._prefs.get('developer-key') || null);
    }
    // Change the Thingpedia developer key, if possible
    // Returns true if the change actually happened
    setDeveloperKey(key) {
        return this._prefs.set('developer-key', key);
    }
    getOrigin() {
        // pretend to be a local thingpedia server
        // the user is expected to copy-paste oauth urls manually
        return 'http://127.0.0.1:8080';
    }
    getCloudId() {
        return (this._prefs.get('cloud-id') || null);
    }
    getAuthToken() {
        return this._prefs.get('auth-token');
    }
    // Change the auth token
    // Returns true if a change actually occurred, false if the change
    // was rejected
    setAuthToken(authToken) {
        const oldAuthToken = this._prefs.get('auth-token');
        if (oldAuthToken !== undefined && authToken !== oldAuthToken)
            return false;
        this._prefs.set('auth-token', authToken);
        return true;
    }
}
exports.default = Platform;
//# sourceMappingURL=cmdline-platform.js.map