#!/bin/bash
#
#    Copyright (C) 2022  Luis Guzmán <ark@switnet.org>
#    Copyright (C) 2020  Ruben Rodriguez <ruben@trisquel.info>
#    Copyright (C) 2019  David Trudgian <dave@trudgian.net>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

VERSION=18

. ./config

# Copy in our local Trisquel branding files.
cp $DATA/branding/* comm/mail/branding/nightly/ -r
cp $DATA/branding/* comm/mail/branding/thunderbird/ -r
#for i in aurora nightly official unofficial; do
#  cp $DATA/branding/* browser/branding/$i/ -r
#done

# Remove .gitignore call as those files get cleaned out on dpkg tarball by default.
remove_gitignore_from_json(){
    jq . ${1} |sed '/.gitignore/d'|awk -v RS= '{$1=$1}1'|tr -d ' '|tee ${1}.new
    mv ${1}.new ${1}
}

for json in $(find -name \.cargo-checksum.json|xargs grep -rl gitignore)
do
    remove_gitignore_from_json $json
done

# Privacy and branding via prefs
cat << EOF >> debian/vendor.js

// Trisquel settings
pref("app.releaseNotesURL", "http://trisquel.info/mailclient");
pref("app.vendorURL", "http://trisquel.info/mailclient");

// PFS url
pref("pfs.datasource.url", "https://trisquel.info/sites/pfs.php?mime=%PLUGIN_MIMETYPE%");
pref("pfs.filehint.url", "https://trisquel.info/sites/pfs.php?mime=%PLUGIN_MIMETYPE%");

// DuckDuckGo
pref("keyword.URL", "https://duckduckgo.com/?t=trisquel&q=!+");
pref("browser.search.defaultenginename", "DuckDuckGo");
pref("browser.search.order.1", "DuckDuckGo");
pref("browser.search.defaultenginename", "DuckDuckGo");
pref("browser.search.showOneOffButtons", false);
pref("browser.search.suggest.enabled",false);

// Disable third party cookies
pref("network.cookie.cookieBehavior", 1);

// Extensions cannot be updated without permission
pref("extensions.update.enabled", false);
pref("extensions.systemAddon.update.enabled", false);
pref("lightweightThemes.update.enabled", false);

// Don't show suggested provider list
pref("mail.provider.enabled", false);
pref("mail.provider.providerList", "");
pref("mail.provider.suggestFromName", "");

// Don't prompt if not default mail client
pref("mail.shell.checkDefaultClient", false);

// Preferences for the Get Add-ons panel
pref("extensions.webservice.discoverURL", "https://directory.fsf.org/wiki/Icedove");
pref("extensions.getAddons.search.url", "https://trisquel.info");
pref("extensions.blocklist.enabled", false);

// Dictionary download preference
pref("spellchecker.dictionaries.download.url", "http://dictionaries.mozdev.org/installation.html");

// Privacy protections
pref("mail.rights.version", 1);
pref("toolkit.telemetry.prompted", 2);
pref("toolkit.telemetry.rejected", true);
pref("mailnews.start_page.enabled", false);
pref("mailnews.start_page.url", "");
pref("mailnews.start_page.override_url", "");
pref("app.update.auto", false);
pref("app.update.enabled", false);
pref("browser.search.update", false);

pref("app.update.url.manual", "https://trisquel.info/wiki/icedove");
pref("mail.cloud_files.inserted_urls.footer.link", "https://trisquel.info/wiki/icedove");
pref("mail.cloud_files.learn_more_url", "https://trisquel.info/wiki/icedove");
pref("mail.ignore_thread.learn_more_url", "https://trisquel.info/wiki/icedove");
pref("mail.pgpmime.addon_url", "https://enigmail.net/");
pref("app.support.baseURL", "https://trisquel.info/wiki/icedove");
pref("app.update.url.manual", "https://trisquel.info/wiki/icedove");
pref("extensions.getAddons.link.url", "https://trisquel.info/en/browser-plain");
pref("extensions.getAddons.search.browseURL", "https://trisquel.info/en/browser-plain?keys=%TERMS%&tid=All");
pref("extensions.getAddons.search.url", "https://trisquel.info/en/browser-plain?keys=%TERMS%&tid=All");
EOF

# DDG as default search engine
sed 's/?q=/?t=trisquel&q=/' -i comm/mail/components/search/extensions/ddg/manifest.json
sed '/searchDefault/s/: \".*\"/: "DuckDuckGo"/g' -i comm/mail/components/search/extensions/list.json
sed '/visibleDefaultEngines/{n; s/"ddg",//; s/\"/"ddg", "/}'  -i comm/mail/components/search/extensions/list.json

# ####################################################
# Privacy / branding brought in from abrowser for 60.x
#
# captive portal page
sed '/captivedetect.canonicalURL/s/http.*.html//' -i modules/libpref/init/all.js
# do not alter useragent/platform/oscpu/etc with fingerprinting countermeasure, it makes things worse
sed '/ShouldResistFingerprinting/,/}/s/^/\/\//' -i ./netwerk/protocol/http/nsHttpHandler.cpp
sed '/If fingerprinting resistance is on/,/}/s/^/\/\//' -i ./dom/base/Navigator.cpp
# Disable healtreport
cat << EOF > toolkit/components/telemetry/healthreport-prefs.js
pref("datareporting.healthreport.uploadEnabled", false);
pref("datareporting.healthreport.about.reportUrl", "https://trisquel.info/legal");
pref("datareporting.healthreport.infoURL", "https://trisquel.info/legal");
EOF
#cp toolkit/components/telemetry/healthreport-prefs.js mobile/android/chrome/content/healthreport-prefs.js
# Browser legal page
sed 's%https://www.mozilla.org/legal/privacy/%https://trisquel.info/legal%' -i ./browser/app/profile/firefox.js ./toolkit/content/aboutRights.xhtml
sed -i 's|https://www.mozilla.org/thunderbird/legal/privacy/|https://trisquel.info/legal|' comm/mail/app/profile/all-thunderbird.js
sed -i 's|https://www.mozilla.org/en-US/privacy/thunderbird/|https://trisquel.info/legal|' comm/mail/app/profile/all-thunderbird.js
sed -i 's|https://www.mozilla.org/privacy/|https://trisquel.info/legal|' comm/mail/components/newmailaccount/content/accountProvisioner.xhtml
grep -rl https://www.mozilla.org/privacy/thunderbird/ | xargs sed -i 's|https://www.mozilla.org/privacy/thunderbird/|https://trisquel.info/legal|g'
# Org branding
sed 's/com.ubuntu/org.trisquel/' -i debian/config/mozconfig.in
# Disable DRM support, armhf has no such option.
## Add other archs that might present the same lack of disable-eme option
cat << ARCH >> debian/config/mozconfig.in
%%if DEB_HOST_ARCH != armhf
%%if DEB_HOST_ARCH != arm64
%%if DEB_HOST_ARCH != ppc64el
ac_add_options --disable-eme
%%endif
%%endif
%%endif
ARCH
########################################################
sed '/^MOZ_PKG_NAME/s/.*/MOZ_PKG_NAME=icedove/' -i debian/build/config.mk

