From 05c07344820d3c3fa70cb48f0aae8ef78f39576a Mon Sep 17 00:00:00 2001 From: Sebastian Andersson Date: Thu, 14 Mar 2024 15:15:09 +0100 Subject: [PATCH] Make write_tags work when called from other dirs Also make the service be restarted if the program is stopped with ctrl-c --- write_tags | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/write_tags b/write_tags index 42d86ba..28cd5a1 100755 --- a/write_tags +++ b/write_tags @@ -1,9 +1,27 @@ -#!/bin/sh +#!/usr/bin/env bash # SPDX-FileCopyrightText: 2024 Sebastian Andersson # # SPDX-License-Identifier: CC0-1.0 +if [[ ${BASH_SOURCE[0]} = */* ]]; then + cd -- "${BASH_SOURCE%/*}/" || exit +fi + +if [ ! -d venv ]; then + echo "Creating python virtual environment" + virtualenv -p python3 venv || rm -r venv + echo "Installing python requirements" + venv/bin/pip3 install -r requirements.txt || exit +fi + +restart_service() { + trap SIGINT + systemctl start nfc2klipper.service + exit +} + systemctl stop nfc2klipper.service -./venv/bin/python3 ./write_tags.py || true -systemctl start nfc2klipper.service +trap "restart_service" INT +venv/bin/python3 ./write_tags.py || true +restart_service