#!/usr/bin/env bash
#!/bin/bash
set -e

#RELEASE_NAME=Debian_Unstable
#RELEASE_NAME=Debian_Testing
#RELEASE_NAME=Debian_9.0 # stretch
#RELEASE_NAME=Debian_10  # buster
#RELEASE_NAME=Debian_11  # bullseye
RELEASE_NAME=Debian_12  # bookworm



mkdir -p /etc/apt//sources.list-available || exit
cat <<EOF | sudo tee /etc/apt/sources.list-available/waterfox.list >/dev/null
# curl -fsSL https://download.opensuse.org/repositories/home:hawkeye116477:waterfox/${RELEASE_NAME}/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_hawkeye116477_waterfox.gpg > /dev/null

deb http://download.opensuse.org/repositories/home:/hawkeye116477:/waterfox/${RELEASE_NAME}/ /
EOF

# The dl-bintray repo doesn't play nice with apt-proxy
#echo "Acquire::http::Proxy { dl.bintray.com DIRECT;  };" >> /etc/apt/apt.conf.d/02proxy

sudo ln -sf /etc/apt/sources.list-available/waterfox.list /etc/apt/sources.list.d/waterfox.list

curl -fsSL https://download.opensuse.org/repositories/home:hawkeye116477:waterfox/${RELEASE_NAME}/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_hawkeye116477_waterfox.gpg > /dev/null

# Import GPG key for Ubuntu 22.04 Jammy Jellyfish LTS:
curl -fsSL https://download.opensuse.org/repositories/home:hawkeye116477:waterfox/xUbuntu_22.04/Release.key | sudo gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_hawkeye116477_waterfox.gpg > /dev/null

# Import GPG key for Ubuntu 20.04 Focal Fossa LTS:
curl -fsSL https://download.opensuse.org/repositories/home:/hawkeye116477:/waterfox/xUbuntu_20.04/Release.key | sudo gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_hawkeye116477_waterfox.gpg > /dev/null

sudo apt update

# waterfox-classic-kde - KDE Plasma integration for Waterfox Classic
# waterfox-classic-kpe - Free, open and private browser with openSUSE's patches for better integration with KDE
# waterfox-classic-i18n-en-gb - English (British) language pack for Waterfox Classic

# waterfox-current-kde - KDE Plasma integration for Waterfox Current
# waterfox-current-kpe - Free, open and private browser with better integration with KDE
# waterfox-current-i18n-en-gb - English (British) language pack for Waterfox Current

sudo apt install -y waterfox-g4-kpe waterfox-g4-i18n-en-gb

sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser /usr/bin/waterfox-current 201




# Stop white flash background in dark theme mode.
# https://quasiengineer.dev/tech/guides/fix-white-flash-in-dark-mode-in-firefox/
# https://gist.github.com/gmolveau/a802ded1320a7591a289fb7abd0d6c45
# https://wiki.archlinux.org/title/Firefox/Tweaks

#--------
## These are system wide and break all webpages but do fix flashing system pages

# user_pref("browser.display.foreground_color", "#FEFEFE");
# user_pref("browser.display.background_color", "#2E2E31");
# user_pref("browser.display.document_color_use", 2);
# user_pref("browser.display.use_system_colors", true);
#---------

# Add option toolkit.legacyUserProfileCustomizations.stylesheets to True.
# in about:config

# user_pref("toolkit.legacyUserProfileCustomizations.stylesheets", true);
#-----------

# find the default profile directory
cd ${HOME}/.waterfox
if [[ $(grep '\[Profile[^0]\]' profiles.ini) ]]
then PROFPATH=$(grep -E '^\[Profile|^Path|^Default' profiles.ini | grep -1 '^Default=1' | grep '^Path' | cut -c6-)
else PROFPATH=$(grep 'Path=' profiles.ini | sed 's/^Path=//')
fi

## userChrome.css - change the way Mozilla applications' interfaces look
# http://kb.mozillazine.org/index.php?title=UserChrome.css&printable=yes
echo $PROFPATH
for PATH in $PROFPATH;do
	echo "Writing background color change css to : ${PATH}/chrome/userChrome.css"
	mkdir -p $PATH/chrome
	cat > ${HOME}/.waterfox/${PATH}/chrome/userChrome.css <<'EOF'

