quality-time.git

ref: master

./sqt


#! /bin/bash

#  This file is part of 'Quality Time'
#  Copyright (c) 2025 Paolo Lulli.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, version 3.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program. If not, see .

SYSDIR=$HOME/.config/systemd/user
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

function usage(){
    echo "$(basename $0) "
    echo ""
    echo "$(basename $0) -c|--create   : Creates a new service .service"
    echo "$(basename $0) -L|--list-services     : List all of the user services"
    echo "$(basename $0) -l|--log      : Get logs for .service"
    echo "$(basename $0) -x|--status   : Status for .service"
    echo "$(basename $0) -s|--start    : Starts .service"
    echo "$(basename $0) -S|--stop     : Stops .service"
    echo "$(basename $0) -e|--enable   : Enables .service"
    echo "$(basename $0) -d|--disable  : Disables .service"
}

option=$1
params=${@:2}

function service_log()
{
    service="$1"
    cmd="journalctl --user --no-pager -u ${service}.service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_listall()
{
    service="$1"
    cmd="systemctl --user --type=service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_status()
{
    service="$1"
    cmd="systemctl --user status ${service}.service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_start()
{
    service="$1"
    cmd="systemctl --user start ${service}.service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_stop()
{
    service="$1"
    cmd="systemctl --user stop ${service}.service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_enable()
{
    service="$1"
    cmd="systemctl --user enable ${service}.service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_disable()
{
    service="$1"
    cmd="systemctl --user disable ${service}.service"
    echo "Cmd: [${green}${cmd}${reset}]"

    eval "${cmd}"
}

function service_create()
{
    service="$1"
    unit_name=${SYSDIR}/${service}.service
    script_name=${HOME}/bin/service-${service}.sh

    test -f ${unit_name} && echo "File exists: ${unit_name}"
    test -f ${unit_name} && exit 1

    cat<<__EOT__>${unit_name}

   [Unit]

   Description=Run ${service}
   DefaultDependencies=no
   After=network.target

   [Service]

   Type=simple
   ExecStart=%h/bin/service-${service}.sh
   TimeoutStartSec=0
   Restart=on-failure
   RemainAfterExit=yes
   #StandardOutput=file:%h/log_file

   [Install]

   WantedBy=default.target
__EOT__

    echo "Created unit: [${unit_name}]"

    test -f ${script_name} && exit 0
    cat<<__EOT__>${script_name}
   #! /bin/bash
   #
   echo "START ${service} CALLED"

   # Edit here start of  ${service}"

__EOT__

    chmod 755 ${script_name}
    eval ${cmd}
}


if [ "$option" = "-c" ] || [ "$option" = "--create" ]; then
    service_create "${params}"
    exit 0
fi

if [ "$option" = "-l" ] || [ "$option" = "--log" ]; then
    service_log "${params}"
    exit 0
fi
if [ "$option" = "-x" ] || [ "$option" = "--status" ]; then
    service_status "${params}"
    exit 0
fi
if [ "$option" = "-s" ] || [ "$option" = "--start" ]; then
    service_start "${params}"
    exit 0
fi
if [ "$option" = "-S" ] || [ "$option" = "--stop" ]; then
    service_stop "${params}"
    exit 0
fi

if [ "$option" = "-e" ] || [ "$option" = "--enable" ]; then
    service_enable "${params}"
    exit 0
fi
if [ "$option" = "-d" ] || [ "$option" = "--disable" ]; then
    service_disable "${params}"
    exit 0
fi
if [ "$option" = "-L" ] || [ "$option" = "--list-services" ]; then
    service_listall
    exit 0
fi

usage;