Skip to content
Merged
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
34 changes: 29 additions & 5 deletions launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ BUTTON_LOG="$progdir/log/buttons.log"

SERVICE_NAME="screenshot-monitor"
HUMAN_READABLE_NAME="Screenshot Monitor"
SUPPORTS_DAEMON_MODE=0
ONLY_LAUNCH_THEN_EXIT=1
LAUNCHES_SCRIPT="true"
service_on() {
cd "$SDCARD_PATH" || exit 1
if [ -f "$progdir/log/service.log" ]; then
Expand All @@ -28,7 +29,16 @@ service_on() {
hotkey="HOTKEY_1"
fi

service_off

show_message "Enabling the $HUMAN_READABLE_NAME" 2
PROGDIR="$progdir" HOTKEY="$hotkey" "$progdir/bin/screenshot-monitor" >"$progdir/log/service.log" 2>&1 &
if ! wait_for_service 10; then
show_message "Failed to start $HUMAN_READABLE_NAME" 2
return 1
fi

show_message "Started $HUMAN_READABLE_NAME" 2
}

service_off() {
Expand Down Expand Up @@ -130,11 +140,25 @@ wait_for_button() {
done
}

is_service_running() {
if pgrep "$SERVICE_NAME" >/dev/null 2>&1; then
return 0
fi

if [ "$LAUNCHES_SCRIPT" = "true" ]; then
if pgrep -fn "$SERVICE_NAME" >/dev/null 2>&1; then
return 0
fi
fi

return 1
}

wait_for_service() {
max_counter="$1"
counter=0

while ! pgrep "$SERVICE_NAME" >/dev/null 2>&1; do
while ! is_service_running; do
counter=$((counter + 1))
if [ "$counter" -gt "$max_counter" ]; then
return 1
Expand All @@ -145,7 +169,7 @@ wait_for_service() {

main_daemonize() {
echo "Toggling $SERVICE_NAME..."
if pgrep "$SERVICE_NAME"; then
if is_service_running; then
show_message "Disabling the $HUMAN_READABLE_NAME" 2
service_off
else
Expand All @@ -162,7 +186,7 @@ main_daemonize() {
}

main_process() {
if pgrep "$SERVICE_NAME"; then
if is_service_running; then
show_message "Disabling the $HUMAN_READABLE_NAME" 2
service_off
fi
Expand Down Expand Up @@ -190,7 +214,7 @@ main_process() {
}

main() {
if [ "$SUPPORTS_DAEMON_MODE" -eq 0 ]; then
if [ "$ONLY_LAUNCH_THEN_EXIT" -eq 1 ]; then
service_on
return $?
fi
Expand Down