--    ======================
--    Description
--    ======================
--
--    Logika umożliwiająca instalację ibpakietu
--
--    ======================
--    Parameters
--    ======================
--
--    input.install.trigger                   - wprowadzenie "1" powoduje rozpoczęcie procesu instalacji
--
--    input.data                              - dane wejściowe typu klucz=wartość rozdzielone średnikami np:
--                                              sn=XXXXX;pass=YYYY;soft=ZZZ;
--
--                                              sn    - nr seryjny ibpakietu
--                                              pass  - hasło ibpakietu
--                                              soft  - nazwa softu do zainstalowania
--
--    setting.install.timeout                  - timeout dla procedury aktualizacji (s)
--
--    counter.ibpackage.install.err            - kody błędów związane z aktualizacją ibpakietu
--                                              0 - wszystko OK, można przeprowadzić instalację
--                                              1 - nie wprowadzono danych
--                                              2 - błędny nr seryjny lub hasło ibpakietu
--                                              3 - nie znaleziono softu w bazie
--                                              4 - ibpakiet nie jest uprawniony do instalacji wybranego softu
--                                              5 - soft nie jest aktywny. Nie można go instalować.
--                                              6 - wygasł abonament niezbędny dla instalacji
--                                              7 - licencja ibpakietu została unieważniona (revoked)
--                                              100 - timeout
--
--    counter.ibpackage.serial                - nr seryjny ibpakietu
--
--    counter.ibpackage.password              - hasło ibpakietu
--
--    counter.software.name                   - nazwa softu do instalacji
--
--    counter.os                              - identyfikator systemu operacyjnego
--
--    Logic uses lua language to implement own behaviour
--
--    ======================
--    mandatory variables
--    ======================
--
--    Logic expects following mandatory variables:
--
--    reload.trigger                          - causes reloading lua script
--
--    memcnt                                  - current amount of memory used by lua in bytes
--
--    Logic expects following kv settings:
--
--    LuaScriptPath                           - path to the lua script - must be absolute
--
--    ======================
--    ChangeLog
--    ======================
--
--    2022-06-20 ver 1.0.0
--    # First release
--
--
-- user can use some functions provided by ibmanager.

-- ibmanager provides following functions to use:
--
-- function returns value of required ibmanager variable
-- @param fullName - string - variable name - name of variable of which value must be returned, for example "rs.0.id.255.input.t.0.value"
-- @return         - string or integer - variable value or "nil" if variable not exist or is not readable
--
-- getValue(fullName)


-- function set value of given ibmanager variable
-- @param fullName - string - variable name - name of variable of which value we want to set, for example "rs.0.id.255.input.t.0.value"
-- @param value    - string, int or boolean - value to set
-- @return         - nothing
--
-- setValue(fullName, value)


-- function returns value of required ibmanager variable
-- @param key      - placed in xml logic configuration file:
--                   * as attribute: Name, in Var, RemoteVar and ImportVar elements in the case of stand alone variables
--                   * as concatenation of two attributes: ListName.Postfix in VarListItem, RemoteVarListItem and ImportVarListItem elements
--                     in the case of variables that are placed in lists
-- @return         - string or integer - variable value or "nil" if variable not exist or is not readable
--
-- getLogicValue(key)


-- function set value of given ibmanager variable
-- @param key      - placed in xml logic configuration file:
--                   * as attribute: Name, in Var, RemoteVar and ImportVar elements in the case of stand alone variables
--                   * as concatenation of two attributes: ListName.Postfix in VarListItem, RemoteVarListItem and ImportVarListItem elements
--                     in the case of variables that are placed in lists
-- @param value    - string, int or boolean - value to set
-- @return         - nothing
--
-- setLogicValue(key, value)

