#!/bin/bash
#**************************************************************************
#* sbackuprunfunc - Functions for the execution of ScriptBackUp.
#* Version:     0.2.0 11/08/2006
#* File:        sbackuprunfunc
#* Description: Executes the  ScriptBackUp.
#* Platform(s):	GNU/Linux, All
#* Author(s):	Della Bianca Giuseppe <bepi@adria.it><bepii@libero.it>
#*
#* Copyright (C) 2005-6 Della Bianca Giuseppe <bepi@adria.it><bepii@libero.it>
#* This file is part of ScriptBackUp.
#*
#* ScriptBackUp 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.
#*
#* ScriptBackUp 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 ScriptBackUp; if not, write to the Free Software
#* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#**************************************************************************/

# check and initialize the set of backup/shutdown 
function InitBackUp(){
# initialize the backup/shutdown variables
InitBackUpVar

if [ $GoBackUp = "true" ] || [ $GoShut == "true" ]; then
# read and sets up the configuration of the backup/shutdown
    SetBackUpName
fi
CheckSetBackUp
# verification if a process of backup is already active with BackUpName name
if [ $GoBackUp == "true" ] || [ $GoShut == "true" ]; then
    GetMeRun $ProgName $PidFile
    if [ $pid ]; then
	GoBackUp="false"
	GoShut="false"
	echo `eval_gettext "Previous process of backup/shutdown is running, operation cancelled"`
	ps $pid
    else
# save the pid of the process of backup with BackUpName name
	MakeFile $PidFile "FILE" "640"
	echo $$ > $PidFile

	TxtIni=""
	if [ $GoBackUp == "true" ] && [ $GoShut == "true" ]; then
	    TxtIni=`eval_gettext "Begin backup/shutdown"`
	elif [ $GoBackUp == "true" ]; then
	    TxtIni=`eval_gettext "Begin backup"`
	elif [ $GoShut == "true" ]; then
	    TxtIni=`eval_gettext "Begin shutdown"`
	fi
	echo $TxtIni `eval_gettext "(Last backup ended: Date \\\$s_Ok_BackUpDate - status \\\$s_Ok_BackUpStatus)"`
    fi
fi
} # InitBackUp

# end the set of backup/shutdown 
function EndBackUp(){
    echo `eval_gettext "End of \\\$ProgName ("``date`")"
# visualizes the path of the files with the several information
    if [ "$c_InfoView" != "no" ]; then
	echo -e "\n----> info backup <----"
	echo -e "c_BackUpFile $c_BackUpFile\nc_BackUpDev $c_BackUpDev"
	echo -e "c_BackUpNoFile $c_BackUpNoFile"
	if [ "$c_InfoView" == "full" ]; then
	    echo -e "\n----> info scriptbackup <----"
	    echo -e "ConfFile $ConfFile\nGesConfProg $GesConfProg"
	    echo -e "BackUpNoFile $BackUpNoFile\nStatFile $StatFile"
	    echo -e "BackUpLogFile $BackUpLogFile\nChkFileLogFile $ChkFileLogFile"
	    echo -e "PidFile $PidFile"
	fi
	echo
    fi

    if [ $GoBackUp == "ko" ] || [ $GoShut == "ko" ]; then
	ProgResult=1
    fi
} # EndBackUp

# run the backup
function RunBackUp(){
    if [ $GoBackUp == "true" ]; then
# sets up if is necessary to execute the backup
	CheckGoBackUp
	if [ $GoBackUp == "true" ]; then
# annuls the log file
	    echo -n > $BackUpLogFile
	    echo -n > $ChkFileLogFile
	    s_BackUpStatus="ko"
	    s_BackUpDate=$oggi
	    s_CheckFileStatus="null"
	    s_CheckFileDate=$oggi
# executes the backup
	    backup "$c_BackUpFile" "$c_BackUpDev" "$c_BackUpNoFile"
	    echo `eval_gettext "End backup"`
# verification if are modified file during the backup
	    if [ "$c_FileCheck" ]; then
		echo `eval_gettext "Begin verification of the modified file during the backup"`
		if [ -f $PidFile ]; then
		    CheckFile "$c_FileCheck"
		    echo `eval_gettext "End verification file"`
		else
		    echo `eval_gettext "Verification file not possible, file \\\$PidFile not existent"`
		fi
	    fi # if [ "$c_FileCheck" ]; then
	    if [ $s_BackUpStatus == "ok" ] || [ $s_BackUpStatus == "ko-end" ]; then
		s_Ok_BackUpStatus=$s_BackUpStatus
		s_Ok_BackUpDate=$oggi
	    fi
# save the outcome of the backup
	    SetConf $StatFile $BackUpName "s_"
	fi
# removes the file that contains the process pid of backup with BackUpName name 
	if [ -f $PidFile ]; then
	    rm -f $PidFile
	fi
    fi # [ $GoBackUp == "true" ]
# visualizes the outcome of the backup
    if [ $c_BackUpMode != "no" ]; then
	if [ $GoBackUp == "true" ]; then
	    if [ $s_BackUpStatus == "ok" ]; then
		echo `eval_gettext "BACKUP FINISHED CORRECTLY"`
	    elif [ $s_BackUpStatus == "ko-stop" ]; then
		echo `eval_gettext "BACKUP INTERRUPTED"`
	    elif [ $s_BackUpStatus == "ko-end" ]; then
		echo `eval_gettext "BACKUP FINISHED WITH ERROR"`
	    else
		echo `eval_gettext "THEY HAVE BEEN OF THE ERRORS DURING THE BACKUP"`
	    fi
# visualizes the outcome of the verification of the modified file during the backup 
	    if [ "$c_FileCheck" ]; then
		if [ $s_CheckFileStatus == "null" ]; then
		    echo `eval_gettext "START VERIFICATION MODIFIED FILE DURING THE BACKUP FAILED"`
		elif [ $s_CheckFileStatus == "ko" ]; then
		    echo `eval_gettext "SOME FILE HAVE BEEN MODIFIED DURING THE BACKUP"`
		fi
	    fi
	elif [ $GoBackUp == "false" ]; then
	    echo `eval_gettext "BACKUP NOT EXECUTED"`
	else
	    echo `eval_gettext "START PROCEDURE OF BACKUP FAILED"`
	fi
    fi # if [ $c_BackUpMode != "no" ]; then
} # RunBackUp

