#!/bin/env beesh

# BEE_VERSION firefox_esr-78.9.0-2

#export BEE_TMP_TMPDIR=/dev/shm BEE_TMP_BUILDROOT=/dev/shm/bee-root BEE_MAKEFLAGS='-j80'

#SRCURL[0]="https://ftp.mozilla.org/pub/firefox/releases/${PKGVERSION}${PKGEXTRANAME}/source/firefox-${PKGVERSION}${PKGEXTRANAME}.source.tar.xz"

SRCURL[0]="https://beehive.molgen.mpg.de/10529ea05d2a1b46eb239330a3ae5471/firefox-78.9.0esr.source.tar.xz"

mee_configure() {

    . /usr/local/package/lib/node.profile
    . /pkg/rustc-1.50.0-0/profile

    cat > ${S}/mozconfig <<-EOF
        ac_add_options --enable-application=browser
        ac_add_options --disable-necko-wifi
        ac_add_options --enable-official-branding
        ac_add_options --without-system-icu
        ac_add_options --without-system-nspr
        ac_add_options --disable-elf-hack
        ac_add_options --disable-updater
        ac_add_options --disable-crashreporter
        ac_add_options --disable-tests
        ac_add_options --enable-optimize
        mk_add_options MOZ_MAKE_FLAGS="${BEE_MAKEFLAGS}"
        mk_add_options MOZ_OBJDIR=$B
        unset MOZ_TELEMETRY_REPORTING
	EOF
  ${S}/mach configure
}

mee_build() {
  export CARGO_HOME=$B/.cargo
  export CC=gcc CXX=g++
  export MOZBUILD_STATE_PATH=${B}/mozbuild
  ${S}/mach build
}

mee_build_post() {

    # create menu entry

    cat >${B}/firefoxesr.desktop <<-EOF
	[Desktop Entry]
	Version=1.0
	Name=Firefox-ESR-${PKGVERSION[1]}
	GenericName=Web Browser
	Comment=Surf the World Wide Web
	Exec=firefoxesr${PKGVERSION[1]} -ProfileManager %u
	Icon=firefox
	Terminal=false
	Type=Application
	StartupNotify=true
	Categories=Network;WebBrowser;
	EOF
}

mee_install() {
    DESTDIR=$D ${S}/mach install
}