-- function returns two sections of kvsettings from xml configuration file
-- returned value is two element table, each of these elements is table too.
-- indices of returned table are strings and equal "instance" and "global"
-- values of returned tables are tables and contain KVsettings for applicable section.
-- nested tables have form key = value, where key is index in nested table and value is value.
-- example: {"instance" = {"ikey1" = "ivalue1", "ikey2" = "gvalue2"}, "global" = {"gkey1" = "gvalue1", "gkey2" = "gvalue2", "gkey3" = "gvalue3"}}
-- @return          - two dimensional array - kvsettings for global and instance sections
-- getKvSettings()


-- function schedules alert to send.
-- rules are defined in separated alert configuration files and are described in ibmanager instruction manual
-- @param id - alert identifier - must be defined in current logic configuration file in section: <Alert Id="any_identifier" ...
-- scheduleAlert(id)

-- function cancels alert sending, if was previously scheduled. if not then only wakes up alerts handling thread, so if there is no need to call this function, then do not call it.
-- rules are defined in separated alert configuration files and are described in ibmanager instruction manual
-- @param id - alert identifier - must be defined in current logic configuration file in section: <Alert Id="any_identifier" ...
-- cancelAlert(name)

-- function logs message to logfile
-- @param logLevel - loglevel avaliable values:
--                   * LogLevel.TraceLo
--                   * LogLevel.Trace
--                   * LogLevel.TraceHi
--                   * LogLevel.DebugLo
--                   * LogLevel.Debug
--                   * LogLevel.DebugHi
--                   * LogLevel.Info
--                   * LogLevel.Notice
--                   * LogLevel.Warning
--                   * LogLevel.Error
--                   * LogLevel.Critical
-- @param value    - string - message to log
-- @return         - 
--
-- log(logLevel, logMessage)

-- getVarList(listName)

-- ibmanager provide following global variables:

-- logic type, (in this case it will always be "Lua") - the same as in logic configuration file in section: <Logic Type="Lua" ...
-- LOGIC_TYPE

-- logic version, the same as in logic configuration file in section: <Logic ... Version="x.y.z" ...
-- LOGIC_VERSION

-- logic sub-type, the same as in logic configuration file in section: <Logic ... SubType="Hysteresis" ...
-- LOGIC_SUBTYPE

-- logic sub-version, the same as in logic configuration file in section: <Logic ... SubVersion="x.y.z" ...
-- LOGIC_SUBVERSION

-- logic instance name - the same as in logic configuration file, in section: <Instance Name="0">
-- LOGIC_INSTANCE_NAME

-- add script directory to package path
package.path = package.path .. ";./logic/scripts/utils/?.lua";

-- use script - without .lua extension - delta class
require("State");
require("DownCounter");

SUPPORTED_SUBLOGIC_TYPE = "Installer";
SUPPORTED_SUBLOGIC_VERSION = "1.0.0";
g_versionChecked = false;

-- entry point to the logic
-- @param firstCall - tells if logic is called first time
-- @return        - nothing

-- jaki os: https://gist.github.com/soulik/82e9d02a818ce12498d1
-- get_os_name(), funtion to return current OS name and architecture
-- Copyright Philippe Fremy 2017

