An extensible task executor for Sapphire Linux and other Linux distros, written in Bash.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
sapphire-butler/butler

168 lines
7.1 KiB

#!/bin/bash
# sapphire-butler, a task executor for Sapphire GLS.
#
# This program has preconfigured tasks that are categorized into
# departments.
#
# Copyright (C) 2022 Matthew Evan
#
# This program is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# Get language
BLANG=$(echo $LANG | cut -c1-2)
# Set default conf variables
enable_notifications="true"
enable_espeak="false"
aur_helper="pikaur"
# Obtain custom configurations
if [ -f "${HOME}/.config/sapphire-butler.conf" ]; then
source ${HOME}/.config/sapphire-butler.conf
elif [ -f "/etc/sapphire-butler.conf" ]; then
source /etc/sapphire-butler.conf
fi
m_help() {
# Display help message
echo "sapphire-butler, a task executor for Sapphire GLS."
echo
echo "This program has preconfigured tasks that are categorized into"
echo "departments."
echo
echo "Copyright (C) 2022 Matthew Evan"
echo
echo "This program is free software; you can redistribute it and/or modify"
echo "under the terms of the GNU General Public License as published by"
echo "Free Software Foundation; either version 3 of the License, or"
echo "(at your option) any later version."
echo
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software Foundation,"
echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA"
echo
echo "Syntax: butler [DEPARTMENT/TASK]"
echo "Available departments:"
echo "admin Administrative Tasks"
echo "aux Auxiliary Tasks"
echo "fun Fun Tasks"
echo "net Network/Internet Tasks"
echo "pkg Package Management Tasks"
echo "prod Productivity Tasks"
echo
echo "Specify a department without a task to see tasks in that department."
echo
echo "A yad powered GUI interface is available with this command:"
echo "butler prod/butleryad"
}
butlerlang() {
# Obtain variable data from language files
if [ -f "/usr/share/sapphire-butler/i18n/$1/$BLANG" ]; then
source /usr/share/sapphire-butler/i18n/$1/$BLANG
elif [ -f "/usr/share/sapphire-butler/i18n/$1/en" ]; then
source /usr/share/sapphire-butler/i18n/$1/en
else
source /usr/share/sapphire-butler/i18n/$1/$(find -type f /usr/share/sapphire-butler/i18n/$1/ | head -1)
fi
}
butlerrun() {
butlerlang $1 # Get i18nized text
if [ -f "/usr/share/sapphire-butler/departments/$1/dependencies.list" ]; then # Check if the specified task has a dependency list
declare -a dependencies=($(< /usr/share/sapphire-butler/departments/$1/dependencies.list)) # Put those dependencies into an array
depnum=0
reqdeps=()
for package in ${dependencies[@]}; do
if pacman -Q ${package} &> /dev/null; then # If the package is installed,
depnum=$(echo $depnum + 1 | bc) # Add one to the amount of dependencies we have installed
else # if not
reqdeps+=("$package") # add that to the list of packages we need to install
fi
done
if [ ${#reqdeps} != 0 ]; then # If the user needs to install any dependencies, try to get the user to install them.
butlermsg "${depnum} dependencies found. Task \"$name\" needs ${#reqdeps[@]} more dependencies..."
sudo pacman -S ${reqdeps[@]}
if [ $? != 0 ]; then # If the pacman process did not successfully run, do not continue.
butlermsg "Package transfer failed. Not running task."
exit
fi
fi
fi
# Runs the task
butlermsg "Running task \"$name\"..."
source /usr/share/sapphire-butler/departments/$1/task.sh
butlermsg "Task \"$name\" completed."
}
butlermsg() {
# Displays the requested message according to the user's settings
echo -e "\033[01;36m[❖ BUTLER]\033[0m $1"
if [ "$enable_notifications" == "true" ]; then
if [ -f "/usr/bin/notify-send" ]; then
notify-send -u low "Sapphire Butler" "$1"
fi
fi
if [ "$enable_espeak" == "true" ]; then
if [ -f "/usr/bin/espeak" ]; then
espeak "$1" # "Cool" haxor kids use espeak.
fi
fi
}
# Check if the user has supplied an argument
if [ -n "$1" ]; then
taskstodo=$@ # Set this for auxiliary.
# Find out what the user wants for each argument.
for tasktodo in ${taskstodo[@]}; do
# If the user has supplied a task as an argument, run it
if [ -f "/usr/share/sapphire-butler/departments/$tasktodo/task.sh" ]; then
butlerrun "$tasktodo"
# If the user has supplied a department as an arguement, list the tasks in that department
elif [ -d "/usr/share/sapphire-butler/departments/$tasktodo" ]; then
echo -e "\033[01;36m[❖ BUTLER]\033[0m Available tasks in the $tasktodo department:"
declare -a tasksearchresults=($(find "/usr/share/sapphire-butler/departments/$tasktodo/" -name task.sh | sed -e 's/\/usr\/share\/sapphire-butler\/departments\///g' -e 's/\/task.sh//g' | sort))
printf "\t%-35s %-30s" "Name" "Department/Task"
echo -e "\n-----------------------------------------------------------------"
for tasksearch in ${tasksearchresults[@]}; do
butlerlang $tasksearch
printf "%-35s %-30s" "$name" "$tasksearch"
echo
done
else
# The user did not supply a department or task, so check if the user supplied only a task name without the department
didyoumean=$(find "/usr/share/sapphire-butler/departments/" -name "$tasktodo" | sed 's/\/usr\/share\/sapphire-butler\/departments\///g')
# If the user supplied a task name without the department, recommend them to run it with the department
# Actually we just run that task we found because why not.
if [ -f "/usr/share/sapphire-butler/departments/$didyoumean/task.sh" ]; then
#echo -e "\033[01;36m[❖ BUTLER]\033[0m Did you mean to run:" # yeet
#echo "butler $didyoumean"
butlerrun $didyoumean
else
# The user did not supply a valid department or task
echo -e "\033[01;36m[❖ BUTLER]\033[0m $tasktodo is not a valid department or task."
fi
fi
done
else
# If no arguements were passed, display the help message
m_help
fi