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