# run the shutdown
function RunShutDown(){
    if [ $GoShut == "true" ]; then
#  sets up if is necessary to execute the shutdown
	CheckGoShut
	if [ $GoShut == "true" ]; then
	    s_ShutStatus="ok"
	    s_ShutDate=$oggi
# save the outcome of the shutdown
	    SetConf $StatFile $BackUpName "s_"
# executes the shutdown
	    if ! [ $c_ShutDelay ]; then
		c_ShutDelay="1"
	    fi
	    shutdown -h $c_ShutDelay &
	fi
    fi
# visualizes the outcome of the shutdown
    if [ $c_ShutMode != "no" ]; then
	if [ $GoShut == "true" ]; then
	    if [ $s_ShutStatus == "ok" ]; then
		echo `eval_gettext "SHUTDOWN EXECUTED"`
	    else
		echo `eval_gettext "THEY HAVE BEEN OF THE ERRORS DURING THE SHUTDOWN"`
	    fi
	elif [ $GoShut == "false" ]; then
	    echo `eval_gettext "SHUTDOWN NOT EXECUTED"`
	else
	    echo `eval_gettext "START PROCEDURE OF SHUTDOWN FAILED"`
	fi
    fi # if [ $c_ShutMode != "no" ]; then
} # RunShutDown

# initialize the backup/shutdown variables
function InitBackUpVar(){
    c_BackUpMode=""
    c_BackUpFile=""
    c_BackUpNoFile=""
    c_BackUpNoDev=""
    c_BackUpComp=""
    c_BackUpDev=""
    c_BackUpCheckDev=""
    c_BackUpNoProc=""
    c_FileCheck=""
    c_ShutMode=""
    c_ShutDelay=""
    c_ShutNoProc=""
    c_InfoView=""
    s_BackUpStatus=""
    s_BackUpDate=""
    s_Ok_BackUpStatus=""
    s_Ok_BackUpDate=""
    s_CheckFileStatus=""
    s_CheckFileDate=""
    s_ShutStatus=""
    s_ShutDate=""
} # InitBackUpVar

function SetBackUpName(){
    local result FileUID
# set the name of the backup and of the work file
    PidFile=$RunPath"/"$BackUpName".pid"
    BackUpNoFile=$LogPath"/"$BackUpName".nofile"
    BackUpLogFile=$LogPath"/"$BackUpName".log"
    ChkFileLogFile=$LogPath"/"$BackUpName".check.log"
    StatFile=$VarPath"/"$BackUpName".status"

    result="true"
# check the status file of the backup
    if [ -f $StatFile ]; then
	FileUID=`stat -c %u $StatFile`
	if [ "$FileUID" != $UID ]; then
	    result="ko"
	    echo `eval_gettext "This set of backup/shutdown is used from other user"`
	    echo
	fi
    fi
# read and set the configuration of the backup
    if [ $result == "true" ]; then
	GetConf $ConfFile $BackUpName "c_"
	if [ $? != "0" ]; then
	    result="ko"
	fi
    fi
# check the work file of the backup
    if [ $result == "true" ]; then
	if ! [ -f $StatFile ]; then
	    echo `eval_gettext "The first use of this set of backup/shutdown set the actual user as owner of this work file:"`
	    echo -e $BackUpNoFile $BackUpLogFile $ChkFileLogFile $StatFile"\n"
# creation of the work file
	    MakeFile $BackUpNoFile "FILE" "640"
	    MakeFile $BackUpLogFile "FILE" "640"
	    MakeFile $ChkFileLogFile "FILE" "640"
	    MakeFile $StatFile "FILE" "640"
	fi
# read and set the status of the backup
	GetConf $StatFile $BackUpName "s_"
	if [ $? != "0" ]; then
	    result="ko"
	fi
    fi

    if [ $GoBackUp == "true" ]; then
	GoBackUp=$result
    fi
    if [ $GoShut == "true" ]; then
	GoShut=$result
    fi
} # SetBackUpName

