diff --git a/serial/install.sh b/serial/install.sh index 7fe8471..2e57dfc 100644 --- a/serial/install.sh +++ b/serial/install.sh @@ -1,8 +1,15 @@ #!/bin/bash if [ "$EUID" -eq 0 ]; then - echo "Please do not run as root" - exit + read -r -p "Are you sure you want to run this service as root? [y/N] " response + response=${response,,} # tolower + if ! [[ "$response" =~ ^(yes|y)$ ]]; then + exit + fi + SERVICE_PATH="/etc/systemd/system/cyd-klipper-serial.service" +else + mkdir -p ~/.config/systemd/user + SERVICE_PATH="~/.config/systemd/user/cyd-klipper-serial.service" fi set -e @@ -15,20 +22,26 @@ source ./env/bin/activate pip3 install -r requirements.txt # Create systemd unit file -mkdir -p ~/.config/systemd/user -echo "[Unit]" > ~/.config/systemd/user/cyd-klipper-serial.service -echo "Description=CYD Klipper serial server" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "After=network.target" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "[Service]" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "ExecStart=$(pwd)/run.sh" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "WorkingDirectory=$(pwd)" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "Restart=always" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "[Install]" >> ~/.config/systemd/user/cyd-klipper-serial.service -echo "WantedBy=multi-user.target" >> ~/.config/systemd/user/cyd-klipper-serial.service + +echo "[Unit]" > $SERVICE_PATH +echo "Description=CYD Klipper serial server" >> $SERVICE_PATH +echo "After=network.target" >> $SERVICE_PATH +echo "" >> $SERVICE_PATH +echo "[Service]" >> $SERVICE_PATH +echo "ExecStart=$(pwd)/run.sh" >> $SERVICE_PATH +echo "WorkingDirectory=$(pwd)" >> $SERVICE_PATH +echo "Restart=always" >> $SERVICE_PATH +echo "" >> $SERVICE_PATH +echo "[Install]" >> $SERVICE_PATH +echo "WantedBy=multi-user.target" >> $SERVICE_PATH # Start the service -systemctl --user daemon-reload -systemctl --user enable cyd-klipper-serial -systemctl --user start cyd-klipper-serial \ No newline at end of file +if [ "$EUID" -eq 0 ]; then + systemctl daemon-reload + systemctl enable cyd-klipper-serial + systemctl start cyd-klipper-serial +else + systemctl --user daemon-reload + systemctl --user enable cyd-klipper-serial + systemctl --user start cyd-klipper-serial +fi \ No newline at end of file diff --git a/serial/uninstall.sh b/serial/uninstall.sh index 02735b2..34d210c 100644 --- a/serial/uninstall.sh +++ b/serial/uninstall.sh @@ -1,13 +1,19 @@ #!/bin/bash +set -e + if [ "$EUID" -eq 0 ]; then - echo "Please do not run as root" - exit -fi + systemctl stop cyd-klipper-serial + systemctl disable cyd-klipper-serial -systemctl --user stop cyd-klipper-serial -systemctl --user disable cyd-klipper-serial + rm /etc/systemd/system/cyd-klipper-serial.service -rm ~/.config/systemd/user/cyd-klipper-serial.service + systemctl daemon-reload +else + systemctl --user stop cyd-klipper-serial + systemctl --user disable cyd-klipper-serial -systemctl --user daemon-reload \ No newline at end of file + rm ~/.config/systemd/user/cyd-klipper-serial.service + + systemctl --user daemon-reload +fi \ No newline at end of file