#!/bin/env beesh

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

# BEE_VERSION firefox-85.0.1-0

#https://ftp.mozilla.org/pub/firefox/releases/$pkgversion/SOURCE to find the right download link
SRCURL[0]="https://ftp.mozilla.org/pub/firefox/releases/${PKGVERSION}/source/firefox-${PKGVERSION}.source.tar.xz"

# PATCHURL+=()

#BEE_BUILDTYPE=autotools
#build_in_sourcedir

mee_configure() {

    . /pkg/rustc-1.47.0-0/profile

    cat > ${S}/mozconfig <<-EOF
	ac_add_options --prefix=/usr
	ac_add_options --enable-application=browser
	ac_add_options --disable-necko-wifi
	ac_add_options --enable-official-branding
	ac_add_options --enable-system-pixman
	ac_add_options --without-system-icu
	ac_add_options --without-system-nspr
	ac_add_options --disable-tests
	ac_add_options --enable-optimize
	ac_add_options --disable-crashreporter
	ac_add_options --disable-updater
	mk_add_options MOZ_MAKE_FLAGS="${BEE_MAKEFLAGS}"
	mk_add_options MOZ_OBJDIR=$B
	EOF
    export MOZBUILD_STATE_PATH=${B}/mozbuild

}

mee_build() {
  export CARGO_HOME=$B/.cargo
  export MACH_USE_SYSTEM_PYTHON=1
  ${S}/mach build
}

mee_build_post() {

    # create menu entry

    cat >${B}/firefox.desktop <<-EOF
	[Desktop Entry]
	Encoding=UTF-8
	Version=1.0
	Name=Firefox
	GenericName=Web Browser
	Comment=Surf the World Wide Web
	Exec=firefox %u
	Icon=firefox
	Terminal=false
	Type=Application
	MimeType=text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;x-scheme-handler/http;x-scheme-handler/https;
	StartupNotify=true
	Categories=Network;WebBrowser;
	EOF
}

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

mee_install_post() {
    start_cmd install -d ${D}${LIBDIR}/mariux64
    start_cmd rm ${D}${BINDIR}/${PKGNAME}

    cat >${D}${LIBDIR}/mariux64/firefox<<EOF
NSS_SDB_USE_CACHE=1 exec "${LIBDIR}/firefox/firefox" "\$@"
EOF

chmod 755 ${D}${LIBDIR}/mariux64/firefox

    # 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

    # install menu-entry

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

  cat <<-EOF >${D}${LIBDIR}/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}${LIBDIR}/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", "");

// Set default homepage - users can change
// Requires a complex preference
defaultPref("browser.startup.homepage","data:text/plain,browser.startup.homepage=https://intranet.molgen.mpg.de/");

// 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);
EOF
}