950ebc
#!/usr/bin/lua
950ebc
-- rpm call
cccebf
-- debug=true lua -- copy_jdk_configs.lua   --currentjvm "%{uniquesuffix %{nil}}" --jvmdir "%{_jvmdir %{nil}}" --origname "%{name}" --origjavaver "%{javaver}" --arch "%{_arch}"
950ebc
--test call
cccebf
-- debug=true lua -- copy_jdk_configs.lua   --currentjvm "java-1.8.0-openjdk-1.8.0.65-3.b17.fc22.x86_64" --jvmdir "/usr/lib/jvm" --origname "java-1.8.0-openjdk" --origjavaver "1.8.0" --arch "x86_64" --jvmDestdir /home/jvanek/Desktop
950ebc
cccebf
local M = {}
950ebc
cccebf
if (os.getenv("debug") == "true") then
cccebf
    debug = true;
cccebf
else
cccebf
    debug = false;
950ebc
end
950ebc
950ebc
local function debugOneLinePrint(string)
cccebf
    if (debug) then
cccebf
        print(string)
cccebf
    end ;
950ebc
end
950ebc
cccebf
function getPath(str, sep)
cccebf
    sep = sep or '/'
cccebf
    return str:match("(.*" .. sep .. ")")
950ebc
end
950ebc
950ebc
function splitToTable(source, pattern)
cccebf
    local i1 = string.gmatch(source, pattern)
cccebf
    local l1 = {}
cccebf
    for i in i1 do
cccebf
        table.insert(l1, i)
cccebf
    end
cccebf
    return l1
950ebc
end
950ebc
950ebc
local function slurp(path)
950ebc
    local f = io.open(path)
950ebc
    local s = f:read("*a")
950ebc
    f:close()
950ebc
    return s
950ebc
end
950ebc
950ebc
function trim(s)
cccebf
    return (s:gsub("^%s*(.-)%s*$", "%1"))
950ebc
end
950ebc
950ebc
local function dirWithParents(path)
cccebf
    local s = ""
cccebf
    local dirs = splitToTable(path, "[^/]+")
cccebf
    for i, d in pairs(dirs) do