mee_install_post() {
    if [ "${PKGEXTRANAME}" = "esr" ] ; then
        mv -v "${D}${PREFIX}/local/bin/firefox" "${D}${PREFIX}/local/bin/firefox${PKGEXTRANAME}${PKGVERSION[1]}"
    else
        start_cmd install -d ${D}${LIBDIR}/mariux64
        start_cmd mv ${D}${BINDIR}/${PKGNAME} ${D}${LIBDIR}/mariux64/${PKGNAME}

        # install official icon in all sizes

        for size in 16 22 24 32 48 256 ; do
            srcdir=${S}/browser/branding/official
            destdir=${D}${DATADIR}/icons/hicolor/${size}x${size}/apps

            start_cmd mkdir -p ${destdir}
            start_cmd install -m 644 ${srcdir}/default${size}.png ${destdir}/firefox.png
        done
    fi

    # install menu-entry

    start_cmd mkdir -p ${D}${DATADIR}/applications
    start_cmd desktop-file-install --dir ${D}${DATADIR}/applications ${B}/firefoxesr.desktop

	mkdir -pv ${D}/usr/local/bin

	cat > ${D}/usr/local/bin/sbportal <<-EOF
	#!/bin/bash

	export MOZ_APP_LAUNCHER=firefox

	export XAUTHORITY=\${XAUTHORITY:-\$HOME/.Xauthority}
	export ICEAUTHORITY=\${ICEAUTHORITY:-\$HOME/.ICEauthority}
	export HOME=/scratch/local/sap-firefoxesr-\${USER}-home

	mkdir -p \${HOME}
	chmod 0700 \${HOME}

	exec firefox${PKGEXTRANAME}${PKGVERSION[1]} -no-remote https://sbportal.sap.mpg.de/irj/portal/classic
	EOF

	chmod 755 ${D}/usr/local/bin/sbportal
	
	
	
cat <<-EOF >${D}${PREFIX}/local/lib/firefox/defaults/pref/autoconfig.js
  // Any comment. You must start the file with a comment!
pref("general.config.filename", "default_config_mariux.cfg");
pref("general.config.obscure_value", 0);
EOF

  cat <<-EOF >${D}${PREFIX}/local/lib/firefox/default_config_mariux.cfg
// Any comment. You must start the file with a comment!
pref("storage.nfs_filesystem", true);
pref("network.captive-portal-service.enabled",false);

// Don't show WhatsNew on first run after every update
pref("browser.startup.homepage_override.mstone","ignore");

//disable slowStartup Notification
pref("browser.slowStartup.notificationDisabled", true);
pref("browser.slowStartup.maxSamples", 0);
pref("browser.slowStartup.samples", 0);
pref("browser.rights.3.shown", true);
pref("startup.homepage_welcome_url", "");
pref("startup.homepage_welcome_url.additional", "");
pref("startup.homepage_override_url", "");
pref("browser.laterrun.enabled", false);
pref("browser.shell.checkDefaultBrowser", false);

//disable daily pings to Mozilla about extensions and recent startup
lockPref("extensions.getAddons.cache.enabled", false);

//disable sending the URL of the website where a plugin crashed
pref("dom.ipc.plugins.reportCrashURL", false);

//disable auto update for extensions
lockPref("extensions.update.autoUpdateDefault", false);

//disable about:addons' Get Add-ons panel (uses Google-Analytics)
pref("extensions.getAddons.showPane", false); // hidden pref
pref("extensions.webservice.discoverURL", "");

// Don't ask to install the Flash plugin
pref("plugins.notifyMissingFlash", false);

// Disable upload of health reports
lockPref("datareporting.healthreport.uploadEnabled", false);
//disable about:healthreport page (which connects to Mozilla for locale/css+js+json)
lockPref("datareporting.healthreport.about.reportUrl", "data:text/plain,");

// Disable all data upload (Telemetry and FHR)
lockPref("toolkit.telemetry.unified", false);
lockPref("toolkit.telemetry.enabled", false);
lockPref("toolkit.telemetry.server", "");
lockPref("toolkit.telemetry.newProfilePing.enabled", false);
lockPref("toolkit.telemetry.shutdownPingSender.enabled", false);
lockPref("toolkit.telemetry.updatePing.enabled", false);
lockPref("toolkit.telemetry.bhrPing.enabled", false);
lockPref("toolkit.telemetry.firstShutdownPing.enabled", false);
lockPref("toolkit.telemetry.cachedClientID", "");
lockPref("toolkit.telemetry.archive.enabled", false);
lockPref("datareporting.policy.dataSubmissionEnabled", false);
lockPref("browser.ping-centre.telemetry", false);

//disable "Snippets" (Mozilla content shown on about:home screen)
defaultPref("browser.aboutHomeSnippets.updateUrl", "https://127.0.0.1"); //test

//disable experiments https://wiki.mozilla.org/Telemetry/Experiments
defaultPref("experiments.enabled", false);
defaultPref("experiments.manifest.uri", "");
defaultPref("experiments.supported", false);
defaultPref("experiments.activeExperiment", false);
// disable Mozilla permission to silently opt you into tests
defaultPref("network.allow-experiments", false);

/* 0206: disable geographically specific results/search engines e.g. "browser.search.*.US"
 * i.e. ignore all of Mozilla's various search engines in multiple locales ***/
defaultPref("browser.search.geoSpecificDefaults", false);
defaultPref("browser.search.geoSpecificDefaults.url", "");

/* 0503: disable Normandy/Shield [FF60+]
 * Shield is an telemetry system (including Heartbeat) that can also push and test "recipes"
 * [1] https://wiki.mozilla.org/Firefox/Shield
 * [2] https://github.com/mozilla/normandy ***/
defaultPref("app.normandy.enabled", false);
defaultPref("app.normandy.api_url", "");

/* 0601: disable link prefetching
 * [1] https://developer.mozilla.org/docs/Web/HTTP/Link_prefetching_FAQ ***/
defaultPref("network.prefetch-next", false);
/* 0602: disable DNS prefetching
 * [1] https://www.ghacks.net/2013/04/27/firefox-prefetching-what-you-need-to-know/
 * [2] https://developer.mozilla.org/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control ***/
defaultPref("network.dns.disablePrefetch", true);
defaultPref("network.dns.disablePrefetchFromHTTPS", true); // [HIDDEN PREF ESR] [DEFAULT: true FF70+]
/* 0603: disable predictor / prefetching ***/
defaultPref("network.predictor.enabled", false);
defaultPref("network.predictor.enable-prefetch", false); // [FF48+]

/* 0320: disable about:addons' Recommendations pane (uses Google Analytics) ***/
defaultPref("extensions.getAddons.showPane", false); // [HIDDEN PREF]

//disable pocket
defaultPref("extensions.pocket.enabled", false);

//disable flyweb https://flyweb.github.io/
pref("dom.flyweb.enabled", false);

//disable Shield https://wiki.mozilla.org/Firefox/Shield
lockPref("extensions.shield-recipe-client.enabled", false);
lockPref("extensions.shield-recipe-client.api_url", "");

//disable Follow on Search and Activity Stream
defaultPref("browser.newtabpage.activity-stream.enabled", false);
defaultPref("browser.library.activity-stream.enabled", false);

//disable link-mouseover opening connection to linked server
lockPref("network.http.speculative-parallel-limit", 0);

//disable pings (but enforce same host in case)
lockPref("browser.send_pings", false);
lockPref("browser.send_pings.require_same_host", true);

//disable location bar using search - PRIVACY
defaultPref("keyword.enabled", false);

//disable location bar domain guessing - PRIVACY/SECURITY
defaultPref("browser.fixup.alternate.enabled", false);

//display all parts of the url in the location bar - helps SECURITY
lockPref("browser.urlbar.trimURLs", false);

//disable location bar making speculative connections (FF56+)
lockPref("browser.urlbar.speculativeConnect.enabled", false);

//disable SSL session tracking test
defaultPref("security.ssl.disable_session_identifiers", true);

//disable SSL Error Reporting
defaultPref("security.ssl.errorReporting.automatic", false);
defaultPref("security.ssl.errorReporting.enabled", false);
defaultPref("security.ssl.errorReporting.url", "");

//disable the DNT HTTP header, which is essentially USELESS
defaultPref("privacy.donottrackheader.enabled", false);

//set max popups from a single non-click event - default is 20!
defaultPref("dom.popup_maximum", 3);

//limit events that can cause a popup default is "change click dblclick mouseup pointerup notificationclick reset submit touchend"
defaultPref("dom.popup_allowed_events", "click dblclick");

//enable Firefox's built-in PDF reader
defaultPref("pdfjs.disabled", false);

//session restore save time
defaultPref("browser.sessionstore.interval",150000);

//enable legacy extensions
defaultPref("extensions.legacy.enabled",true);

//disable firefox refresh request
defaultPref("browser.disableResetPrompt",true);

//disable firefox hardware acceleration
defaultPref("layers.acceleration.disabled",true);

//enable gfx, boost for X11 forwarding
defaultPref("gfx.xrender.enabled",true);

EOF
}