#!/bin/bash

# file: is_stopped.sh
# bk_version 26.01.1

# Copyright (C) 2017-2026 Richard Albrecht
# www.rleofield.de
# 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 3 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/>.
#------------------------------------------------------------------------------       



. ./cfg.working_folder

. ./src_exitcodes.sh
. ./src_filenames.sh
. ./src_log.sh

cd $bv_workingfolder
if [ ! -d $bv_workingfolder ] && [ ! $( pwd ) = $bv_workingfolder ]
then
        echo "WD '$bv_workingfolder'"
        echo "WD is wrong"
        exit 1
fi


# BK_WAITING=70  
# BK_STOPPED=71

# BK_EXECONCESTOPPED=72
# BK_WAITINTERVAL=73


# only used if rsync is running
# BK_RUNNING=75



if test -f $bv_logfile 
then
	lastlogline=$( gawk  'END { print }'  $bv_logfile )

	# waiting, backup ready, normal waiting for next hour
	vtest="$text_marker_waiting"
        if [[ $lastlogline == *"$vtest"* ]]
        then
                echo "log contains '$vtest' at end, exit 'waiting': $BK_WAITING"
                exit $BK_WAITING
	fi

	# stopped run one, backup stopped by hand './stop.sh', 
	# text_marker_stop="--- stopped ---"
	# text_do_once_count_reached="$text_marker_stop, end, bv_test_do_once_count loops reached"
        vtest="$text_do_once_count_reached"
        if [[ $lastlogline == *"$vtest"* ]]
        then
		echo "log contains '$vtest' at end, exit 'stopped do once count loops end': $BK_EXECONCESTOPPED"
                exit $BK_EXECONCESTOPPED
	fi

	# stopped run one, backup stopped by hand './stop.sh', 
	vtest="$text_marker_stop, end reached, test_execute_once"
        if [[ $lastlogline == *"$vtest"* ]]
        then
		echo "log contains '$vtest' at end, exit 'stopped run once': $BK_EXECONCESTOPPED"
                exit $BK_EXECONCESTOPPED
	fi
	# stopped, backup stopped by hand './stop.sh', 
        vtest="$text_marker_stop"
        if [[ $lastlogline == *"$vtest"* ]]
        then
		echo "log contains '$vtest' at end, exit 'stopped': $BK_STOPPED"
                exit $BK_STOPPED
	fi


	# wait interval
	# text_wait_interval_reached="wait interval reached"
        vtest="$text_wait_interval_reached"
        if [[ $lastlogline == *"$vtest"* ]]
        then
		echo "log contains '$vtest' at end, in interval waiting, exit 'waitinterval': $BK_WAITINTERVAL"
                exit $BK_WAITINTERVAL
	fi


	# waiting error
        vtest="$text_marker_error_in_waiting"
        if [[ $lastlogline  = *"$vtest"* ]]
        then
		echo "log contains '$vtest' at end, errors in projekt, exit 'rsyncfails': $BK_RSYNCFAILS"
                exit $BK_RSYNCFAILS
	fi
	# stop with error
        vtest="$text_marker_error_in_stop"
        if [[ $lastlogline  = *"$vtest"* ]]
        then
		echo "log contains '$vtest' at end, stopped, errors in projekt, exit 'rsyncfails': $BK_RSYNCFAILS"
                exit $BK_RSYNCFAILS
	fi
	
	
	echo "log shows no stop marker, backup is running, exit 'running': $BK_RUNNING"
        exit $BK_RUNNING
			


fi

exit 0

# EOF

