#!/bin/sh
# tuptime - Report the historical and statistical real time of the system, keeping it between restarts.
# Author Ricardo Fraile - 2020

# 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, either version 2 of the License, or
# (at your option) any later version.

# 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 <http://www.gnu.org/licenses/>.
#

### BEGIN INIT INFO
# Provides:          tuptime
# Required-Start:    $local_fs $time $remote_fs
# Required-Stop:     $local_fs $time $remote_fs
# Should-Start:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start tuptime at boot time
# Description:       Update tuptime.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions

USER='_tuptime'
BIN_NAME='tuptime'
PATH_BIN="/usr/bin/$BIN_NAME"


do_start () {
        # Start service
        log_action_begin_msg "Starting $BIN_NAME"
        log_end_msg 0
        su -s /bin/sh $USER -c "$PATH_BIN -x" > /dev/null
}

do_stop () {
        # Stop service
        log_action_begin_msg "Stopping $BIN_NAME"
        log_end_msg 0
        su -s /bin/sh $USER -c "$PATH_BIN -xg" > /dev/null
}

do_status () {
        # Status service
        su -s /bin/sh $USER -c "$PATH_BIN"
}


case "$1" in
  start|"")
        do_start
        ;;
  restart|force-reload)
        log_action_begin_msg "Restarting $BIN_NAME"
        log_end_msg 0
        do_stop
        do_start
        ;;
  stop)
        do_stop
        ;;
  status)
        do_status
        exit $?
        ;;
  *)
        echo "Usage: $BIN_NAME [start|stop|restart|status]" >&2
        exit 3
        ;;
esac

