46c95d0
#!/usr/bin/lua
46c95d0
-- rpm call
46c95d0
-- lua -- copy_jdk_configs.lua   --currentjvm "%{uniquesuffix %{nil}}" --jvmdir "%{_jvmdir %{nil}}" --origname "%{name}" --origjavaver "%{javaver}" --arch "%{_arch}" --debug true
46c95d0
--test call
46c95d0
--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
46c95d0
46c95d0
-- yum install lua-posix
46c95d0
local posix = require "posix"
46c95d0
46c95d0
-- the one we are installing
46c95d0
local currentjvm = nil
46c95d0
local jvmdir = nil
46c95d0
local jvmDestdir = nil
46c95d0
local origname = nil
46c95d0
local origjavaver = nil
46c95d0
local arch = nil
46c95d0
local debug = false;
46c95d0
46c95d0
for i=1,#arg,2 do 
46c95d0
  if (arg[i] == "--help" or arg[i] == "-h") then 
46c95d0
    print("all but jvmDestdir and debug are mandatory")
46c95d0
    print("  --currentjvm")
46c95d0
    print("    NVRA of currently installed java")
46c95d0
    print("  --jvmdir") 
46c95d0
    print("    Directory where to find this kind of virtual machine. Generally /usr/lib/jvm")
46c95d0
    print("  --origname")
46c95d0
    print("    convinient switch to determine jdk. Generally java-1.X.0-vendor")
46c95d0
    print("  --origjavaver")
46c95d0
    print("    convinient switch to determine jdk's version. Generally 1.X.0")
46c95d0
    print("  --arch")
46c95d0
    print("    convinient switch to determine jdk's arch")
46c95d0
    print("  --jvmDestdir")
46c95d0
    print("    Migration/testing switch. Target Mostly same as jvmdir, but you may wont to copy ouside it.")
46c95d0
    print("  --debug")
46c95d0
    print("    Enables printing out whats going on. true/false")
46c95d0
    os.exit(0)
46c95d0
  end
46c95d0
  if (arg[i] == "--currentjvm") then 
46c95d0
    currentjvm=arg[i+1]
46c95d0
  end
46c95d0
  if (arg[i] == "--jvmdir") then 
46c95d0
    jvmdir=arg[i+1]
46c95d0
  end
46c95d0
  if (arg[i] == "--origname") then 
46c95d0
    origname=arg[i+1]
46c95d0
  end
46c95d0
  if (arg[i] == "--origjavaver") then 
46c95d0
    origjavaver=arg[i+1]
46c95d0
  end
46c95d0
  if (arg[i] == "--arch") then 
46c95d0
    arch=arg[i+1]
46c95d0
  end
46c95d0
  if (arg[i] == "--jvmDestdir") then 
46c95d0
    jvmDestdir=arg[i+1]
46c95d0
  end
46c95d0
  if (arg[i] == "--debug") then 
46c95d0
--no string, boolean, workaround
46c95d0
    if (arg[i+1] == "true") then
46c95d0
     debug = true
46c95d0
    end
46c95d0
  end
46c95d0
end
46c95d0
46c95d0
if (jvmDestdir == nill) then
46c95d0
jvmDestdir = jvmdir
46c95d0
end
46c95d0
46c95d0
46c95d0
if (debug) then
46c95d0
  print("--currentjvm:");
46c95d0
  print(currentjvm);
46c95d0
  print("--jvmdir:");
46c95d0
  print(jvmdir);
46c95d0
  print("--jvmDestdir:");
46c95d0
  print(jvmDestdir);
46c95d0
  print("--origname:");
46c95d0
  print(origname);
46c95d0
  print("--origjavaver:");
46c95d0
  print(origjavaver);
46c95d0
  print("--arch:");
46c95d0
  print(arch);
46c95d0
  print("--debug:");
46c95d0
  print(debug);
46c95d0
end
46c95d0
46c95d0
46c95d0
--trasnform substitute names to lua patterns
46c95d0
local name = string.gsub(string.gsub(origname, "%-", "%%-"), "%.", "%%.")
46c95d0
local javaver = string.gsub(origjavaver, "%.", "%%.")
46c95d0
46c95d0
local jvms = { }
46c95d0
46c95d0
local caredFiles = {"jre/lib/calendars.properties",
46c95d0
              "jre/lib/content-types.properties",
46c95d0
              "jre/lib/flavormap.properties",
46c95d0
              "jre/lib/logging.properties",
46c95d0
              "jre/lib/net.properties",
46c95d0
              "jre/lib/psfontj2d.properties",
46c95d0
              "jre/lib/sound.properties",
46c95d0
              "jre/lib/deployment.properties",
46c95d0
              "jre/lib/deployment.config",
46c95d0
              "jre/lib/security/US_export_policy.jar",
46c95d0
              "jre/lib/security/java.policy",
46c95d0
              "jre/lib/security/java.security",
46c95d0
              "jre/lib/security/local_policy.jar",
c31c255
              "jre/lib/security/nss.cfg",
46c95d0
              "jre/lib/ext"}
46c95d0
46c95d0
function splitToTable(source, pattern)
46c95d0
  local i1 = string.gmatch(source, pattern) 
46c95d0
  local l1 = {}
