# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# generate AutoInstall files from gbuild data for further scp2 processing

import sys

module = sys.argv[1]
scp2componentcondition = sys.argv[2]
scp2libtemplate = sys.argv[3]
scp2exetemplate = sys.argv[4]
scp2jartemplate = sys.argv[5]
scp2pkgtemplate = sys.argv[6]
# use 'with open(file) as f:' to avoid 'ResourceWarning: unclosed file'
with open(sys.argv[7]) as f:
    sdklibs = f.readline().split()
with open(sys.argv[8]) as f:
    libs = f.readline().split()
with open(sys.argv[9]) as f:
    exes = f.readline().split()
with open(sys.argv[10]) as f:
    jars = f.readline().split()
with open(sys.argv[11]) as f:
    pkgs = f.readline().split()

if len(scp2componentcondition) > 0:
    scp2componentcondition = "," + scp2componentcondition

def escape(string):
    return string.replace(".", "_").replace("-", "_").replace("/", "_")

def to_tuple(l):
    ret = []
    i = 0
    while i < len(l):
        ret.append((l[i], l[i+1]))
        i += 2
    return ret

def to_triple(l):
    ret = []
    i = 0
    while i < len(l):
        ret.append((l[i], l[i+1], l[i+2]))
        i += 3
    return ret

print("/* autogenerated installs for group " + module + " */")
print("#define auto_" + module + "_ALL \\")

autosdklibs = [("auto_" + module + "_link_" + escape(lib),link,target) for (lib,link,target) in to_triple(sdklibs)]
autolibs = [("auto_" + module + "_lib_" + escape(lib),libfile) for (lib,libfile) in to_tuple(libs)]
autoexes = [("auto_" + module + "_exe_" + escape(exe),exefile) for (exe,exefile) in to_tuple(exes)]
autojars = [("auto_" + module + "_jar_" + escape(jar),jar + ".jar") for jar in jars]
autopkgs = [("auto_" + module + "_pkg_" + escape(pkg),pkg + ".filelist") for pkg in pkgs]

allgids = [gid for (gid,_,_) in autosdklibs] + \
          [gid for (gid,_) in autolibs] + \
          [gid for (gid,_) in autoexes] + \
          [gid for (gid,_) in autojars] + \
          [gid for (gid,_) in autopkgs]

print(", \\\n".join(["    " + gid for gid in allgids]))

for (gid, link, target) in autosdklibs:
    print("SDK_LIBRARY_LINK(" + gid + "," + link + "," + target + ")")

scp2libtemplates = set([ "URE_PRIVATE_LIB", "LIBO_LIB_FILE", "LIBO_LIB_FILE_BINARYTABLE", "LIBO_LIB_FILE_COMPONENTCONDITION", "SHLXTHDL_LIB_FILE", "SHLXTHDL_X64_LIB_FILE_COMPONENTCONDITION" ])
for (gid, libfile) in autolibs:
    if not(scp2libtemplate in scp2libtemplates):
        raise Exception("invalid scp2libtemplate \"" + scp2libtemplate + "\"")
    print(scp2libtemplate + "(" + gid + "," + libfile + scp2componentcondition + ")")

scp2exetemplates = set([ "URE_EXECUTABLE", "LIBO_EXECUTABLE", "LIBO_EXECUTABLE_COMPONENTCONDITION", "SDK_EXECUTABLE" ])
for (gid, exefile) in autoexes:
    if not(scp2exetemplate in scp2exetemplates):
        raise Exception("invalid scp2exetemplate \"" + scp2exetemplate + "\"")
    print(scp2exetemplate + "(" + gid + "," + exefile + scp2componentcondition + ")")

scp2jartemplates = set([ "URE_JAR_FILE", "LIBO_JAR_FILE" ])
for (gid, jarfile) in autojars:
    if not(scp2jartemplate in scp2jartemplates):
        raise Exception("invalid scp2jartemplate \"" + scp2jartemplate + "\"")
    print(scp2jartemplate + "(" + gid + "," + jarfile + scp2componentcondition + ")")

scp2pkgtemplates = set([ "PACKAGE_FILELIST", "PACKAGE_FILELIST_COMPONENTCONDITION","PACKAGE_FILELIST_FONT", "SDK_PACKAGE_FILELIST" ])
for (gid, pkgfilelist) in autopkgs:
    if not(scp2pkgtemplate in scp2pkgtemplates):
        raise Exception("invalid scp2pkgtemplate \"" + scp2pkgtemplate + "\"")
    print(scp2pkgtemplate + "(" + gid + "," + pkgfilelist + scp2componentcondition + ")")

# vim:set shiftwidth=4 softtabstop=4 expandtab: