#! /bin/bash

# Assumes base Debian install with GNOME desktop environment

TEMP_DIR=$(mktemp -d)
PUBLIC_URL="https://public.counter.llc"
FIREFOX_DIR="/usr/lib/firefox-esr/"

# Install apt packages
sudo add-apt-repository ppa:obsproject/obs-studio -y
curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o $TEMP_DIR/google.pub
sudo gpg --no-default-keyring --keyring /etc/apt/keyrings/google-chrome.gpg --import $TEMP_DIR/google.pub
echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y firefox-esr google-chrome-stable obs-studio vlc
sudo apt-get install -y dbus-x11 cifs-utils ffmpeg unzip alien libglu1-mesa
sudo apt-get install -y git python3 python3-pip nodejs npm
sudo apt-get install -y yaru-theme-icon gnome-shell-extension-autohidetopbar gnome-shell-extension-dashtodock
yes | sudo DEBIAN_FRONTEND=noninteractive apt-get install -yqq jackd qjackctl

# Install NPM packages
sudo npm install --global yarn

# Fetch local files
curl $PUBLIC_URL/assets/wallpaper.png -o $HOME/Pictures/wallpaper.png
curl $PUBLIC_URL/assets/magicq.pdf -o $HOME/Documents/magicq.pdf

# Install ZeroTier
curl -s https://install.zerotier.com | sudo bash
sudo zerotier-cli join 8bd5124fd62ef069

# Install VSCode
CODE_URL=$(curl -Ls -o /dev/null -w %{url_effective} "https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64")
curl $CODE_URL -o $TEMP_DIR/code.deb
yes | sudo DEBIAN_FRONTEND=noninteractive dpkg -i $TEMP_DIR/code.deb

# Install MagicQ
MQ_VERSION="beta"
MQ_PLATFORM="linux64"
MQ_URL=$(curl -Ls -o /dev/null -w %{url_effective} "https://secure.chamsys.co.uk/bugtracker/mc_software_download.php?software=magicq&platform=$MQ_PLATFORM&version=$MQ_VERSION&email=william%40madebycounter.com")

curl $MQ_URL -o $TEMP_DIR/magicq.deb
sudo dpkg -i $TEMP_DIR/magicq.deb
sudo mv /opt/magicq/lib/libstdc.so.6 /opt/magicq/lib/libstdc.so.6~

# Install Reaper
curl -L https://www.reaper.fm/files/7.x/reaper722_linux_x86_64.tar.xz -o $TEMP_DIR/reaper.tar.xz
tar xf $TEMP_DIR/reaper.tar.xz -C $TEMP_DIR
sudo sh $TEMP_DIR/reaper_*/install-reaper.sh --install /opt --integrate-desktop

# Install BRAW player
curl $PUBLIC_URL/assets/Blackmagic_RAW_Linux_4.2.tar.gz -o $TEMP_DIR/braw.tar.gz
tar xf $TEMP_DIR/braw.tar.gz -C $TEMP_DIR
sudo alien "$TEMP_DIR/Blackmagic RAW/blackmagic-raw-4.2.x86_64.rpm"
mv blackmagic-raw*.deb $TEMP_DIR/braw.deb
sudo dpkg -i $TEMP_DIR/braw.deb

# Configure Firefox
sudo tee $FIREFOX_DIR/defaults/pref/autoconfig.js << END
pref("general.config.filename", "firefox.cfg");
pref("general.config.obscure_value", 0);
END

sudo tee $FIREFOX_DIR/firefox.cfg << END
// Do not remove this comment
lockPref('browser.toolbars.bookmarks.visibility', 'never');
lockPref('browser.uiCustomization.state', '{"placements":{"widget-overflow-fixed-list":[],"unified-extensions-area":[],"nav-bar":["back-button","forward-button","stop-reload-button","urlbar-container","downloads-button","unified-extensions-button","reset-pbm-toolbar-button"],"toolbar-menubar":["menubar-items"],"TabsToolbar":["tabbrowser-tabs","new-tab-button","alltabs-button"],"PersonalToolbar":["personal-bookmarks"]},"seen":["save-to-pocket-button","developer-button"],"dirtyAreaCache":["nav-bar","toolbar-menubar","TabsToolbar","PersonalToolbar","widget-overflow-fixed-list"],"currentVersion":20,"newElementCount":3}');
lockPref('browser.uidensity', 1);
lockPref('browser.startup.homepage', 'about:home');
lockPref('browser.newtabpage.activity-stream.showRecentSaves', false);
lockPref('browser.newtabpage.activity-stream.showSearch', true);
lockPref('browser.newtabpage.activity-stream.showSponsored', false);
lockPref('browser.newtabpage.activity-stream.showSponsoredTopSites', false);
lockPref('browser.newtabpage.activity-stream.showWeather', false);
lockPref('browser.newtabpage.activity-stream.topSitesRows', 2);
lockPref('ui.systemUsesDarkTheme', 0);
END

