fakesystemctl/fakesystemctl

238 lines
7.1 KiB
Bash

#!/bin/bash
############################################################################################
# fakesystemctl a hack helper script for Artix Linux #
#------------------------------------------------------------------------------------------#
# Created for Webmin software installation where is enforcing the use of systemctl command #
# and there is not any other option (way) to eliminate it (due to sudo / doas restrictions #
# to operate with bash exported functions), but it also can be used with gnome-extensions. #
# This is just a simple hack, that in no terms dhould be taken as an embracing the systemd,#
# by us, but vice versa, how easily can be tresspassed, when it is a actual barrier. #
# It may be pulled by other PKGBUILDS as a helper script, BUT IS NOT FOR PACKAGING, AS IS #
# Webmin, Virtualmin, gnome-shell-extensions are some cases that it must be used. #
# (if the above changes, this README and code, will be modified accordingly) #
#------------------------------------------------------------------------------------------#
# Licence: GPL-2.0-only https://spdx.org/licenses/GPL-2.0-only.html #
# Copyright (c) 2021 Linuxer <linuxer@artixlinux.org> #
# Release 1.0.0 #
# Code complies with shellcheck https://www.shellcheck.net #
############################################################################################
# functions definitions
get_init() {
openrc="$(pidof init)"
runit="$(pidof runit)"
s6="$(pidof s6-svscan)"
if [ "$openrc" = "1" ]; then
export init_sys="openrc"
elif [ "$runit" = "1" ]; then
export init_sys="runit"
elif [ "$s6" = "1" ]; then
export init_sys="s6"
fi
}
enable() {
if [ "$init_sys" = "openrc" ]; then
rc-update add "$service default"
elif [ "$init_sys" = "runit" ]; then
ln -s "/etc/runit/sv/$service" "run/runit/service"
elif [ "$init_sys" = "s6" ]; then
sh /usr/share/libalpm/scripts/s6-rc-db-update-hook "$service"
fi
}
disable() {
if [ "$init_sys" = "openrc" ]; then
rc-update del "$service"
elif [ "$init_sys" = "runit" ]; then
unlink "run/runit/service/$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc -d change "$service"
fi
}
start() {
if [ "$init_sys" = "openrc" ]; then
rc-update add "/etc/runit/sv/$service default" && pgrep -a "$service"
elif [ "$init_sys" = "runit" ]; then
sv up "$service" && pgrep -a "$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc -u change "$service" && pgrep -a "$service"
fi
}
status() {
if [ "$init_sys" = "openrc" ]; then
rc-update show -v | grep "$service" && pgrep -a "$service"
elif [ "$init_sys" = "runit" ]; then
sv status "$service" && pgrep -a "$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc -a list "$service" | grep "$service" && pgrep -a "$service"
fi
}
stop(){
if [ "$init_sys" = "openrc" ]; then
rc-update del "$service" default
elif [ "$init_sys" = "runit" ]; then
sv stop "$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc-bundle-update delete default "$service"
fi
}
hibernate() {
loginctl hibernate
}
reboot(){
loginctl reboot
}
shutdown() {
loginctl poweroff
}
suspend() {
loginctl susppend
}
setcolors() {
local opt=$1
local colors
if [[ $opt == auto ]]; then
# no colors if stdout is not a TTY
if [[ ! -t 1 ]]; then
opt='off'
else
# stdout is a tty, check tput capability for colors
colors=$(tput colors 2>/dev/null || echo -1)
if ! [[ $colors =~ $num_re ]]; then
fatal "failed to parse output of \`tput colors\` ($colors)"
fi
if ((colors >= 8)); then
opt='on'
else
opt='off'
fi
fi
fi
case "$opt" in
on|yes|true)
#colorred=$(tput setaf 1)
colorgreen=$(tput setaf 2)
#coloryellow=$(tput setaf 3)
#colorblue=$(tput setaf 4)
colormagenta=$(tput setaf 5)
colorcyan=$(tput setaf 6)
#colorwhite=$(tput setaf 7)
#colorgray=$(tput setaf 8)
#colorbold=$(tput bold)
#colorreset=$(tput sgr0)
;;
off|no|false)
#colorred=
colorgreen=
#coloryellow=
colormagenta=
colorcyan=
#colorwhite=
#colorgray=
#colorbold=
#colorreset=
;;
*)
echo "unknown color option: '$opt'" >&2
exit 1
;;
esac
}
print_help() {
printf "\n%20s\n" "${colormagenta}---------------------------------------------------------------------------------"
printf "Artix Linux is a systemd-free distribution, so this is a hack systemctl command,\n"
printf "(c) is held by Linuxer <linuxer@artixlinux.org>, to bypass special cases of systemd deps\n"
printf "Licence: GPL-2.0-only https://spdx.org/licenses/GPL-2.0-only.html \n"
printf "Sources: https://gitea.artixlinux.org/linuxer/fakesystemctl\n"
printf "%s\n" "---------------------------------------------------------------------------------"
printf "\n%s\n" "${colorcyan}Available commands are:"
printf "\n%s\n" "${colorgreen}${tab}systemctl enable <service>"
printf "%s\n" "${tab}systemctl start <service>"
printf "%s\n" "${tab}systemctl restart <service>"
printf "%s\n" "${tab}systemctl stop <service>"
printf "%s\n" "${tab}systemctl status <service>"
printf "%s\n" "${tab}systemctl disable <service>"
printf "%s\n" "${tab}systemctl hibernate"
printf "%s\n" "${tab}systemctl reboot"
printf "%s\n" "${tab}systemctl poweroff"
printf "%s\n" "${tab}systemctl suspend"
}
# the main body of the script
num_re='^-?[0-9]+$'
tab=' '
setcolors "yes";
get_init
serviceinput="$2"
# clean up the .service suffix on user input
forbidden='.service'
if [[ "$serviceinput" =~ .*"$forbidden".* ]]; then
service=${serviceinput%.*}
else
service="$serviceinput"
fi
# main script flow
if [ -z "$1" ]; then
printf "\n%20s\n" "${colormagenta}---------------------------------------------------------------------------------"
printf "Artix Linux is a systemd-free distribution, so this is a hack systemctl command,\n"
printf "(c) is held by Linuxer <linuxer@artixlinux.org>, to bypass special cases of systemd deps\n"
printf "Licence: GPL-2.0-only https://spdx.org/licenses/GPL-2.0-only.html \n"
printf "Sources: https://gitea.artixlinux.org/linuxer/fakesystemctl\n"
printf "%s\n" "---------------------------------------------------------------------------------"
elif [ "$1" = "hibernate" ]; then
hibernate
elif [ "$1" = "reboot" ]; then
reboot
elif [ "$1" = "shutdown" ]; then
poweroff
elif [ "$1" = "suspend" ]; then
suspend
elif [ "$1" = "enable" ]; then
enable
elif [ "$1" = "disable" ]; then
disable
elif [ "$1" = "start" ]; then
start
elif [ "$1" = "restart" ]; then
start
elif [ "$1" = "status" ]; then
status
elif [ "$1" = "stop" ]; then
stop
elif [ "$1" = "-h" ]; then
print_help
elif [ "$1" = "--help" ]; then
print_help
else
printf '%s' "ERROR: wrong command detected:"
printf '%s' "$@"
printf "\n"
return 1
fi
############################################################################################
# Code End #
############################################################################################