tabbrowser tabpanels { background-color: rgb(19,19,20) !important; }
browser { background-color: #131314 !important; }

@-moz-document url(chrome://browser/content/browser.xul)
{

  #main-window,
  browser[type="content-primary"],
  browser[type="content"],
  tabbrowser#content,
  #content,
  browser[type="content"] > html
  {
     background: #323234 !important;
  }

}
EOF
done

##  userContent.css - change the way web sites and e-mails look.
# http://kb.mozillazine.org/index.php?title=UserContent.css&printable=yes
for PATH in $PROFPATH;do
	echo "Writing background change css to: ${PATH}/chrome/userContent.css"
	mkdir -p $PATH/chrome
	cat > ${HOME}/.waterfox/${PATH}/userContent.css <<'EOF'

@charset "utf-8";
/* CSS Document */
@-moz-document url("about:newtab") { body { background-color: #011326 !important; } }

@-moz-document url(chrome://browser/content/browser.xul) { browser[type="content-primary"] {background: #011326 !important } }
@-moz-document url(chrome://browser/content/browser.xul) {} => @-moz-document url(chrome://browser/content/browser.xhtml) {}

@-moz-document url("about:sessionrestore") { * { background-color: rgb(19,19,20) } }

@-moz-document url-prefix("about:sessionrestore") { * { background-color: rgb(19,19,20); } }

@-moz-document  url("about:blank") { html { background-color: rgb(19,19,20) !important; } }

@-moz-document url-prefix("about:blank") { * { background-color: rgb(19,19,20); } }
EOF
done

# Solarize viewsource
# https://gist.github.com/abelcheung/0229ee3ab896feac8126f40eb1405996

for PATH in $PROFPATH;do
	echo "Writing solarize view-source css to: ${PATH}/chrome/userContent.css" 
	mkdir -p $PATH/chrome
	cat > ${HOME}/.waterfox/${PATH}/userContent.css <<'EOF'

@-moz-document url-prefix(view-source:) {

	*|*:root {
		background: none !important;
	}

	#viewsource {
		font-family: "Roboto Mono", Inconsolata, "Source Code Pro", SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace, -moz-fixed !important;
		line-height: 1.5em;
		background-image:
		url(https://farm9.staticflickr.com/8536/8680613751_71ef92bbdb_o_d.jpg),
		linear-gradient(to right, #002b36d0, #002b36d0, #000000d0),
		linear-gradient(#00000040 50%, transparent 50%);
		background-size: cover, cover, auto 3em;
		background-position: center, center, top 10px left 0;
		background-attachment: fixed, fixed, scroll;
		background-repeat: no-repeat, no-repeat, repeat-y;
		background-blend-mode: overlay, normal, normal;
		color: #839496;
	}

	pre[id]:before,
	span[id]:before {
		width: 6ch !important;
		color: #586e75 !important;
		margin-left: -7ch !important;
	}
	pre {
		padding-left: 1ch;
		margin-left: 6ch !important;
		border-left: 2px solid #073642;
	}
	pre > :first-child {
		padding-right: 1ch;
	}

	.highlight .start-tag {
	 color: #d33682 !important;
	}
	.highlight .end-tag {
	 color: #d33682 !important;
	}
	.highlight .comment {
	 color: #586e75 !important;
	}
	.highlight .cdata {
	 color: #cb4b16 !important;
	}
	.highlight .doctype {
	 color: #268be2 !important;
	}
	.highlight .pi {
	 color: #268be2 !important; /* orchid -> blue */
	}
	.highlight .entity {
	 color: #b58900 !important; /* violet -> yellow */
	}
	.highlight .attribute-name {
	 color: #93a1a1 !important;
	}
	.highlight .attribute-value {
	 color: #2aa198 !important;
	 text-shadow: 1px 1px 2px black;
	}
	.highlight .markupdeclaration {
	 color: #859900 !important;
	}
	.highlight .error,
	.highlight .error > :-moz-any(.start-tag, .end-tag, .comment, .cdata, .doctype,
	.pi, .entity, .attribute-name, .attribute-value) {
		color: #002b36 !important;
		background-color: #dc322f !important;
	}
}
EOF
done 

# Themes
## Australis Dark - enabled in Firefox 53
## Has rounded corners on active tab and grey surround
# http://github.com/wilfredwee/australis-dark
# https://addons.mozilla.org/en-US/firefox/addon/devedition-theme-enabler/
#https://addons.mozilla.org/firefox/downloads/file/477584/developer_edition_theme_enabler-1.0.1-fx.xpi

## Aa Dark Background and light text

## ff2mpv
## Firebug - being retired
## firepath
## greasemonkey
## it's all text
## saved password editor
## text link
## try xpath
## ublock origin
## umatrix
## web developer
## xdebug helper
## xpath finder
## wideo blocker - doesn't seem to work
