11#! /usr/bin/env bash
2-
2+ #
33# auto-cpufreq-installer:
44# auto-cpufreq source code based installer
55
6- cd " $( dirname " $( readlink -f " $0 " ) " ) " || exit 1
6+ set -o pipefail
7+
8+ cd " $( dirname -- " $( readlink -f -- " $0 " ) " ) " || exit 1
79
8- COLOUMNS= " ` tput cols` "
9- MID=" $(( COLOUMNS / 2 )) "
10+ COLUMNS= " $( tput cols) "
11+ MID=" $(( COLUMNS / 2 )) "
1012
1113APPLICATIONS_PATH=" /usr/share/applications"
1214VENV_PATH=" /opt/auto-cpufreq"
1315
14- SHARE_DIR=" /usr/local/share/auto-cpufreq/ "
16+ SHARE_DIR=" /usr/local/share/auto-cpufreq"
1517
1618AUTO_CPUFREQ_FILE=" /usr/local/bin/auto-cpufreq"
17- AUTO_CPUFREQ_GTK_FILE=$ AUTO_CPUFREQ_FILE -gtk
18- AUTO_CPUFREQ_GTK_DESKTOP_FILE =" $( basename $AUTO_CPUFREQ_GTK_FILE ) .desktop"
19+ AUTO_CPUFREQ_GTK_FILE=" ${ AUTO_CPUFREQ_FILE} -gtk"
20+ AUTO_CPUFREQ_GTK_DESKTOP_FILE_NAME =" $( basename -- " $AUTO_CPUFREQ_GTK_FILE " ) .desktop"
1921
2022IMG_FILE=" /usr/share/pixmaps/auto-cpufreq.png"
2123ORG_FILE=" /usr/share/polkit-1/actions/org.auto-cpufreq.pkexec.policy"
2224
23- function header {
25+ header () {
2426 echo
25- printf " %0.s─" $( seq $(( MID- (${# 1} / 2 )- 2 )) )
26- printf " $1 "
27- printf " %0.s─" $( seq $(( MID- (${# 1} / 2 )- 2 )) )
28- echo ; echo
27+ printf ' %0.s─' $( seq $(( MID- (${# 1} / 2 )- 2 )) )
28+ printf ' %s ' " $1 "
29+ printf ' %0.s─' $( seq $(( MID- (${# 1} / 2 )- 2 )) )
30+ echo ; echo
2931}
3032
31- function ask_operation {
32- header " auto-cpufreq installer"
33+ ask_operation () {
34+ header " auto-cpufreq installer"
3335 echo " Welcome to auto-cpufreq tool installer." ; echo
34- read -p " Select a key [I]nstall/[R]emove or press ctrl+c to quit: " answer
36+ read -rp " Select a key [I]nstall/[R]emove or press ctrl+c to quit: " OPERATION
3537}
3638
37- function manual_install {
39+ manual_install () {
3840 if command -v lsb_release > /dev/null; then
3941 distro=" $( lsb_release -is) "
4042 release=" $( lsb_release -rs) "
4143 codename=" $( lsb_release -cs) "
4244 fi
4345
44- echo " Didn't detect Debian or RedHat or Arch based distro." ; echo
46+ echo " Didn't detect Debian or RedHat or Arch based distro." ; echo
4547 echo " To complete installation, you need to:"
4648 echo " Install: python3, pip3, python3-setuptools, gobject-introspection, cairo (or cairo-devel), gcc, and gtk3" ; echo
4749 echo " Install necessary Python packages:"
@@ -55,8 +57,8 @@ function manual_install {
5557 echo " After which tool is installed, for full list of options run:" ; echo
5658 echo " auto-cpufreq --help"
5759
58- echo ; printf " %0.s─" $( seq $COLOUMNS ) ; echo
59-
60+ echo ; printf ' %0.s─' $( seq " $COLUMNS " ) ; echo
61+
6062 echo " Consider creating a feature request to add support for your distro:"
6163 echo " https://github.com/AdnanHodzic/auto-cpufreq/issues/new" ; echo
6264 echo " Make sure to include following information:" ; echo
@@ -68,21 +70,21 @@ function manual_install {
6870 exit 1
6971}
7072
71- function tool_install {
73+ tool_install () {
7274 echo
7375 # First argument is the distro
74- function detected_distro {
76+ detected_distro () {
7577 header " Detected $1 distribution"
7678 header " Setting up Python environment"
7779 }
7880
79- if [ -f /etc/debian_version ]; then
81+ if [[ -s /etc/debian_version ]] ; then
8082 detected_distro " Debian based"
81- VERSION=$( cat /etc/debian_version)
83+ VERSION=" $( cat /etc/debian_version) "
8284
8385 # install necessary libgirepository debian package dependencies
8486 # https://github.com/AdnanHodzic/auto-cpufreq/pull/826#issuecomment-2794549837
85- apt update
87+ apt update || exit 1
8688
8789 if apt-cache show libgirepository-2.0-dev > /dev/null 2>&1 ; then
8890 LIB_GI_REPO=" libgirepository-2.0-dev"
@@ -96,10 +98,10 @@ function tool_install {
9698
9799 # Update PyGObject in pyproject.toml
98100 # https://github.com/AdnanHodzic/auto-cpufreq/pull/826#issuecomment-2794549837
99- if [ -f ./pyproject.toml ]; then
101+ if [[ -f ./pyproject.toml ] ]; then
100102 if grep -q ' PyGObject *= *{[^}]*version *= *"' pyproject.toml; then
101- sed -i " s/\(PyGObject *= *{[^}]*version *= *\ " \)[^\ " ]*\(.*\)/\1$PYGOBJECT_VER \2/" pyproject.toml
102- echo " PyGObject version updated to $PYGOBJECT_VER in pyproject.toml"
103+ sed -i ' s/\(PyGObject *= *{[^}]*version *= *"\)[^"]*\(.*\)/\1' " $PYGOBJECT_VER " ' \2/' pyproject.toml
104+ echo " PyGObject version updated to [ $PYGOBJECT_VER ] in pyproject.toml"
103105 else
104106 echo " Warning: Could not find PyGObject version entry in pyproject.toml!"
105107 fi
@@ -108,41 +110,43 @@ function tool_install {
108110 fi
109111
110112
111- echo " $LIB_GI_REPO needs to be installed for version $VERSION "
113+ echo " $LIB_GI_REPO pkg needs to be installed for version $VERSION "
112114 echo ' ---- '
113115 apt install -y python3-dev python3-pip python3-venv python3-setuptools dmidecode \
114- " $LIB_GI_REPO " libcairo2-dev libgtk-3-dev gcc
116+ " $LIB_GI_REPO " libcairo2-dev libgtk-3-dev gcc || exit 1
115117
116- elif [ -f /etc/redhat-release ]; then
118+ elif [[ -f /etc/redhat-release ] ]; then
117119 detected_distro " RedHat based"
118- if [ -f /etc/centos-release ]; then yum install platform-python-devel
119- else yum install python-devel
120+ if [[ -f /etc/centos-release ]]; then
121+ yum install platform-python-devel || exit 1
122+ else
123+ yum install python-devel || exit 1
120124 fi
121- yum install dmidecode gcc cairo-devel gobject-introspection-devel cairo-gobject-devel gtk3-devel
125+ yum install dmidecode gcc cairo-devel gobject-introspection-devel cairo-gobject-devel gtk3-devel || exit 1
122126
123- elif [ -f /etc/solus-release ]; then
127+ elif [[ -f /etc/solus-release ] ]; then
124128 detected_distro " Solus"
125- eopkg install pip python3 python3-devel dmidecode gobject-introspection-devel libcairo-devel gcc libgtk-3
126- eopkg install -c system.devel
129+ eopkg install pip python3 python3-devel dmidecode gobject-introspection-devel libcairo-devel gcc libgtk-3 || exit 1
130+ eopkg install -c system.devel || exit 1
127131
128- elif [ -f /etc/arch-release ]; then
132+ elif [[ -f /etc/arch-release ] ]; then
129133 detected_distro " Arch Linux based"
130- pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode gobject-introspection gtk3 gcc
131-
132- elif [ -f /etc/os-release ]; then
133- . /etc/os-release
134- case $ID in
134+ pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode gobject-introspection gtk3 gcc || exit 1
135+
136+ elif [[ -s /etc/os-release ] ]; then
137+ . /etc/os-release || exit 1
138+ case " $ID " in
135139 opensuse-leap)
136140 detected_distro " OpenSUSE"
137- zypper install -y python3 python3-pip python311-setuptools python3-devel gcc dmidecode gobject-introspection-devel python3-cairo-devel gtk3 gtk3-devel
141+ zypper install -y python3 python3-pip python311-setuptools python3-devel gcc dmidecode gobject-introspection-devel python3-cairo-devel gtk3 gtk3-devel || exit 1
138142 ;;
139143 opensuse-tumbleweed)
140144 detected_distro " OpenSUSE"
141- zypper install -y python3 python3-pip python311-setuptools python3-devel gcc dmidecode gobject-introspection-devel python3-cairo-devel gtk3 gtk3-devel
145+ zypper install -y python3 python3-pip python311-setuptools python3-devel gcc dmidecode gobject-introspection-devel python3-cairo-devel gtk3 gtk3-devel || exit 1
142146 ;;
143147 void)
144148 detected_distro " Void Linux"
145- xbps-install -Sy python3 python3-pip python3-devel python3-setuptools base-devel dmidecode cairo-devel gobject-introspection gcc gtk+3
149+ xbps-install -Sy python3 python3-pip python3-devel python3-setuptools base-devel dmidecode cairo-devel gobject-introspection gcc gtk+3 || exit 1
146150 ;;
147151 nixos)
148152 echo " NixOS detected"
@@ -152,116 +156,113 @@ elif [ -f /etc/os-release ];then
152156 ;;
153157 * ) manual_install;;
154158 esac
155- else # In case /etc/os-release doesn't exist
159+ else
156160 manual_install
157161 fi
158162
159163 header " Installing necessary Python packages"
160164
161- venv_dir=$VENV_PATH /venv
162- mkdir -p " $venv_dir "
163- python3 -m venv " $venv_dir "
165+ venv_dir=" $VENV_PATH /venv"
166+ mkdir -p " $venv_dir " || exit 1
167+ python3 -m venv " $venv_dir " || exit 1
164168
165- source " $venv_dir /bin/activate"
166- python3 -m pip install --upgrade pip wheel
169+ source " $venv_dir /bin/activate" || exit 1
170+ python3 -m pip install --upgrade pip wheel || exit 1
167171
168172
169173 # debian specific PyGObject Installation
170- if [ -f /etc/debian_version ]; then
171- VERSION=$( cat /etc/debian_version | cut -d' .' -f1)
174+ if [[ -s /etc/debian_version ]] ; then
175+ VERSION=$( cat /etc/debian_version | cut -d' .' -f1)
172176
173- if [[ " $VERSION " =~ ^12(\. [0-9]+)? $ ]]; then
174- python3 -m pip install PyGObject==3.50.0
177+ if [[ " $VERSION " =~ ^12(\. [0-9]+)? $ ]]; then
178+ python3 -m pip install PyGObject==3.50.0 || exit 1
175179 fi
176180 fi
177- python3 -m pip install PyGObject
181+ python3 -m pip install PyGObject || exit 1
178182
179183 header " Installing auto-cpufreq tool"
180-
181- git config --global --add safe.directory $( pwd)
182- python -m pip install .
183184
184- mkdir -p $SHARE_DIR
185- cp -r scripts/ $SHARE_DIR
186- cp -r images/ $SHARE_DIR
187- cp images/icon.png $IMG_FILE
188- cp scripts/$( basename $ORG_FILE ) $( dirname $ORG_FILE )
185+ git config --global --add safe.directory " $( pwd) "
186+ python -m pip install . || exit 1
187+
188+ mkdir -p -- " $SHARE_DIR " || exit 1
189+ cp -r scripts/ " $SHARE_DIR /" || exit 1
190+ cp -r images/ " $SHARE_DIR /" || exit 1
191+ cp images/icon.png " $IMG_FILE " || exit 1
192+ cp " scripts/$( basename -- " $ORG_FILE " ) " " $( dirname -- " $ORG_FILE " ) " || exit 1
189193
190194 # this is necessary since we need this script before we can run auto-cpufreq itself
191- cp scripts/auto-cpufreq-venv-wrapper $AUTO_CPUFREQ_FILE
192- chmod a+x $AUTO_CPUFREQ_FILE
193- cp scripts/start_app $AUTO_CPUFREQ_GTK_FILE
194- chmod a+x $AUTO_CPUFREQ_GTK_FILE
195-
196- desktop-file-install --dir=$APPLICATIONS_PATH scripts/$AUTO_CPUFREQ_GTK_DESKTOP_FILE
197- update-desktop-database $APPLICATIONS_PATH
198-
195+ cp scripts/auto-cpufreq-venv-wrapper " $AUTO_CPUFREQ_FILE " || exit 1
196+ chmod a+x " $AUTO_CPUFREQ_FILE " || exit 1
197+ cp scripts/start_app " $AUTO_CPUFREQ_GTK_FILE " || exit 1
198+ chmod a+x " $AUTO_CPUFREQ_GTK_FILE " || exit 1
199+
200+ desktop-file-install " --dir=$APPLICATIONS_PATH " " scripts/$AUTO_CPUFREQ_GTK_DESKTOP_FILE_NAME " || exit 1
201+ update-desktop-database " $APPLICATIONS_PATH " || exit 1
202+
199203 header " auto-cpufreq tool successfully installed"
200204 echo " For list of options, run:"
201205 echo " auto-cpufreq --help" ; echo
202206}
203207
204- function tool_remove {
208+ tool_remove () {
205209 # stop any running auto-cpufreq argument (daemon/live/monitor)
206- tool_arg_pids=( $( pgrep -f " auto-cpufreq --" ) )
207- for pid in " ${tool_arg_pids[@]} " ; do [ $pid != $$ ] && kill " $pid " ; done
210+ mapfile -t tool_arg_pids < <( pgrep -f ' auto-cpufreq --' | grep -vFx " $$ " )
211+ kill " ${tool_arg_pids[@]} "
208212
209- function remove_directory {
210- [ -d $1 ] && rm -rf $1
213+ remove_directory () {
214+ [[ -d " $1 " ]] && rm -rf " $1 "
211215 }
212- function remove_file {
213- [ -f $1 ] && rm $1
216+ remove_file () {
217+ [[ -f " $1 " ]] && rm " $1 "
214218 }
215219
216220 srv_remove=" $AUTO_CPUFREQ_FILE -remove"
217221
218222 # run uninstall in case of installed daemon
219- if [ -f $srv_remove -o -f $AUTO_CPUFREQ_FILE ]; then
220- eval " $AUTO_CPUFREQ_FILE --remove"
223+ if [[ -f " $srv_remove " || -f " $AUTO_CPUFREQ_FILE " ] ]; then
224+ " $AUTO_CPUFREQ_FILE " --remove || exit 1
221225 else
222- echo ; echo " Couldn't remove the auto-cpufreq daemon, $srv_remove do not exist."
226+ echo ; echo " Couldn't remove the auto-cpufreq daemon, [ $srv_remove ] does not exist."
223227 fi
224228
225229 # remove auto-cpufreq and all its supporting files
226- remove_directory $SHARE_DIR
230+ remove_directory " $SHARE_DIR "
227231
228232 remove_file " $AUTO_CPUFREQ_FILE -install"
229- remove_file $srv_remove
230- remove_file $AUTO_CPUFREQ_FILE
231- remove_file $AUTO_CPUFREQ_GTK_FILE
232- remove_file $IMG_FILE
233- remove_file $ORG_FILE
233+ remove_file " $srv_remove "
234+ remove_file " $AUTO_CPUFREQ_FILE "
235+ remove_file " $AUTO_CPUFREQ_GTK_FILE "
236+ remove_file " $IMG_FILE "
237+ remove_file " $ORG_FILE "
234238 remove_file " /usr/local/bin/cpufreqctl.auto-cpufreq"
235239 remove_file " /var/run/auto-cpufreq.stats"
236240
237- remove_file " $APPLICATIONS_PATH /$AUTO_CPUFREQ_GTK_DESKTOP_FILE "
238- update-desktop-database $APPLICATIONS_PATH
241+ remove_file " $APPLICATIONS_PATH /$AUTO_CPUFREQ_GTK_DESKTOP_FILE_NAME "
242+ update-desktop-database " $APPLICATIONS_PATH "
239243
240244 # remove python virtual environment
241- remove_directory $venv_path
245+ remove_directory " $VENV_PATH "
242246
243247 echo ; echo " auto-cpufreq tool and all its supporting files successfully removed" ; echo
244248}
245249
246250# root check
247- if (( EUID != 0 )) ; then
251+ if [[ " $ EUID" -ne 0 ]] ; then
248252 echo ; echo " Must be run as root (i.e: 'sudo $0 ')." ; echo
249253 exit 1
250254fi
251255
252- if [[ -z " $1 " ]]; then ask_operation
253- else
254- case " $1 " in
255- --install) answer=" i" ;;
256- --remove) answer=" r" ;;
257- * ) ask_operation;;
258- esac
259- fi
256+ case " $1 " in
257+ --install) OPERATION=i;;
258+ --remove) OPERATION=r;;
259+ * ) ask_operation;;
260+ esac
260261
261- case $answer in
262- I|i) tool_install;;
263- R|r) tool_remove;;
264- * )
262+ case " $OPERATION " in
263+ I|i) tool_install;;
264+ R|r) tool_remove;;
265+ * )
265266 echo " Unknown key, aborting ..." ; echo
266267 exit 1
267268 ;;
0 commit comments