# Configure GNOME
gsettings set org.gnome.desktop.wm.preferences button-layout ":minimize,maximize,close"
gsettings set org.gnome.desktop.wm.keybindings toggle-fullscreen "['<Alt>f']"
gsettings set org.gnome.desktop.background color-shading-type 'solid'
gsettings set org.gnome.desktop.background picture-options 'zoom'
gsettings set org.gnome.desktop.background picture-uri "file:///$HOME/Pictures/wallpaper.png"
gsettings set org.gnome.desktop.background primary-color '#000000'
gsettings set org.gnome.desktop.background secondary-color '#000000'
gsettings set org.gnome.desktop.interface icon-theme 'Yaru'
gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'
gsettings set org.gnome.desktop.interface show-battery-percentage 'true'
gsettings set org.gnome.desktop.session idle-delay 'uint32 0'
gsettings set org.gnome.desktop.peripherals.mouse accel-profile 'flat'
gsettings set org.gnome.shell favorite-apps "['firefox-esr.desktop', 'org.gnome.Nautilus.desktop', 'org.gnome.Terminal.desktop', 'code.desktop', 'magicq.desktop', 'cockos-reaper.desktop', 'org.rncbc.qjackctl.desktop']"
gsettings set org.gnome.shell disable-user-extensions 'false'
gsettings set org.gnome.shell disabled-extensions '@as []'
gsettings set org.gnome.shell enabled-extensions "['hidetopbar@mathieu.bidon.ca', 'dash-to-dock@micxgx.gmail.com']"
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing'
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'

# Allow Parsec (and other apps) to grab keyboard/mouse
gsettings set org.gnome.mutter.wayland xwayland-allow-grabs 'true'
gsettings set org.gnome.mutter.wayland xwayland-grab-access-rules "['Remmina', 'VirtualBox Machine', 'parsecd']"

# Configure Fonts
gsettings set org.gnome.desktop.interface font-name 'Sans 11'
gsettings set org.gnome.desktop.interface monospace-font-name 'Monospace 11'
gsettings set org.gnome.desktop.interface document-font-name 'Sans 11'
gsettings set org.gnome.desktop.wm.preferences titlebar-font 'Sans Bold 11'

# Configure Dash-to-Dock
gsettings set org.gnome.shell.extensions.dash-to-dock custom-theme-shrink 'false'
gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size '48'
gsettings set org.gnome.shell.extensions.dash-to-dock dock-position 'BOTTOM'
gsettings set org.gnome.shell.extensions.dash-to-dock extend-height 'false'
gsettings set org.gnome.shell.extensions.dash-to-dock height-fraction '0.9'
gsettings set org.gnome.shell.extensions.dash-to-dock icon-size-fixed 'false'
gsettings set org.gnome.shell.extensions.dash-to-dock isolate-locations 'true'
gsettings set org.gnome.shell.extensions.dash-to-dock isolate-workspaces 'false'
gsettings set org.gnome.shell.extensions.dash-to-dock multi-monitor 'false'
gsettings set org.gnome.shell.extensions.dash-to-dock show-favorites 'true'
gsettings set org.gnome.shell.extensions.dash-to-dock show-mounts-network 'false'
gsettings set org.gnome.shell.extensions.dash-to-dock show-show-apps-button 'true'
gsettings set org.gnome.shell.extensions.dash-to-dock show-trash 'false'

# Configure Terminal
TERM_PROFILE_ID=$(gsettings get org.gnome.Terminal.ProfilesList default)
TERM_PROFILE_ID=${TERM_PROFILE_ID:1:-1} # remove leading and trailing single quotes
gsettings set org.gnome.Terminal.Legacy.Settings theme-variant 'dark'
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:$TERM_PROFILE_ID/ use-theme-colors 'false'
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:$TERM_PROFILE_ID/ foreground-color 'rgb(255,255,255)'
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:$TERM_PROFILE_ID/ background-color 'rgb(48,10,36)'
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:$TERM_PROFILE_ID/ audible-bell 'false'

# Clean up
rm -rf $TEMP_DIR
echo "Done. Reboot recommended"