cat << EOF >> debian/build/config.mk

# Trisquel settings
MOZ_FORCE_UNOFFICIAL_BRANDING = 1
MOZ_WANT_UNIT_TESTS = 0
MOZ_ENABLE_BREAKPAD = 0
EOF

# Replace Thunderbird branding
find -type d -name '*thunderbird*' | xargs rename s/thunderbird/icedove/
find -type f -name '*thunderbird*' | xargs rename s/thunderbird/icedove/

SEDSCRIPT="
s|Search addons.mozilla.org|Search|g;
s/Mozilla Thunderbird/Icedove/g;
s/thunderbird/icedove/g;
s/Thunderbird/Icedove/g;
s/THUNDERBIRD/ICEDOVE/g;
s/ Mozilla / Trisquel /g;
s/Firefox/Abrowser/g;
s|PACKAGES/icedove|PACKAGES/thunderbird|g;
s/iceweasel, icedove/iceweasel, thunderbird/g;
s/Replaces: icedove/Replaces: thunderbird/g;
s|www.mozilla.com/icedove/central|trisquel.info/browser|g;
s|mozilla.com/plugincheck|trisquel.info/browser|g;
s|www.mozilla.com/legal/privacy|trisquel.info/legal|g;

s/Trisquel Public/Mozilla Public/g;
s/Trisquel Foundation/Mozilla Foundation/g;
s/Trisquel Corporation/Mozilla Corporation/g;
"
echo "Running batch replace operation"
find . -type f -not -iregex '.*changelog.*' -not -iregex '.*copyright.*' -not -iregex '.*third_party/rust.*' -execdir /bin/sed --follow-symlinks -i "$SEDSCRIPT" '{}' ';'

sed 's|https://www.icedove.net/features/|https://trisquel.info/wiki/icedove|' -i comm/mail/base/content/utilityOverlay.js
sed 's|https://www.icedove.net/get-involved/|https://trisquel.info/wiki/|' -i comm/mail/base/content/utilityOverlay.js
sed 's|https://give.icedove.net/.*"|https://trisquel.info/donate/"|' -i comm/mail/base/content/utilityOverlay.js
sed 's|https://www.icedove.net/.*/features/|https://trisquel.info/wiki/icedove|' -i comm/mailnews/base/content/msgAccountCentral.xhtml
sed 's|https://give.icedove.net/.*"|https://trisquel.info/donate/"|' -i comm/mailnews/base/content/msgAccountCentral.xhtml
sed 's|https://support.mozilla.org/products/icedove|https://trisquel.info/wiki/icedove|' -i comm/mailnews/base/content/msgAccountCentral.xhtml
sed 's|https://www.icedove.net/.*/get-involved/|https://trisquel.info/wiki/|' -i comm/mailnews/base/content/msgAccountCentral.xhtml
sed 's|https://developer.icedove.net/|https://gitlab.trisquel.org/trisquel|' -i comm/mailnews/base/content/msgAccountCentral.xhtml