46c95d0
  for i in i1 do
46c95d0
    table.insert(l1, i)
46c95d0
  end
46c95d0
  return l1
46c95d0
end
46c95d0
46c95d0
if (debug) then
46c95d0
  print("started")
46c95d0
end;
46c95d0
46c95d0
foundJvms = posix.dir(jvmdir);
46c95d0
if (foundJvms == nil) then
46c95d0
  if (debug) then
46c95d0
    print("no, or nothing in "..jvmdir.." exit")
46c95d0
  end;
46c95d0
  return
46c95d0
end
46c95d0
46c95d0
if (debug) then
46c95d0
  print("found "..#foundJvms.."jvms")
46c95d0
end;
46c95d0
46c95d0
for i,p in pairs(foundJvms) do
46c95d0
-- regex similar to %{_jvmdir}/%{name}-%{javaver}*%{_arch} bash command
46c95d0
  if (string.find(p, name.."%-"..javaver..".*"..arch) ~= nil ) then
46c95d0
    if (debug) then
46c95d0
      print("matched:  "..p)
46c95d0
    end;
46c95d0
    if (currentjvm ==  p) then
46c95d0
      if (debug) then
46c95d0
        print("this jdk is already installed. exiting lua script")
46c95d0
      end;
46c95d0
      return
46c95d0
    end ;
46c95d0
    table.insert(jvms, p)
46c95d0
  else
46c95d0
    if (debug) then
46c95d0
      print("NOT matched:  "..p)
46c95d0
    end;
46c95d0
  end
46c95d0
end
46c95d0
46c95d0
if (#jvms <=0) then 
46c95d0
  if (debug) then
46c95d0
    print("no matching jdk in "..jvmdir.." exit")
46c95d0
  end;
46c95d0
  return
46c95d0
end;
46c95d0
46c95d0
if (debug) then
46c95d0
  print("matched "..#jvms.." jdk in "..jvmdir)
46c95d0
end;
46c95d0
46c95d0
--full names are like java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64
46c95d0
table.sort(jvms , function(a,b) 
46c95d0
-- version-sort
46c95d0
-- split on non word: . - 
46c95d0
  local l1 = splitToTable(a, "[^%.-]+") 
46c95d0
  local l2 = splitToTable(b, "[^%.-]+") 
46c95d0
  for x = 1, math.min(#l1, #l2) do
46c95d0
    local l1x = tonumber(l1[x])
46c95d0
    local l2x = tonumber(l2[x])
46c95d0
    if (l1x ~= nil and l2x ~= nil)then
46c95d0
--if hunks are numbers, go with them 
46c95d0
      if (l1x < l2x) then return true; end
46c95d0
      if (l1x > l2x) then return false; end
46c95d0
    else
46c95d0
      if (l1[x] < l2[x]) then return true; end
46c95d0
      if (l1[x] > l2[x]) then return false; end
46c95d0
    end
46c95d0
-- if hunks are equals then move to another pair of hunks
46c95d0
  end
46c95d0
return a
46c95d0
46c95d0
end)
46c95d0
46c95d0
if (debug) then
46c95d0
  print("sorted lsit of jvms")
46c95d0
  for i,file in pairs(jvms) do
46c95d0
    print(file)
46c95d0
  end
46c95d0
end
46c95d0
46c95d0
latestjvm = jvms[#jvms]
46c95d0
46c95d0
46c95d0
for i,file in pairs(caredFiles) do
46c95d0
  local SOURCE=jvmdir.."/"..latestjvm.."/"..file
46c95d0
  local DEST=jvmDestdir.."/"..currentjvm.."/"..file
46c95d0
  if (debug) then
46c95d0
    print("going to copy "..SOURCE)
46c95d0
    print("to  "..DEST)
46c95d0
  end;
46c95d0
  local stat1 = posix.stat(SOURCE, "type");
46c95d0
  if (stat1 ~= nil) then
46c95d0
  if (debug) then
46c95d0
    print(SOURCE.." exists")
46c95d0
  end;
46c95d0
  local s = ""
46c95d0
  local dirs = splitToTable(DEST, "[^/]+") 
46c95d0
  for i,d in pairs(dirs) do
46c95d0
    if (i == #dirs) then
46c95d0
      break
46c95d0
    end
46c95d0
    s = s.."/"..d
46c95d0
    local stat2 = posix.stat(s, "type");
46c95d0
    if (stat2 == nil) then
46c95d0
      if (debug) then
46c95d0
        print(s.." does not exists, creating")
46c95d0
      end;
46c95d0
      posix.mkdir(s)
46c95d0
    else
46c95d0
      if (debug) then
46c95d0
        print(s.." exists,not creating")
46c95d0
      end;
46c95d0
    end
46c95d0
  end
46c95d0
-- Copy with -a to keep everything intact
46c95d0
    local exe = "cp".." -ar "..SOURCE.." "..DEST
46c95d0
    if (debug) then
46c95d0
      print("executing "..exe)
46c95d0
    end;    
46c95d0
    os.execute(exe)
46c95d0
  else
46c95d0
    if (debug) then
46c95d0
      print(SOURCE.." does not exists")
46c95d0
    end;
46c95d0
  end
46c95d0
end