# check the set of backup/shutdown
function CheckSetBackUp(){
# check backup/shutdown configuration
    if [ $GoBackUp == "true" ]; then
	if ! [ $c_BackUpMode ]; then
	    GoBackUp="ko"
	    echo `eval_gettext "Not existent backup configuration"`
	elif [ $c_BackUpMode == "no" ]; then
	    GoBackUp="false"
	fi
    fi
    if [ $GoShut == "true" ]; then
	if ! [ $c_ShutMode ]; then
	    GoShut="ko"
	    echo `eval_gettext "Not existent shutdown configuration"`
	elif [ $c_ShutMode == "no" ]; then
	    GoShut="false"
	fi
    fi
#
    if ! [ $c_BackUpMode ]; then
	c_BackUpMode="null"
    fi
    if ! [ $c_ShutMode ]; then
	c_ShutMode="null"
    fi
    if ! [ $c_InfoView ]; then
	c_InfoView="no"
    fi
    if ! [ $s_BackUpDate ]; then
	s_BackUpDate="null"
    fi
    if ! [ $s_BackUpStatus ]; then
	s_BackUpStatus="null"
    fi
    if ! [ $s_CheckFileDate ]; then
	s_CheckFileDate="null"
    fi
    if ! [ $s_CheckFileStatus ]; then
	s_CheckFileStatus="null"
    fi
} # CheckBackUp

function IsBackUpOk(){
    local oggi=$1 result=0
# verification if the backup of the period has been correctly executed
    if [ $s_BackUpDate != $oggi ] || [ $s_BackUpStatus != "ok" ]; then
	result=1
    fi
    if [ $s_CheckFileStatus == "ko" ]; then
	result=1
    fi

    return $result
} # IsBackUpOk

function CheckGoBackUp(){
    local result
# sets up if is necessary to execute the backup
    if [ $c_BackUpMode == "no" ]; then 
	GoBackUp="false"
    fi
# if the backup does not executed in any case
    if [ $GoBackUp == "true" ] && [ $c_BackUpMode != "force-yes" ]; then
# verification if the backup of the period has been correctly executed
	IsBackUpOk $oggi; result=$?
	if [ $result == "0" ]; then
	    echo `eval_gettext "Backup of the period already correctly executed, backup cancelled"`
	    GoBackUp="false"
	fi
    fi
# if the backup goes executed only if not there are active processes
    if [ $GoBackUp ] && [ $c_BackUpMode == "yes" ]; then
# verification if there are active processes
	FindProc "$c_BackUpNoProc" "false"
        if [ $pid ]; then
	    GoBackUp="false"
	    echo `eval_gettext "Found process, backup cancelled"`
	    ps -f $pid
	fi
    fi # [ $GoBackUp ] && [ $c_BackUpMode == "yes" ]; then
# verification the data for the backup
    if [ $GoBackUp == "true" ] && ! [ "$c_BackUpFile" ]; then
	GoBackUp="false"
	echo `eval_gettext "No file for the backup have been specified"`
    fi
    if [ $GoBackUp == "true" ] && ! [ "$c_BackUpDev" ]; then
	GoBackUp="false"
	echo `eval_gettext "No destination of the backup has been specified"`
    fi
    if [ $GoBackUp == "true" ] && [ "$c_BackUpCheckDev" == "yes" ]; then
	if ! [ -e "$c_BackUpDev" ]; then
	    GoBackUp="false"
	    echo `eval_gettext "The destination of the backup \\\$c_BackUpDev not exist"`
	fi
    fi
} # CheckGoBackUp

function CheckGoShut(){
    local result
# sets up if is necessary to execute the shutdown
    if [ $c_ShutMode == "no" ]; then 
	GoShut="false"
    fi
# if the shutdown does not executed in any case
    if [ $GoShut == "true" ] && [ $c_ShutMode != "force-yes" ]; then
# verification if the backup of the period it has been correctly executed
	IsBackUpOk $oggi; result=$?
	if [ $result != "0" ]; then
	    GoShut="false"
	    echo `eval_gettext "Backup of the period not still correctly executed, shutdown cancelled"`
	fi
    fi
# if the shutdown goes executed only if not there are active processes
    if [ $GoShut ] && [ $c_ShutMode == "yes" ]; then
# verification if there are active processes
	FindProc "$c_ShutNoProc" "false"
        if [ $pid ]; then
	    GoShut="false"
	    echo `eval_gettext "Found process, shutdown cancelled"`
	    ps -f $pid
	fi
    fi # [ $GoShut ] && [ $c_ShutMode == "yes" ]; then
} # CheckGoShut
