#!/bin/bash # auto-cpufreq-installer: # auto-cpufreq source code based installer SCRIPT_PATH=$(readlink -f "$0") SCRIPT_DIR=$(dirname "${SCRIPT_PATH}") cd "${SCRIPT_DIR}" # check if lsb_release exists on the system before using it if command -v lsb_release > /dev/null then distro="$(lsb_release -is)" release="$(lsb_release -rs)" codename="$(lsb_release -cs)" fi sep="\n-------------------------------------------------------------------------------" # functions #separator function separator { echo -e $sep } # root check function root_check { if ((EUID != 0)); then separator echo -e "\nMust be run as root (i.e: 'sudo $0')." separator exit 1 fi } # python packages install function setup_venv { venv_dir=/opt/auto-cpufreq/venv mkdir -p "${venv_dir}" python3 -m venv "${venv_dir}" source "${venv_dir}/bin/activate" python3 -m pip install --upgrade pip wheel python3 -m pip install -r requirements.txt } # tool install function install { python3 setup.py install --record files.txt mkdir -p /usr/local/share/auto-cpufreq/ cp -r scripts/ /usr/local/share/auto-cpufreq/ # this is necessary since we need this script before we can run auto-cpufreq itself cp scripts/auto-cpufreq-venv-wrapper /usr/local/bin/auto-cpufreq chmod a+x /usr/local/bin/auto-cpufreq } # First argument is the distro function detected_distro() { echo -e "\nDetected $1 distribution" separator echo -e "\nSetting up Python environment\n" } # merged functions pip - install - complete_msg, since it repeats function completed () { echo -e "\nInstalling necessary Python packages\n" setup_venv separator echo -e "\ninstalling auto-cpufreq tool\n" install } function complete_msg() { separator echo -e "\nauto-cpufreq tool successfully installed.\n" echo -e "For list of options, run:\nauto-cpufreq --help" separator } function manual_install { echo -e "\nDidn't detect Debian or RedHat based distro.\n" echo -e "To complete installation, you need to:" echo -e "Install: python3, pip3, python3-setuptools\n" echo -e "Install necessary Python packages:" echo -e "pip3 install psutil click distro power" echo -e "\nRun following sequence of lines:" echo -e "\n-----" echo -e "\npython3 setup.py install --record files.txt" echo -e "mkdir -p /usr/local/share/auto-cpufreq/" echo -e "cp -r scripts/ /usr/local/share/auto-cpufreq/" echo -e "\n-----" echo -e "\nAfter which tool is installed, for full list of options run:" echo -e "auto-cpufreq" separator echo -e "\nConsider creating a feature request to add support for your distro:" echo -e "https://github.com/AdnanHodzic/auto-cpufreq/issues/new" echo -e "\nMake sure to include following information:\n" echo -e "Distribution: $distro" echo -e "Release: $release" echo -e "Codename: $codename" separator } function tool_install { separator if [ -f /etc/debian_version ]; then detected_distro "Debian based" apt install python3-dev python3-pip python3-venv python3-setuptools dmidecode -y completed complete_msg elif [ -f /etc/redhat-release ]; then detected_distro "RedHat based" if [ -f /etc/centos-release ]; then yum install platform-python-devel dmidecode else yum install python-devel dmidecode fi completed complete_msg elif [ -f /etc/solus-release ]; then detected_distro "Solus" eopkg install pip python3 python3-devel dmidecode eopkg install -c system.devel completed complete_msg elif [ -f /etc/os-release ];then eval "$(cat /etc/os-release)" separator case $ID in opensuse-leap) detected_distro "OpenSUSE" zypper install -y python3 python3-pip python3-setuptools python3-devel gcc dmidecode completed ;; opensuse) detected_distro "OpenSUSE" echo -e "\nDetected an OpenSUSE distribution\n\nSetting up Python environment\n" zypper install -y python38 python3-pip python3-setuptools python3-devel gcc dmidecode completed ;; arch|manjaro|endeavouros|garuda|artix) detected_distro "Arch Linux based" pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode completed ;; void) detected_distro "Void Linux" xbps-install -Sy python3 python3-pip python3-devel python3-setuptools base-devel dmidecode completed ;; *) #Any other distro manual_install completed ;; esac complete_msg else # In case /etc/os-release doesn't exist manual_install fi } function tool_remove { files="files.txt" share_dir="/usr/local/share/auto-cpufreq/" srv_install="/usr/bin/auto-cpufreq-install" srv_remove="/usr/bin/auto-cpufreq-remove" stats_file="/var/run/auto-cpufreq.stats" tool_proc_rm="/usr/local/bin/auto-cpufreq --remove" wrapper_script="/usr/local/bin/auto-cpufreq" unit_file="/etc/systemd/system/auto-cpufreq.service" venv_path="/opt/auto-cpufreq" cpufreqctl="/usr/bin/cpufreqctl.auto-cpufreq" # stop any running auto-cpufreq argument (daemon/live/monitor) tool_arg_pids=($(pgrep -f "auto-cpufreq --")) for pid in "${tool_arg_pids[@]}"; do if [[ $tool_arg_pids != $$ ]]; then kill "$tool_arg_pids" fi done # run uninstall in case of installed daemon if [ -f $srv_remove ]; then eval $tool_proc_rm fi # remove auto-cpufreq and all its supporting files [ -f $files ] && cat $files | xargs sudo rm -rf && rm -f $files [ -d $share_dir ] && rm -rf $share_dir # files cleanup [ -f $srv_install ] && rm $srv_install [ -f $srv_remove ] && rm $srv_remove [ -f $stats_file ] && rm $stats_file [ -f $unit_file ] && rm $unit_file [ -f $wrapper_script ] && rm $wrapper_script [ -f $cpufreqctl ] && rm $cpufreqctl # remove python virtual environment rm -rf "${venv_path}" separator echo -e "\nauto-cpufreq tool and all its supporting files successfully removed." separator } function ask_operation { echo -e "\n-------------------------- auto-cpufreq installer -----------------------------" echo -e "\nWelcome to auto-cpufreq tool installer." echo -e "\nOptions:\n" read -p \ "[I]nstall [R]emove [Q]uit Select a key: [i/r/q]: " answer } # End of functions root_check if [[ -z "${1}" ]]; then ask_operation else case "${1}" in "--install") answer="i" ;; "--remove") answer="r" ;; *) answer="n" ;; esac fi case $answer in I|i) tool_install ;; R|r) tool_remove ;; Q|q) separator echo "" exit 0 ;; *) separator echo -e "\nUnknown key, aborting ...\n" exit 1 ;; esac