replace Daily Trisquel comm/mail/branding/
sed '/.*<description.*communityExperimentalDesc/,/\/description/d' -i ./comm/mail/base/content/aboutDialog.xhtml
sed '/.*<description.*communityDesc/,/\/description/d' -i ./comm/mail/base/content/aboutDialog.xhtml
sed '/.*<description.*contributeDesc/,/\/description/d' -i ./comm/mail/base/content/aboutDialog.xhtml

sed '/^Source/s/.*/Source: thunderbird/' -i debian/control.in
sed 's/Provides: mail-reader,/Provides: mail-reader, thunderbird,/' -i debian/control.in
sed "s/Provides.*/Provides: icedove-locale-@LANGCODE@/" -i debian/control.langpacks

#Manual fixes to allow rebranding processed through hash checks and other changes.
# Restore wrong replacement module name.
grep -rl IcedoveProfileImporter.jsm | xargs sed -i 's|IcedoveProfileImporter.jsm|ThunderbirdProfileImporter.jsm|g'
grep -rl IcedoveImport.jsm | xargs sed -i 's|IcedoveImport.jsm|ThunderbirdImport.jsm|g'
grep -rl IcedoveProfileMigrator.jsm | xargs sed -i 's|IcedoveProfileMigrator.jsm|ThunderbirdProfileMigrator.jsm|g'
#Fix 1 character bigger than allowed yml line (81 > 80)
sed -i 's|Abrowser is not running.  It|Abrowser is not running. It|' toolkit/mozapps/update/pings.yaml

sed -i \
's|91a59d470e0700b4bcb3ff735d06799f3107b8ef4875a2e9904607b164be0326|05f8e055df0e14ee6d50183e701888a49b74fa085dfac9d6032a2f985b5309e6|' \
third_party/dump_syms/vendor/regex/.cargo-checksum.json

sed -i \
's|bcb4b59cfc5f48fbaba954b8ae4daa9eaecf9044afc89208a78a7e995c321b81|5f33885b0cc238abc78622e32bd56e6c550d90f1de419d5d7768c8da7f53f039|' \
third_party/dump_syms/vendor/encoding_rs/.cargo-checksum.json

sed -i \
's|a64116a6d46ab30ce91deabdb4c5388f03be632326e8a00110bfb0c598474a0a|5cd020aae75fdcd21aca4799d302f1720075c9dd6fb12ba19e6403a8012970b2|' \
third_party/dump_syms/vendor/encoding_rs/.cargo-checksum.json

sed -i \
's|fe4881780e8cd0181748a891102b2dd54c2060546d7648ec1b6435529bd0dcc2|40588888ea0d4b7e86bc1e184be9e667bd1b0480fe00406ebb828821f5080a07|' \
third_party/dump_syms/vendor/h2/.cargo-checksum.json

sed -i \
's|6f3be57dae549574569b812acab6213f4136ba9c4ad60a21848043d26e6aaece|07f28083516d3660082d9c46fe2349a1099720b4f30cc7431f1cc267690d6505|' \
third_party/dump_syms/vendor/httparse/.cargo-checksum.json

sed -i \
's|dc1147f2b16de747c15ff9bdab44fc02fe18dcc1b6abe6acba56b69bec99013b|0fcf90cc1bd0498e18dba003142bc0dfdb43e761d88a8caefeb2d8018e2da1bb|' \
third_party/dump_syms/vendor/openssl/.cargo-checksum.json

sed -i \
's|82c82f8ef547f67f3f449a0b240e2df4ed538d976a379b07e53d5ce7cde2438c|01baa5e11aa7ec74360151d379ba8b5607abcd34132e69568c816c0116958a26|' \
third_party/dump_syms/vendor/symbolic-minidump/.cargo-checksum.json

sed -i \
's|6018b016930c86610953a88ca35e8cebffd2dbbd92f11502c2464ab4281e7bbc|04c8d6c8a498abac61ffb2725484c2732328270e25a7e3ed452657962ce76f4e|' \
third_party/dump_syms/vendor/bitvec/.cargo-checksum.json
#EO Manual fixing.

# about: page mods, like rights.
## Hide extra links in about box
find -wholename '*/brand.dtd' |xargs /bin/sed 's/trademarkInfo.part1.*/trademarkInfo.part1 "">/' -i

