#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU General Public License, version 3.
# See the file http://www.gnu.org/licenses/gpl.txt

from pisi.actionsapi import autotools
from pisi.actionsapi import pisitools
from pisi.actionsapi import shelltools
from pisi.actionsapi import get

WorkDir = "chromium-%s" % get.srcVERSION()

shelltools.export("HOME", get.workDIR())

ARCH = "x64"

def setup():
    shelltools.export("LC_ALL", "C")
    shelltools.export("JAVA_CMD", "/usr/bin/java_8")
    shelltools.system("mkdir -p third_party/node/linux/node-linux-x64/bin")
    shelltools.system("ln -s /usr/bin/node third_party/node/linux/node-linux-x64/bin/")

    #for LIB in ["freetype", "flac", "ffmpeg", "fontconfig", "harfbuzz-ng", "libdrm", "libjpeg", "libxml" ,"libxslt", "libwebp", "opus", "re2", "snappy", "yasm"]:
        #shelltools.system('find -type f -path "*third_party/$LIB/*" \! -path "*third_party/$LIB/chromium/*" \! -path "*third_party/$LIB/google/*" \! -regex ".*\.\(gn\|gni\|isolate\|py\)" -delete')
	
	
    #shelltools.system("build/linux/unbundle/replace_gn_files.py --system-libraries flac ffmpeg fontconfig freetype harfbuzz-ng libdrm libjpeg libxml libxslt libwebp opus re2 snappy yasm")
    
    shelltools.system("sed -i -e 's/\<xmlMalloc\>/malloc/' -e 's/\<xmlFree\>/free/' \
                       third_party/blink/renderer/core/xml/*.cc \
                       third_party/blink/renderer/core/xml/parser/xml_document_parser.cc \
					   third_party/libxml/chromium/libxml_utils.cc")

    opt = 'custom_toolchain="//build/toolchain/linux/unbundle:default" \
           host_toolchain="//build/toolchain/linux/unbundle:default" \
           use_sysroot=false \
           enable_nacl=true \
           enable_nacl_nonsfi=true \
           rtc_use_pipewire=true \
           use_custom_libcxx=true \
           clang_use_chrome_plugins=false \
           is_official_build=true \
           fieldtrial_testing_like_official_build=true \
           clang_use_chrome_plugins=false \
           fatal_linker_warnings=false \
           treat_warnings_as_errors=false \
           use_gnome_keyring=false\
           use_gold=false \
           enable_hangout_services_extension=true \
           enable_widevine=true \
           linux_use_bundled_binutils=false \
           is_debug=false \
           ffmpeg_branding="Chrome" \
           google_default_client_secret="0ZChLK6AxeA3Isu96MkwqDR4" \
           google_api_key="AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM" \
           google_default_client_id="413772536636.apps.googleusercontent.com" \
           remove_webcore_debug_symbols=true \
           proprietary_codecs=true \
           link_pulseaudio=true \
           enable_swiftshader=false \
           use_vaapi=true \
           closure_compile=false \
           symbol_level=0'
           
        
    #clangpath = "%s/chromium-%s/third_party/llvm-build/Release+Asserts/bin/" %(get.workDIR(), get.srcVERSION())

    shelltools.export("CC", "clang" )
    shelltools.export("CXX", "clang++" )
    shelltools.export("AR", "ar" )
    shelltools.export("NM", "nm" )
    shelltools.export("RANLIB", "ranlib" )
    
    pisitools.cflags.add("-Wno-builtin-macro-redefined -Wno-unknown-warning-option")
    pisitools.cxxflags.add("-Wno-builtin-macro-redefined -Wno-unknown-warning-option")
    pisitools.ldflags.add(" -fuse-ld=lld")
    shelltools.export("CPPFLAGS", "-D__DATE__=  -D__TIME__=  -D__TIMESTAMP__=")
    
    shelltools.system("/usr/bin/python3 build/download_nacl_toolchains.py --packages nacl_x86_newlib,pnacl_newlib,pnacl_translator sync --extract")
    #shelltools.system("/usr/bin/python3 tools/clang/scripts/update.py")
    shelltools.system("/usr/bin/python3 tools/gn/bootstrap/bootstrap.py --gn-gen-args '%s'"% opt)
    shelltools.system("out/Release/gn gen out/Release --args='%s'"% opt)


def build():
    #Sandbox for error must remain separate
    shelltools.system("ninja -C out/Release chrome")
    shelltools.system("ninja -C out/Release chrome_sandbox")
    shelltools.system("ninja -C out/Release chromedriver")
    #shelltools.system("ninja -C out/Release widevinecdmadapter")

def install():
    shelltools.cd("out/Release")

    #should be checked should for the missing folder "out/Release"
    for vla in ["*.pak", "*.json", "chrome", "locales", "resources", "icudtl.dat", "mksnapshot", "chromedriver", "snapshot_blob.bin", "character_data_generator", \
			    "libEGL.so", "libGLESv2.so", "libVk*.so", "v8_context_snapshot.bin", "MEIPreload", "nacl_helper", "nacl_helper_bootstrap", "nacl_helper_nonsfi", "nacl_irt_x86_64.nexe"]:
        pisitools.insinto("/usr/lib/chromium-browser", "%s" % vla)

    pisitools.insinto("/usr/lib/chromium-browser", "chrome_sandbox", "chrome-sandbox")
    pisitools.dosym("/usr/lib/chromium-browser/chrome", "/usr/bin/chromium-browser")
 
    shelltools.system("chmod -v 4755 %s/usr/lib/chromium-browser/chrome-sandbox" %get.installDIR())

    #pisitools.newman("chrome.1", "chromium-browser.1")

    shelltools.cd("../..")
    for size in ["24", "48", "64", "128", "256"]:
        pisitools.insinto("/usr/share/icons/hicolor/%sx%s/apps" %(size, size), "chrome/app/theme/chromium/product_logo_%s.png" % size, "chromium-browser.png")

    pisitools.dosym("/usr/share/icons/hicolor/256x256/apps/chromium-browser.png", "/usr/share/pixmaps/chromium-browser.png")

    pisitools.dodoc("LICENSE")
