Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenRC support and install.sh rewrite #59

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions razer_control_gui/data/services/openrc/razercontrol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/sbin/openrc-run

name="razerdaemon"
command="/usr/share/razercontrol/daemon"
pidfile="${XDG_RUNTIME_DIR}/${RC_SVCNAME}.pid"
output="/var/log/razercontrol.log"

depend() {
need localmount udev
}

start() {
# prepare the log file
touch "$output"
chown USERNAME_CHANGEME "$output"

ebegin "Starting ${RC_SVCNAME}"
start-stop-daemon --start --user USERNAME_CHANGEME --background --make-pidfile --pidfile "$pidfile" --exec "$command" --stdout "$output" --stderr "$output"
eend $?
}

stop() {
ebegin "Stopping ${RC_SVCNAME}"
start-stop-daemon --stop --pidfile "$pidfile"
eend $?
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ ExecStart=/usr/share/razercontrol/daemon

[Install]
WantedBy=default.target

162 changes: 106 additions & 56 deletions razer_control_gui/install.sh
Original file line number Diff line number Diff line change
@@ -1,85 +1,135 @@
#!/bin/bash
#!/usr/bin/env bash

if [ "$EUID" -eq 0 ]
then echo "Please do not run as root"
exit
fi
detect_init_system() {
if pidof systemd 1>/dev/null 2>/dev/null; then
INIT_SYSTEM="systemd"
elif [ -f "/sbin/rc-update" ]; then
INIT_SYSTEM="openrc"
else
INIT_SYSTEM="other"
fi
}

install() {
echo "Building the project..."
cargo build --release

if [[ -z "$@" ]];then
echo "usage: |install|uninstall|"
exit -1
fi
if [ $? -ne 0 ]; then
echo "An error occurred while building the project"
exit 1
fi

# Build the project
echo "Building the project..."
cargo build --release
# Stop the service if it's running
echo "Stopping the service..."
case $INIT_SYSTEM in
systemd)
systemctl --user stop razercontrol
;;
openrc)
sudo rc-service razercontrol stop
;;
esac

# Check if the build was successful
if [ $? -ne 0 ]; then
echo "Build failed, exiting."
exit 1
fi
# Install the files
echo "Installing the files..."
mkdir -p ~/.local/share/razercontrol
sudo bash <<EOF
mkdir -p /usr/share/razercontrol
cp target/release/razer-cli /usr/bin/
cp target/release/daemon /usr/share/razercontrol/
cp data/devices/laptops.json /usr/share/razercontrol/
cp data/udev/99-hidraw-permissions.rules /etc/udev/rules.d/
udevadm control --reload-rules
EOF

echo "Stopping razerdaemon service..."
systemctl --user stop razerdaemon.service
if [ $? -ne 0 ]; then
echo "An error occurred while installing the files"
exit 1
fi

uninstall() {
sudo /bin/bash <<EOF
rm -rf /usr/share/razercontrol
rm -f /usr/bin/razer-cli
rm -f /etc/udev/rules.d/99-hidraw-permissions.rules
rm -f /usr/lib/systemd/user/razerdaemon.service
udevadm control --reload-rules
# Start the service
echo "Starting the service..."
case $INIT_SYSTEM in
systemd)
sudo cp data/services/systemd/razercontrol.service /etc/systemd/system/
systemctl --user enable --now razercontrol
;;
openrc)
sudo bash <<EOF
cp data/services/openrc/razercontrol /etc/init.d/
# HACK: Change the username in the script
sed -i 's/USERNAME_CHANGEME/$USER/' /etc/init.d/razercontrol

chmod +x /etc/init.d/razercontrol
rc-update add razercontrol default
rc-service razercontrol start
EOF
;;
esac

echo "Installation complete"
}
install() {
echo "Creating directories, copying files, and setting up services..."
mkdir -p ~/.local/share/razercontrol
sudo /bin/bash <<EOF
mkdir -p /usr/share/razercontrol
cp target/release/razer-cli /usr/bin/
cp target/release/daemon /usr/share/razercontrol/
cp data/devices/laptops.json /usr/share/razercontrol/
cp data/udev/99-hidraw-permissions.rules /etc/udev/rules.d/
cp razerdaemon.service /usr/lib/systemd/user/
udevadm control --reload-rules

uninstall() {
# Remove the files
echo "Uninstalling the files..."
sudo bash <<EOF
rm -f /usr/bin/razer-cli
rm -f /usr/share/razercontrol/daemon
rm -f /usr/share/razercontrol/laptops.json
rm -f /etc/udev/rules.d/99-hidraw-permissions.rules
udevadm control --reload-rules
EOF

# Check if the previous commands were successful
if [ $? -ne 0 ]; then
echo "An error occurred while setting up, exiting."
echo "An error occurred while uninstalling the files"
exit 1
fi

echo "Enabling razerdaemon service..."
systemctl --user enable razerdaemon.service
# Stop the service
echo "Stopping the service..."
case $INIT_SYSTEM in
systemd)
systemctl --user disable --now razercontrol
rm -f /etc/systemd/system/razercontrol.service
;;
openrc)
sudo bash <<EOF
rc-service razercontrol stop
rc-update del razercontrol default
rm -f /etc/init.d/razercontrol
EOF
;;
esac

echo "Starting razerdaemon service..."
systemctl --user start razerdaemon.service
echo "Uninstalled"
}

# Check if the service started successfully
if [ $? -ne 0 ]; then
echo "Failed to start razerdaemon service, exiting."
main() {
if [ "$EUID" -eq 0 ]; then
echo "Please do not run as root"
exit 1
fi

echo "Install complete!"
detect_init_system

return $?
}
if [ "$INIT_SYSTEM" = "other" ]; then
echo "Unsupported init system"
exit 1
fi

case "$@" in
"install")
case $1 in
install)
install
;;
"uninstall")
uninstall)
uninstall
;;
*)
echo "unknown arg $@"
exit -1
echo "Usage: $0 {install|uninstall}"
exit 1
;;
esac
esac
}

exit $?
main $@