for STRING in rights-intro-point-2 rights-intro-point-3 rights-intro-point-4 rights-intro-point-5 rights-intro-point-6 rights-webservices rights-safebrowsing
do
 find -name aboutRights.ftl | xargs sed -i "s/^$STRING.*/$STRING = /"
done
##Remove invalid policy for trisquel.
find -name aboutRights.ftl | xargs sed -i '/rights-intro-point-5/,$d'
find -name aboutRights.xhtml |xargs sed -i "/rights-intro-point-5/d"
find -name aboutRights.xhtml |xargs sed -i "/rights-intro-point-6/d"
find -name aboutRights-unbranded.xhtml |xargs sed -i "/rights-intro-point-5/d"

sed -i 's/<a\ href\=\"https\:\/\/www.icedove.net\/\">Icedove\ Project<\/a>/<a\ href\=\"https\:\/\/www.trisquel.info\/\"\>Trisquel\ Project<\/a>/g' \
    comm/mail/base/content/overrides/app-license.html
#EO about: mods.

#Debbuging tools
sed -i 's|this-firefox|this-abrowser|g' devtools/client/framework/test/browser_about-devtools-toolbox_reload.js
#Rebrand debug tools
echo "devtools/client/themes/images/aboutdebugging-fenix.svg \
devtools/client/themes/images/aboutdebugging-fenix-nightly.svg \
devtools/client/themes/images/aboutdebugging-firefox-aurora.svg \
devtools/client/themes/images/aboutdebugging-firefox-beta.svg \
devtools/client/themes/images/aboutdebugging-firefox-nightly.svg \
devtools/client/themes/images/aboutdebugging-firefox-release.svg" | xargs -n1 cp $DATA/debug_brand/debuglogo.svg
cp $DATA/debug_brand/debuglogo_gray.svg devtools/client/themes/images/aboutdebugging-firefox-logo.svg
cp $DATA/debug_brand/about-logo.svg comm/mail/branding/icedove/TB-symbolic.svg
cp $DATA/debug_brand/about-logo.svg comm/mail/themes/shared/mail/icons/aboutdebugging-logo.svg
cp $DATA/debug_brand/icon.png    comm/mail/themes/icon.png
cp $DATA/debug_brand/icon64.png  comm/mail/themes/icon64.png
cp $DATA/debug_brand/account-watermark*.png comm/mail/themes/shared/mail/images/

#Requirements for Icedove
THNDR_SYS_URL="https://www.icedove.net/icedove/system-requirements/"
ICEDV_SYS_URL="https://trisquel.info/wiki/icedove-requirements/"
sed -i "s|$THNDR_SYS_URL|$ICEDV_SYS_URL|" comm/mail/branding/icedove/branding.nsi

#Remove search engines inherited from firefox (remove all?).
for r in amazon amazondotcn amazondotcom bing google mercadolibre mercadolivre yahoo-jp yahoo-jp-auctions
do
    rm -r comm/mail/components/search/extensions/$r
done

for r2 in amazon bing google yahoo
do
    find comm/suite -name $r2* -delete
done

# Reprocess search preconfiguration dump
python3 $DATA/process-json-files.py . browser/components/extensions/schemas/

#EO Debbuging tools

cat << EOF > comm/mail/branding/nightly/locales/en-US/brand.properties
brandShortName=Icedove
brandShorterName=Icedove
brandFullName=Icedove Mail
vendorShortName=Icedove
EOF

BRAND_FTL_NTLY=comm/mail/branding/nightly/locales/en-US/brand.ftl
BRAND_FTL_STD=comm/mail/branding/icedove/locales/en-US/brand.ftl
sed -i '/shorter-name/s|Trisquel|Icedove|' $BRAND_FTL_NTLY
sed -i '/short-name/s|Trisquel|Icedove|' $BRAND_FTL_NTLY
sed -i '/full-name/s|Icedove Trisquel|Icedove Mail|' $BRAND_FTL_NTLY
sed -i '/full-name/s|Icedove|Icedove Mail|' $BRAND_FTL_STD

cat << EOF > comm/mail/branding/nightly/locales/en-US/brand.dtd
<!-- 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/. -->

<!ENTITY  brandShortName        "Icedove">
<!ENTITY  brandShorterName      "Icedove">
<!ENTITY  brandFullName         "Icedove Mail">
<!-- LOCALIZATION NOTE (brandProductName):
   This brand name can be used in messages where the product name needs to
   remain unchanged across different versions (Trisquel, Beta, etc.). -->
<!ENTITY  brandProductName      "Icedove">
<!ENTITY  vendorShortName       "mozilla.org">
<!ENTITY  trademarkInfo.part1   " ">
EOF

debian/rules debian/control
touch -d "yesterday" debian/control
debian/rules debian/control

changelog  "Rebranded for Trisquel"

compile
