"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
function tryOSRelease(text) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!text) {
            return null;
        }
        const idRes = text.match(/^ID=(.+)$/m);
        if (!idRes) {
            throw new Error("Failed to parse /etc/os-release");
        }
        const name = idRes[1].replace(/"/g, "");
        const versionRes = text.match(/^VERSION_ID=(.+)$/m);
        let version = versionRes ? versionRes[1].replace(/"/g, "") : "unstable";
        if (name === "ol") {
            version = version.split(".")[0];
        }
        return { name, version };
    });
}
exports.tryOSRelease = tryOSRelease;
function tryLsbRelease(text) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!text) {
            return null;
        }
        const idRes = text.match(/^DISTRIB_ID=(.+)$/m);
        const versionRes = text.match(/^DISTRIB_RELEASE=(.+)$/m);
        if (!idRes || !versionRes) {
            throw new Error("Failed to parse /etc/lsb-release");
        }
        const name = idRes[1].replace(/"/g, "").toLowerCase();
        const version = versionRes[1].replace(/"/g, "");
        return { name, version };
    });
}
exports.tryLsbRelease = tryLsbRelease;
function tryDebianVersion(text) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!text) {
            return null;
        }
        text = text.trim();
        if (text.length < 2) {
            throw new Error("Failed to parse /etc/debian_version");
        }
        return { name: "debian", version: text.split(".")[0] };
    });
}
exports.tryDebianVersion = tryDebianVersion;
function tryAlpineRelease(text) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!text) {
            return null;
        }
        text = text.trim();
        if (text.length < 2) {
            throw new Error("Failed to parse /etc/alpine-release");
        }
        return { name: "alpine", version: text };
    });
}
exports.tryAlpineRelease = tryAlpineRelease;
function tryRedHatRelease(text) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!text) {
            return null;
        }
        const idRes = text.match(/^(\S+)/m);
        const versionRes = text.match(/(\d+)\./m);
        if (!idRes || !versionRes) {
            throw new Error("Failed to parse /etc/redhat-release");
        }
        const name = idRes[1].replace(/"/g, "").toLowerCase();
        const version = versionRes[1].replace(/"/g, "");
        return { name, version };
    });
}
exports.tryRedHatRelease = tryRedHatRelease;
function tryOracleRelease(text) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!text) {
            return null;
        }
        const idRes = text.match(/^(\S+)/m);
        const versionRes = text.match(/(\d+\.\d+)/m);
        if (!idRes || !versionRes) {
            throw new Error("Failed to parse /etc/oracle-release");
        }
        const name = idRes[1].replace(/"/g, "").toLowerCase();
        const version = versionRes[1].replace(/"/g, "").split(".")[0];
        return { name, version };
    });
}
exports.tryOracleRelease = tryOracleRelease;
//# sourceMappingURL=release-analyzer.js.map