raw download copy
#!/usr/bin/env bash

set -e

# Space-separated of packages required for the script to run
# These are installed automatically
REQUIRED_PACKAGES="dialog git"

# List of distros that provide their own Gnome Control Center patches
PROVIDES_GCC_PATCHES=("Zorin")

function setup() {
  set -e

  to_install=()

  for package in "$@"; do
    # if [ -z "$(dpkg -l "$package")" ]; then
    to_install+=("$package")
    # fi
  done

  apt install -y "${to_install[@]}"
}

function get_choices() {
  dialog --stdout --checklist "Select Features" 100 100 80 \
    "oled" "Control brightness on OLED displays" "oled" \
    "power" "Utility for managing graphics and power profiles." "power" \
    "hidpi" "Manage HiDPI and LoDPI monitors on X " "hidpi" \
    "firmware" "Check and update firmware from the fwupd and system76-firmware services" "firmware"
}

function install_packages() {
  set -e

  add-apt-repository -y ppa:system76/pop
  apt-get update

  cat >/etc/apt/preferences.d/popos.pref <<-'EOF'
	Package: *
	Pin: release o=LP-PPA-system76-pop
	Pin-Priority: 200
	EOF

  to_install=()

  for choice in "$@"; do
    [[ "$choice" == "oled" ]] && to_install+=(system76-oled)
    [[ "$choice" == "power" ]] && to_install+=(system76-power gnome-shell-extension-system76-power)
    [[ "$choice" == "hidpi" ]] && to_install+=(hidpi-daemon libs76-hidpi-widget)
    [[ "$choice" == "firmware" ]] && to_install+=(firmware-manager)
  done

  apt install -y "${to_install[@]}"
}

function install_control_center() {
  cat >>/etc/apt/preferences.d/popos.pref <<-'EOF'
	Package: gnome-control-center
	Pin: release o=LP-PPA-system76-pop
	Pin-Priority: 600
	EOF

  apt install gnome-control-center
}

function build_control_center() {
  set -e

  # build newer version of polkit
  wget https://www.freedesktop.org/software/polkit/releases/polkit-0.114.tar.gz
  tar xvf polkit-0.114.tar.gz
  pushd polkit-0.114

  sed -e '/JS_ReportWarningUTF8/s/,/, "%s",/' \
    -i src/polkitbackend/polkitbackendjsauthority.cpp

  ./configure --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --disable-static
  make
  make install

  popd

  # get source code
  git clone https://github.com/pop-os/gnome-control-center.git --depth 1
  pushd gnome-control-center

  codename=$(lsb_release -sc)
  git checkout master_"$codename"

  # Handle distro-specific Gnome Control Centre patches here
  release="$(lsb_release -si)"
  case $release in
  "Zorin")
    # GNOME 3.38.6 patches
    GCC_PATCHES_URL='https://launchpad.net/~zorinos/+archive/ubuntu/patches/+sourcefiles/gnome-control-center/1:3.38.6-0ubuntu1+zorin3/gnome-control-center_3.38.6-0ubuntu1+zorin3.debian.tar.xz'
    echo $GCC_PATCHES_URL | xargs -n1 wget -O zorin_patches.tar.xz
    tar xvf zorin_patches.tar.xz debian/patches --skip-old-files
    ;;
  esac

  meson builddir/
  ninja -C builddir/
  ninja -C builddir/ install

  popd
  rm -rf gnome-control-center
}

function enable_extensions() {
  for choice in "$@"; do
    if [[ "$choice" == "power" ]]; then
      gnome-extensions enable system76-power@system76
    fi
  done
}

release="$(lsb_release -si)"
requires_gcc_rebuild=$([[ " ${PROVIDES_GCC_PATCHES[*]} " =~ ${release} ]])

if $requires_gcc_rebuild; then
  REQUIRED_PACKAGES+=" meson ninja clang python3-setuptools libglib2.0-dev libpulse-dev libgtk-3-dev libaccountsservice-dev libcolord-dev libgnome-desktop-3-dev gnome-settings-daemon-dev libgoa-1.0-dev"
fi

sudo bash -c "$(declare -f setup); setup $REQUIRED_PACKAGES"

choices=$(get_choices)

if $requires_gcc_rebuild; then
  sudo bash -c "$(declare -f build_control_center); build_control_center || rm -rf gnome-control-center"
else
  sudo bash -c "$(declare -f install_control_center); install_control_center"
fi

sudo bash -c "$(declare -f install_packages); install_packages $choices"
enable_extensions "$choices"

killall -3 gnome-shell
killall -3 gnome-control-center