forked from mainsail-crew/moonraker-timelapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·153 lines (132 loc) · 4.25 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Moonraker Timelapse component installer
#
# Copyright (C) 2021 Christoph Frei <fryakatkop@gmail.com>
# Copyright (C) 2021 Stephan Wendel aka KwadFan <me@stephanwe.de>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
#
# Note:
# this installer script is heavily inspired by
# https://github.com/protoloft/klipper_z_calibration/blob/master/install.sh
# Prevent running as root.
if [ ${UID} == 0 ]; then
echo -e "DO NOT RUN THIS SCRIPT AS 'root' !"
echo -e "If 'root' privileges needed, you will prompted for sudo password."
exit 1
fi
# Force script to exit if an error occurs
set -e
# Find SRCDIR from the pathname of this script
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/ && pwd )"
# Default Parameters
MOONRAKER_TARGET_DIR="${HOME}/moonraker/moonraker/components"
SYSTEMDDIR="/etc/systemd/system"
KLIPPER_CONFIG_DIR="${HOME}/klipper_config"
FFMPEG_BIN="/usr/bin/ffmpeg"
# Define text colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
function stop_klipper {
if [ "$(sudo systemctl list-units --full -all -t service --no-legend | grep -F "klipper.service")" ]; then
echo "Klipper service found! Stopping during Install."
sudo systemctl stop klipper
else
echo "Klipper service not found, please install Klipper first"
exit 1
fi
}
function stop_moonraker {
if [ "$(sudo systemctl list-units --full -all -t service --no-legend | grep -F "moonraker.service")" ]; then
echo "Moonraker service found! Stopping during Install."
sudo systemctl stop moonraker
else
echo "Moonraker service not found, please install Moonraker first"
exit 1
fi
}
function link_extension {
if [ -d "${MOONRAKER_TARGET_DIR}" ]; then
echo "Linking extension to moonraker..."
ln -sf "${SRCDIR}/component/timelapse.py" "${MOONRAKER_TARGET_DIR}/timelapse.py"
else
echo -e "ERROR: ${MOONRAKER_TARGET_DIR} not found."
echo -e "Please Install moonraker first!\nExiting..."
exit 1
fi
if [ -d "${KLIPPER_CONFIG_DIR}" ]; then
echo "Linking macro file..."
ln -sf "${SRCDIR}/klipper_macro/timelapse.cfg" "${KLIPPER_CONFIG_DIR}/timelapse.cfg"
else
echo -e "ERROR: ${KLIPPER_CONFIG_DIR} not found."
echo -e "Try:\nUsage: ${0} -c /path/to/klipper_config\nExiting..."
exit 1
fi
}
function install_script {
# Create systemd service file
SERVICE_FILE="${SYSTEMDDIR}/timelapse.service"
#[ -f $SERVICE_FILE ] && return
if [ -f $SERVICE_FILE ]; then
# Force remove
sudo rm -f "$SERVICE_FILE"
fi
echo "Installing system start script..."
sudo /bin/sh -c "cat > ${SERVICE_FILE}" << EOF
[Unit]
Description=Dummy Service for timelapse plugin
After=moonraker.service
Wants=moonraker.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "sleep 1; echo 'Restarting Klipper and Moonraker...'"
ExecStopPost=systemctl restart klipper
ExecStopPost=systemctl restart moonraker
TimeoutStopSec=1s
[Install]
WantedBy=multi-user.target
EOF
# Use systemctl to enable the systemd service script
sudo systemctl daemon-reload
sudo systemctl enable timelapse.service
}
function restart_services {
echo "Restarting Moonraker..."
sudo systemctl restart moonraker
echo "Restarting Klipper..."
sudo systemctl restart klipper
}
function check_ffmpeg {
if [ ! -f "$FFMPEG_BIN" ]; then
echo -e "${YELLOW}WARNING:${NC} FFMPEG not found in '${FFMPEG_BIN}'. Render will not be possible!\nPlease install FFMPEG running:\n\n sudo apt install ffmpeg\n\nor specify 'ffmpeg_binary_path' in moonraker.conf in the [timelapse] section if ffmpeg is installed in a different directory, to use render functionality"
fi
}
### MAIN
# Parse command line arguments
while getopts "c:h" arg; do
if [ -n "${arg}" ]; then
case $arg in
c)
KLIPPER_CONFIG_DIR=$OPTARG
break
;;
[?]|h)
echo -e "\nUsage: ${0} -c /path/to/klipper_config"
exit 1
;;
esac
fi
break
done
# Run steps
stop_klipper
stop_moonraker
link_extension
install_script
restart_services
check_ffmpeg
# If something checks status of install
exit 0