1#!/usr/bin/env bash
2
3set -e
4
5# Space-separated of packages required for the script to run
6# These are installed automatically
7REQUIRED_PACKAGES="dialog git"
8
9# List of distros that provide their own Gnome Control Center patches
10PROVIDES_GCC_PATCHES=("Zorin")
11
12function setup() {
13  set -e
14
15  to_install=()
16
17  for package in "[email protected]"; do
18    # if [ -z "$(dpkg -l "$package")" ]; then
19    to_install+=("$package")
20    # fi
21  done
22
23  apt install -y "${to_install[@]}"
24}
25
26function get_choices() {
27  dialog --stdout --checklist "Select Features" 100 100 80 \
28    "oled" "Control brightness on OLED displays" "oled" \
29    "power" "Utility for managing graphics and power profiles." "power" \
30    "hidpi" "Manage HiDPI and LoDPI monitors on X " "hidpi" \
31    "firmware" "Check and update firmware from the fwupd and system76-firmware services" "firmware"
32}
33
34function install_packages() {
35  set -e
36
37  add-apt-repository -y ppa:system76/pop
38  apt-get update
39
40  cat >/etc/apt/preferences.d/popos.pref <<-'EOF'
41	Package: *
42	Pin: release o=LP-PPA-system76-pop
43	Pin-Priority: 200
44	EOF
45
46  to_install=()
47
48  for choice in "[email protected]"; do
49    [[ "$choice" == "oled" ]] && to_install+=(system76-oled)
50    [[ "$choice" == "power" ]] && to_install+=(system76-power gnome-shell-extension-system76-power)
51    [[ "$choice" == "hidpi" ]] && to_install+=(hidpi-daemon libs76-hidpi-widget)
52    [[ "$choice" == "firmware" ]] && to_install+=(firmware-manager)
53  done
54
55  apt install -y "${to_install[@]}"
56}
57
58function install_control_center() {
59  cat >>/etc/apt/preferences.d/popos.pref <<-'EOF'
60	Package: gnome-control-center
61	Pin: release o=LP-PPA-system76-pop
62	Pin-Priority: 600
63	EOF
64
65  apt install gnome-control-center
66}
67
68function build_control_center() {
69  set -e
70
71  # build newer version of polkit
72  wget https://www.freedesktop.org/software/polkit/releases/polkit-0.114.tar.gz
73  tar xvf polkit-0.114.tar.gz
74  pushd polkit-0.114
75
76  sed -e '/JS_ReportWarningUTF8/s/,/, "%s",/' \
77    -i src/polkitbackend/polkitbackendjsauthority.cpp
78
79  ./configure --prefix=/usr \
80    --sysconfdir=/etc \
81    --localstatedir=/var \
82    --disable-static
83  make
84  make install
85
86  popd
87
88  # get source code
89  git clone https://github.com/pop-os/gnome-control-center.git --depth 1
90  pushd gnome-control-center
91
92  codename=$(lsb_release -sc)
93  git checkout master_"$codename"
94
95  # Handle distro-specific Gnome Control Centre patches here
96  release="$(lsb_release -si)"
97  case $release in
98  "Zorin")
99    # GNOME 3.38.6 patches
100    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'
101    echo $GCC_PATCHES_URL | xargs -n1 wget -O zorin_patches.tar.xz
102    tar xvf zorin_patches.tar.xz debian/patches --skip-old-files
103    ;;
104  esac
105
106  meson builddir/
107  ninja -C builddir/
108  ninja -C builddir/ install
109
110  popd
111  rm -rf gnome-control-center
112}
113
114function enable_extensions() {
115  for choice in "[email protected]"; do
116    if [[ "$choice" == "power" ]]; then
117      gnome-extensions enable [email protected]
118    fi
119  done
120}
121
122release="$(lsb_release -si)"
123requires_gcc_rebuild=$([[ " ${PROVIDES_GCC_PATCHES[*]} " =~ ${release} ]])
124
125if $requires_gcc_rebuild; then
126  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"
127fi
128
129sudo bash -c "$(declare -f setup); setup $REQUIRED_PACKAGES"
130
131choices=$(get_choices)
132
133if $requires_gcc_rebuild; then
134  sudo bash -c "$(declare -f build_control_center); build_control_center || rm -rf gnome-control-center"
135else
136  sudo bash -c "$(declare -f install_control_center); install_control_center"
137fi
138
139sudo bash -c "$(declare -f install_packages); install_packages $choices"
140enable_extensions "$choices"
141
142killall -3 gnome-shell
143killall -3 gnome-control-center
144