cccebf
        if (i == #dirs) then
cccebf
            break
cccebf
        end
cccebf
        s = s .. "/" .. d
cccebf
        local stat2 = posix.stat(s, "type");
cccebf
        if (stat2 == nil) then
cccebf
            debugOneLinePrint(s .. " does not exists, creating")
cccebf
            if (not dry) then
cccebf
                posix.mkdir(s)
cccebf
            end
cccebf
        else
cccebf
            debugOneLinePrint(s .. " exists,not creating")
cccebf
        end
950ebc
    end
950ebc
end
950ebc
950ebc
cccebf
-- main function,
cccebf
-- formelry main body
cccebf
-- move to function resolved
cccebf
-- https://bugzilla.redhat.com/show_bug.cgi?id=1892224
cccebf
-- for readability not indented, todo, indent once tuned
cccebf
cccebf
function M.mainProgram(arg)
cccebf
    debugOneLinePrint("cjc: lua debug on")
cccebf
    local caredFiles = { "jre/lib/calendars.properties",
cccebf
                         "jre/lib/content-types.properties",
cccebf
                         "jre/lib/flavormap.properties",
cccebf
                         "jre/lib/logging.properties",
cccebf
                         "jre/lib/net.properties",
cccebf
                         "jre/lib/psfontj2d.properties",
cccebf
                         "jre/lib/sound.properties",
cccebf
                         "jre/lib/deployment.properties",
cccebf
                         "jre/lib/deployment.config",
cccebf
                         "jre/lib/security/US_export_policy.jar",
cccebf
                         "jre/lib/security/unlimited/US_export_policy.jar",
cccebf
                         "jre/lib/security/limited/US_export_policy.jar",
cccebf
                         "jre/lib/security/policy/unlimited/US_export_policy.jar",
cccebf
                         "jre/lib/security/policy/limited/US_export_policy.jar",
cccebf
                         "jre/lib/security/java.policy",
cccebf
                         "jre/lib/security/java.security",
cccebf
                         "jre/lib/security/local_policy.jar",
cccebf
                         "jre/lib/security/unlimited/local_policy.jar",
cccebf
                         "jre/lib/security/limited/local_policy.jar",
cccebf
                         "jre/lib/security/policy/unlimited/local_policy.jar",
cccebf
                         "jre/lib/security/policy/limited/local_policy.jar",
cccebf
                         "jre/lib/security/nss.cfg",
cccebf
                         "jre/lib/security/cacerts",
cccebf
                         "jre/lib/security/blacklisted.certs",
cccebf
                         "jre/lib/security/jssecacerts",
cccebf
                         "jre/lib/security/trusted.certs",
cccebf
                         "jre/lib/security/trusted.jssecerts",
cccebf
                         "jre/lib/security/trusted.clientcerts",
cccebf
                         "jre/lib/ext",
cccebf
                         "jre/lib/security/blacklist",
cccebf
                         "jre/lib/security/javaws.policy",
cccebf
                         "jre/lib/security/nss.fips.cfg",
cccebf
                         "lib/security",
cccebf
                         "conf",
cccebf
                         "lib/ext" }
cccebf
cccebf
    -- before import to allow run from spec
cccebf
    if (arg[1] == "--list") then
cccebf
        for i, file in pairs(caredFiles) do
cccebf
            print(file)
cccebf
        end
cccebf
        return 0;
cccebf
    end
cccebf
cccebf
    -- yum install lua-posix
cccebf
    local posix = require "posix"
cccebf
cccebf
    -- the one we are installing
cccebf
    local currentjvm = nil
cccebf
    local jvmdir = nil
cccebf
    local jvmDestdir = nil
cccebf
    local origname = nil
cccebf
    local origjavaver = nil
cccebf
    local arch = nil
cccebf
    local temp = nil;
cccebf
    local dry = false;
cccebf
cccebf
    for i = 1, #arg, 2 do
cccebf
        if (arg[i] == "--help" or arg[i] == "-h") then
cccebf
            print("all but jvmDestdir and debug are mandatory")
cccebf
            print("  --currentjvm")
cccebf
            print("    NVRA of currently installed java")
cccebf
            print("  --jvmdir")
cccebf
            print("    Directory where to find this kind of virtual machine. Generally /usr/lib/jvm")
cccebf
            print("  --origname")
cccebf
            print("    convinient switch to determine jdk. Generally java-1.X.0-vendor")
cccebf
            print("  --origjavaver")
cccebf
            print("    convinient switch to determine jdk's version. Generally 1.X.0")
cccebf
            print("  --arch")
cccebf
            print("    convinient switch to determine jdk's arch")
cccebf
            print("  --jvmDestdir")
cccebf
            print("    Migration/testing switch. Target Mostly same as jvmdir, but you may wont to copy ouside it.")
cccebf
            print("  --debug or $debug")
cccebf
            print("    Enables printing out whats going on. true/false. False by default")
cccebf
            print("  --temp")
cccebf
            print("    optional file to save intermediate result - directory configs were copied from")
cccebf
            print("  --dry")
cccebf
            print("    true/fase if true, then no changes will be written to disk except one tmp file. False by default")
cccebf
            print("  **** specil parasm ****")
cccebf
            print("  --list")
cccebf
            print("    if present on cmdline, list all cared files and exists")
cccebf
            os.exit(0)
cccebf
        end
cccebf
        if (arg[i] == "--currentjvm") then
cccebf
            currentjvm = arg[i + 1]
cccebf
        end
cccebf
        if (arg[i] == "--jvmdir") then
cccebf
            jvmdir = arg[i + 1]
cccebf
        end
cccebf
        if (arg[i] == "--origname") then
cccebf
            origname = arg[i + 1]
cccebf
        end
cccebf
        if (arg[i] == "--origjavaver") then
cccebf
            origjavaver = arg[i + 1]
cccebf
        end
cccebf
        if (arg[i] == "--arch") then
cccebf
            arch = arg[i + 1]
cccebf
        end
cccebf
        if (arg[i] == "--jvmDestdir") then
cccebf
            jvmDestdir = arg[i + 1]
cccebf
        end
cccebf
        if (arg[i] == "--debug") then
cccebf
            --no string, boolean, workaround
cccebf
            if (arg[i + 1] == "true") then
cccebf
                debug = true
cccebf
            end
cccebf
        end
cccebf
        if (arg[i] == "--dry") then
cccebf
            --no string, boolean, workaround
cccebf
            if (arg[i + 1] == "true") then
cccebf
                dry = true
cccebf
            end
cccebf
        end
cccebf
        if (arg[i] == "--temp") then
cccebf
            temp = arg[i + 1]
cccebf
        end
cccebf
    end
cccebf
cccebf
    if (jvmDestdir == nil) then
cccebf
        jvmDestdir = jvmdir
cccebf
    end
cccebf
cccebf
    if (debug) then
cccebf
        print("--currentjvm:");
cccebf
        print(currentjvm);
cccebf
        print("--jvmdir:");
cccebf
        print(jvmdir);
cccebf
        print("--jvmDestdir:");
cccebf
        print(jvmDestdir);
cccebf
        print("--origname:");
cccebf
        print(origname);
cccebf
        print("--origjavaver:");
cccebf
        print(origjavaver);
cccebf
        print("--arch:");
cccebf
        print(arch);
cccebf
        print("--debug:");
cccebf
        print(debug);
cccebf
    end
950ebc
cccebf
    --trasnform substitute names to lua patterns
cccebf
    local name = string.gsub(string.gsub(origname, "%-", "%%-"), "%.", "%%.")
cccebf
    local javaver = string.gsub(origjavaver, "%.", "%%.")
950ebc
cccebf
    local jvms = { }
950ebc
cccebf
    debugOneLinePrint("started")
950ebc
cccebf
    foundJvms = posix.dir(jvmdir);
cccebf
    if (foundJvms == nil) then
cccebf
        debugOneLinePrint("no, or nothing in " .. jvmdir .. " exit")
cccebf
        return
950ebc
    end
950ebc
cccebf
    debugOneLinePrint("found " .. #foundJvms .. " jvms")
cccebf
cccebf
    for i, p in pairs(foundJvms) do
cccebf
        -- regex similar to %{_jvmdir}/%{name}-%{javaver}*%{_arch} bash command
cccebf
        if (string.find(p, name .. "%-" .. javaver .. ".*" .. arch) ~= nil) then
cccebf
            debugOneLinePrint("matched:  " .. p)
cccebf
            if (currentjvm == p) then
cccebf
                debugOneLinePrint("this jdk is already installed. exiting lua script")
cccebf
                return
cccebf
            end ;
cccebf
            if (string.match(p, ".*-debug$")) then
cccebf
                print(p .. " matched but seems to be debug variant. Skipping")
cccebf
            else
cccebf
                table.insert(jvms, p)
cccebf
            end
cccebf
        else
cccebf
            debugOneLinePrint("NOT matched:  " .. p)
cccebf
        end
cccebf
    end
cccebf
cccebf
    if (#jvms <= 0) then
cccebf
        debugOneLinePrint("no matching jdk in " .. jvmdir .. " exit")
cccebf
        return
cccebf
    end ;
cccebf
cccebf
    debugOneLinePrint("matched " .. #jvms .. " jdk in " .. jvmdir)
cccebf
cccebf
    --full names are like java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64
cccebf
    table.sort(jvms, function(a, b)
cccebf
        -- version-sort
cccebf
        -- split on non word: . -
cccebf
        local l1 = splitToTable(a, "[^%.-]+")
cccebf
        local l2 = splitToTable(b, "[^%.-]+")
cccebf
        for x = 1, math.min(#l1, #l2) do
cccebf
            local l1x = tonumber(l1[x])
cccebf
            local l2x = tonumber(l2[x])
cccebf
            if (l1x ~= nil and l2x ~= nil) then
cccebf
                --if hunks are numbers, go with them
cccebf
                if (l1x < l2x) then
cccebf
                    return true;
cccebf
                end
cccebf
                if (l1x > l2x) then
cccebf
                    return false;
cccebf
                end
cccebf
            else
cccebf
                if (l1[x] < l2[x]) then
cccebf
                    return true;
cccebf
                end
cccebf
                if (l1[x] > l2[x]) then
cccebf
                    return false;
cccebf
                end
cccebf
            end
cccebf
            -- if hunks are equals then move to another pair of hunks
cccebf
        end
cccebf
        return a < b
cccebf
cccebf
    end)
cccebf
cccebf
    if (debug) then
cccebf
        print("sorted lsit of jvms")
cccebf
        for i, file in pairs(jvms) do
cccebf
            print(file)
cccebf
        end
950ebc
    end
950ebc
cccebf
    latestjvm = jvms[#jvms]
950ebc
cccebf
    if (temp ~= nil) then
cccebf
        src = jvmdir .. "/" .. latestjvm
cccebf
        debugOneLinePrint("temp declared as " .. temp .. " saving used dir of " .. src)
cccebf
        file = io.open(temp, "w")
cccebf
        file:write(src)
cccebf
        file:close()
cccebf
    end
950ebc
cccebf
    local readlinkOutput = os.tmpname()
cccebf
cccebf
    for i, file in pairs(caredFiles) do
cccebf
        local SOURCE = jvmdir .. "/" .. latestjvm .. "/" .. file
cccebf
        local DEST = jvmDestdir .. "/" .. currentjvm .. "/" .. file
cccebf
        debugOneLinePrint("going to copy " .. SOURCE)
cccebf
        debugOneLinePrint("to  " .. DEST)
cccebf
        local stat1 = posix.stat(SOURCE, "type");
cccebf
        if (stat1 ~= nil) then
cccebf
            debugOneLinePrint(SOURCE .. " exists")
cccebf
            dirWithParents(DEST)
cccebf
            -- Copy with -a to keep everything intact
cccebf
            local exe = "cp" .. " -ar " .. SOURCE .. " " .. DEST
cccebf
            local linkExe = "readlink" .. " -f " .. SOURCE .. " > " .. readlinkOutput
cccebf
            debugOneLinePrint("executing " .. linkExe)
cccebf
            os.remove(readlinkOutput)
cccebf
            os.execute(linkExe)
cccebf
            local link = trim(slurp(readlinkOutput))
cccebf
            debugOneLinePrint("  ...link is " .. link)
cccebf
            if (not ((link) == (SOURCE))) then
cccebf
                debugOneLinePrint("WARNING link " .. link .. " where file " .. SOURCE .. " expected!")
cccebf
                debugOneLinePrint("Will try to copy link target rather then link itself!")
cccebf
                --replacing  any NVRA by future NVRA (still execting to have NVRA for any multiple-installable targets
cccebf
                -- lua stubbornly consider dash as inteval. Replacing by dot to match X-Y more correct as X.Y rather then not at all
cccebf
                local linkDest = string.gsub(link, latestjvm:gsub("-", "."), currentjvm)
cccebf
                debugOneLinePrint("attempting to copy " .. link .. " to " .. linkDest)
cccebf
                if (link == linkDest) then
cccebf
                    debugOneLinePrint("Those are identical files! Nothing to do!")
cccebf
                else
cccebf
                    local exe2 = "cp" .. " -ar " .. link .. " " .. linkDest
cccebf
                    dirWithParents(linkDest)
cccebf
                    debugOneLinePrint("executing " .. exe2)
cccebf
                    if (not dry) then
cccebf
                        os.execute(exe2)
cccebf
                    end
cccebf
                end
cccebf
            else
cccebf
                debugOneLinePrint("executing " .. exe)
cccebf
                if (not dry) then
cccebf
                    os.execute(exe)
cccebf
                end
cccebf
            end
cccebf
        else
cccebf
            debugOneLinePrint(SOURCE .. " does not exists")
950ebc
        end
950ebc
    end
cccebf
cccebf
end --unindented main function
cccebf
cccebf
if (arg == nil) then
cccebf
    debugOneLinePrint("arg variable is nil, you have to call mainProgram manually") -- this can actually not be invoked, as the debug is set via arg
cccebf
else
cccebf
    M.mainProgram(arg)
950ebc
end
cccebf
cccebf
return M