-- This code is based on the following Gist from Soulik (https://gist.github.com/soulik)
-- https://gist.github.com/soulik/82e9d02a818ce12498d1
-- Initial license was unspecified so I am assuming public domain

local function getOS()
    -- Return two strings describing the OS name and OS architecture.
    -- For Windows, the OS identification is based on environment variables
    -- On unix, a call to uname is used.
    -- 
    -- OS possible values: Windows, Linux, Mac, BSD, Solaris
    -- Arch possible values: x86, x86864, powerpc, arm, mips
    -- 
    -- On Windows, detection based on environment variable is limited
    -- to what Windows is willing to tell through environement variables. In particular
    -- 64bits is not always indicated so do not rely hardly on this value.

    local raw_os_name, raw_arch_name = '', ''

    -- LuaJIT shortcut
    if jit and jit.os and jit.arch then
        raw_os_name = jit.os
        raw_arch_name = jit.arch
        -- print( ("Debug jit name: %q %q"):format( raw_os_name, raw_arch_name ) )
    else
        if package.config:sub(1,1) == '\\' then
            -- Windows
            local env_OS = os.getenv('OS')
            local env_ARCH = os.getenv('PROCESSOR_ARCHITECTURE')
            -- print( ("Debug: %q %q"):format( env_OS, env_ARCH ) )
            if env_OS and env_ARCH then
                raw_os_name, raw_arch_name = env_OS, env_ARCH
            end
        else
            -- other platform, assume uname support and popen support
            raw_os_name = io.popen('uname -s','r'):read('*l')
            raw_arch_name = io.popen('uname -m','r'):read('*l')
        end
    end

    raw_os_name = (raw_os_name):lower()
    raw_arch_name = (raw_arch_name):lower()

    -- print( ("Debug: %q %q"):format( raw_os_name, raw_arch_name) )

    local os_patterns = {
        ['windows']     = 'Windows',
        ['linux']       = 'linux',
        ['osx']         = 'Mac',
        ['mac']         = 'Mac',
        ['darwin']      = 'Mac',
        ['^mingw']      = 'Windows',
        ['^cygwin']     = 'Windows',
        ['bsd$']        = 'BSD',
        ['sunos']       = 'Solaris',
    }
    
    local arch_patterns = {
        ['^x86$']           = 'x86',
        ['i[%d]86']         = 'x86',
        ['amd64']           = 'x86_64',
        ['x86_64']          = 'x86_64',
        ['x64']             = 'x86_64',
        ['power macintosh'] = 'powerpc',
        ['^arm']            = 'arm',
        ['^mips']           = 'mips',
        ['i86pc']           = 'x86',
    }

    local os_name, arch_name = 'unknown', 'unknown'

    for pattern, name in pairs(os_patterns) do
        if raw_os_name:match(pattern) then
            os_name = name
            break
        end
    end
    for pattern, name in pairs(arch_patterns) do
        if raw_arch_name:match(pattern) then
            arch_name = name
            break
        end
    end
    --return os_name, arch_name
    return os_name .. "-" .. arch_name
end

g_installTimeoutTimer = nil;
g_os_name = nil;
g_inputData = nil;

g_counterIbpackageSerial = nil;
g_inputIbpackagePassword = nil;
g_counterSoftwareName = nil;

function fileExists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

function lunixArm_checkPermissions()
  local dirName = "/ibsystem-tmp/tmp/installer/" .. LOGIC_INSTANCE_NAME .. "_checkPermissions";
  local fileName = dirName .. "/checkpermissions.sh";
  local logFileName = dirName .. "/getsoftinfo.sh.log";
  local outputFileName = dirName .. "/checkpermissions.out";
  local i = 0;
  local downloadprogress = 0;

  os.execute("mkdir -p " .. dirName)
  os.execute("rm -rf " .. dirName .. "/*")
  os.execute("touch " .. fileName)
  os.execute("chmod 755 " .. fileName)

  setLogicValue("counter.ibpackage.install.err", "100");

  local content =
[[
#!/bin/sh
curl --insecure -u ]] .. g_counterIbpackageSerial .. [[:]] .. g_inputIbpackagePassword .. [[ -H "Accept: application/product+xml" "https://ibsystem.org/getinfo.php?result=permissions&softname=]] .. g_counterSoftwareName .. [[" > ]] .. outputFileName .. [[
]]

  local file = io.open(fileName, "w")
  file:write(content)
  file:close()

  os.execute(fileName .. " > " .. logFileName .. " 2>&1 &")

  g_installTimeoutTimer:reset();

  repeat
    os.execute("sleep 1")

    -- sprawdza postęp z cron'a
    local resFile = io.popen([[sed 's/\r/\n/g' < ]] .. logFileName .. [[ | tail -n 1]])
    local resContent =  assert(resFile:read(_VERSION <= "Lua 5.2" and "*a" or "a"))
    resFile:close()
    i = 0;
    --print(resContent)
    for value in string.gmatch(resContent, "%S+") do
      -- print(i, value)
      if (i == 0) then
        downloadprogress = value
      end
      i = i + 1;
    end

    -- print(downloadprogress)

    -- obsługuje timeouta z logiki
    if g_installTimeoutTimer:elapsed() then
      log(LogLevel.Info, "Permissions checkout timeout!")
      return;
    end

  until tonumber(downloadprogress) == 100

  local fh = assert(io.open(outputFileName, "rb"))
  local resContent = assert(fh:read(_VERSION <= "Lua 5.2" and "*a" or "a"))
  fh:close()

  -- parsuje wartości key=value rozdzielone średnikiem (;)
  for key, value in string.gmatch(resContent, "(%S-)%s*=%s*(.-);") do
    if (key == 'err') then
      if (value == '8') then value = '0' end
      setLogicValue("counter.ibpackage.install.err", value);
    end
    --print(key .. " -> " .. value)
  end

end

function lunixArm_install()
  local dirName = "/ibsystem-tmp/tmp/installer/" .. LOGIC_INSTANCE_NAME .. "_install";
  local fileName = dirName .. "/exeinstall.sh";
  local logFileName = dirName .. "/exeinstall.sh.log";
  local outputFileName = dirName .. "/install.tar.gz";
  local i = 0;
  local downloadprogress = 0;

  os.execute("mkdir -p " .. dirName)
  os.execute("rm -rf " .. dirName .. "/*")
  os.execute("touch " .. fileName)
  os.execute("chmod 755 " .. fileName)

  -- skrypt zapisujący główną paczkę instalacyjną
  local content =
[[
#!/bin/sh
curl --insecure -u ]] .. g_counterIbpackageSerial .. [[:]] .. g_inputIbpackagePassword .. [[ -H "Accept: application/product+xml" "https://ibsystem.org/getpackage.php?soft=]] .. g_counterSoftwareName .. [[&system=]] .. g_os_name .. [[" --output "]] .. outputFileName .. [["
]]

  local file = io.open(fileName, "w")
  file:write(content)
  file:close()

  os.execute(fileName .. " > " .. logFileName .. " 2>&1 &")

  g_installTimeoutTimer:reset();

  repeat
    os.execute("sleep 1")

    -- sprawdza postęp z cron'a
    local resFile = io.popen([[sed 's/\r/\n/g' < ]] .. logFileName .. [[ | tail -n 1]])
    local resContent =  assert(resFile:read(_VERSION <= "Lua 5.2" and "*a" or "a"))
    resFile:close()
    i = 0;
    --print(resContent)
    for value in string.gmatch(resContent, "%S+") do
      -- print(i, value)
      if (i == 0) then
        downloadprogress = value
      end
      i = i + 1;
    end

    -- obsługuje timeouta z logiki
    if g_installTimeoutTimer:elapsed() then
      log(LogLevel.Info, "Install timeout!")
      return;
    end

  until tonumber(downloadprogress) == 100

  outputFileName = dirName .. "/install.sh";

  -- skrypt zapisujący skrypt instalacji
  local content =
[[
#!/bin/sh
curl --insecure -u ]] .. g_counterIbpackageSerial .. [[:]] .. g_inputIbpackagePassword .. [[ -H "Accept: application/product+xml" "https://ibsystem.org/getpackage.php?soft=install.sh&system=]] .. g_os_name .. [[" --output "]] .. outputFileName .. [[" && chmod 0775 ]] .. outputFileName .. [[
]]

  local file = io.open(fileName, "w")
  file:write(content)
  file:close()

  os.execute(fileName .. " > " .. logFileName .. " 2>&1 &")

  repeat
    os.execute("sleep 1")

    -- sprawdza postęp z cron'a
    local resFile = io.popen([[sed 's/\r/\n/g' < ]] .. logFileName .. [[ | tail -n 1]])
    local resContent =  assert(resFile:read(_VERSION <= "Lua 5.2" and "*a" or "a"))
    resFile:close()
    i = 0;
    --print(resContent)
    for value in string.gmatch(resContent, "%S+") do
      -- print(i, value)
      if (i == 0) then
        downloadprogress = value
      end
      i = i + 1;
    end

    -- obsługuje timeouta z logiki
    if g_installTimeoutTimer:elapsed() then
      log(LogLevel.Info, "Install timeout!")
      return;
    end

  until tonumber(downloadprogress) == 100

  os.execute("sleep 1")

  os.execute("rm -rf /ibsystem-tmp/install.tar.gz")
  os.execute("cp " .. dirName .. "/install.tar.gz /ibsystem-tmp/install.tar.gz")

  os.execute("rm -rf /ibsystem-tmp/install.sh")
  os.execute("cp " .. outputFileName .. " /ibsystem-tmp/install.sh")

  os.execute("/ibsystem-tmp/install.sh > /ibsystem-tmp/install.log 2>&1 &")

end

function parseData()
  --print("g_inputData: " .. g_inputData)

  g_counterIbpackageSerial = "";
  g_inputIbpackagePassword = "";
  g_counterSoftwareName = "";

  -- parsuje wartości key=value rozdzielone średnikiem (;)
  for key, value in string.gmatch(g_inputData, "(%S-)%s*=%s*(.-);") do
    --print(key .. " -> " .. value)
    if (key == "sn") then
      setLogicValue("counter.ibpackage.serial", value);
      g_counterIbpackageSerial = value;
    elseif (key == "pass") then
      setLogicValue("counter.ibpackage.password", value);
      g_inputIbpackagePassword = value;
    elseif (key == "soft") then
      setLogicValue("counter.software.name", value);
      g_counterSoftwareName = value;
    end
  end

    if (g_os_name == 'linux-arm') then
      lunixArm_checkPermissions();
    else
      log(LogLevel.Error, "unsupported OS: " .. g_os_name)
    end

end

function onLogicCall(firstCall)

  if not g_versionChecked then
    -- checking sublogic type and sublogic version
    if LOGIC_SUBTYPE ~= SUPPORTED_SUBLOGIC_TYPE then
      error("Wrong logic sub-type. expected " .. SUPPORTED_SUBLOGIC_TYPE .. " but used " .. LOGIC_SUBTYPE);
    end
    local versionWithoutBuild = string.match(LOGIC_SUBVERSION, "[0-9]+%.[0-9]+%.[0-9]+");
    if versionWithoutBuild ~= SUPPORTED_SUBLOGIC_VERSION then
      error("Wrong logic sub-version. expected " .. SUPPORTED_SUBLOGIC_VERSION .. " but used " .. LOGIC_SUBVERSION);
    end
    g_versionChecked = true;
  end

  local inputInstallTrigger = getLogicValue("input.install.trigger");
  local inputData = getLogicValue("input.data");

  g_settingUpdateTimeout = getLogicValue("setting.install.timeout");

-- timers

  if g_installTimeoutTimer == nil or firstCall then
    g_installTimeoutTimer = DownCounter.create();
  end
  g_installTimeoutTimer:updateParams(g_settingUpdateTimeout * 1000);

  if g_os_name == nil or firstCall then
    g_os_name = getOS()
  end

  if (inputData ~= "") and (g_inputData ~= inputData) then
    g_inputData = inputData
    parseData();
  end

  if (inputInstallTrigger == 1) then
    g_inputData = inputData
    parseData();
    setLogicValue("input.install.trigger", 0);


    if (getLogicValue("counter.ibpackage.install.err") == 0) then

      if (g_os_name == 'linux-arm') then
        g_installTimeoutTimer:reset();
        lunixArm_install();
      else
        log(LogLevel.Error, "unsupported OS: " .. g_os_name)
      end

    end

  end

  -- send logic variables to ibmanager
  setLogicValue("counter.os", g_os_name);

end
