#!/bin/bash
#**************************************************************************
#* ScriptBackUp - Executes the backup and if demanded executes the shutdown.
#* Version:     0.1.9 19/02/2006
#* File:        sbackup
#* Description: Read the configuration and executes backup and/or the shutdown.
#* 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
#**************************************************************************/

#export LC_ALL="C"
ProgFile=$0
BackUpName=$1
OptName=$2
ProgPath=`dirname $ProgFile`
ProgName=`basename $ProgFile`
if [ $ProgPath == "." ]; then
    ProgPath=`pwd`;
fi
OLDIFS=$IFS
#
# load the data and the functions necessary to ScriptBackUp and for 
# execute the backup
. $ProgPath"/"$ProgName"conf"
. $ProgPath"/"$ProgName"func"
. $ProgPath"/"$ProgName"exefunc"
#
. gettext.sh
export TEXTDOMAIN
export TEXTDOMAINDIR
#
echo `eval_gettext "Start of \\\$ProgName \\\$ProgVers"`
# read and sets up the parameters for the operation of the program
SetProg $ProgFile
#
TxtSet=""
if [ $ConfFile ] && [ $GesConfProg ]; then
    if ! [ $BackUpName ]; then
	BackUpName="DEFAULT"
    fi
    TxtSet="Set: "$BackUpName
    if [ $OptName ]; then
	TxtSet=$TxtSet" - Set opt.: "$OptName
    fi
else
    GoBackUp="ko"
    GoShut="ko"
    TxtSet=`eval_gettext "Configuration not existent"`
fi
echo $TxtSet
#
# read and sets up the configuration of the backup
if [ $GoBackUp == "true" ] || [ $GoShut == "true" ]; then
    SetBackUpName $BackUpName $OptName
    if [ $c_BackUpMode == "null" ]; then
	GoBackUp="ko"
    elif [ $c_BackUpMode == "no" ]; then
	GoBackUp="false"
    fi
    if [ $c_ShutMode == "null" ]; then
	GoShut="ko"
    elif [ $c_ShutMode == "no" ]; then
	GoShut="false"
    fi
fi
# verification if a process of backup is already active with BackUpName name
if [ $GoBackUp == "true" ] || [ $GoShut == "true" ]; then
    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_BackUpData - status \\\$s_Ok_BackUpStato)"`
    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 
	IfMakeFile $PidFile "FILE"
	echo $$ > $PidFile
    fi
fi
#
# begin of the execution of the backup
#
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_BackUpStato="ko"
	s_BackUpData=$oggi
	s_CheckFileStato="null"
	s_CheckFileData=$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_BackUpStato == "ok" ] || [ $s_BackUpStato == "ko-end" ]; then
	    s_Ok_BackUpStato=$s_BackUpStato
	    s_Ok_BackUpData=$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_BackUpStato == "ok" ]; then
	    echo `eval_gettext "BACKUP FINISHED CORRECTLY"`
	elif [ $s_BackUpStato == "ko-stop" ]; then
	    echo `eval_gettext "BACKUP INTERRUPTED"`
	elif [ $s_BackUpStato == "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_CheckFileStato == "null" ]; then
		echo `eval_gettext "START VERIFICATION MODIFIED FILE DURING THE BACKUP FAILED"`
	    elif [ $s_CheckFileStato == "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
#
#  begin of the execution of the shutdown
#
if [ $GoShut == "true" ]; then
#  sets up if is necessary to execute the shutdown
    CheckGoShut
    if [ $GoShut == "true" ]; then
	s_ShutStato="ok"
	s_ShutData=$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_ShutStato == "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
#
# end of the execution of the backup and of the shutdown
#
echo `eval_gettext "End of \\\$ProgName ("``date`")"
# visualizes the path of the files with the several information
if [ "$c_InfoView" == "yes" ]; then
    echo
    echo "----> info backup <----"
    echo -e "c_BackUpFile ""$c_BackUpFile""\nc_BackUpDev "$c_BackUpDev
    echo -e "c_BackUpNoFile ""$c_BackUpNoFile"
    echo
    echo "----> info scriptbackup <----"
    echo -e "PidFile "$PidFile"\nConfFile "$ConfFile
    echo -e "GesConfProg "$GesConfProg
    echo -e "BackUpNoFile "$BackUpNoFile"\nStatFile "$StatFile
    echo -e "BackUpLogFile "$BackUpLogFile"\nChkFileLogFile "$ChkFileLogFile
fi

esito=1
if [ $GoBackUp != "ko" ] && [ $GoShut != "ko" ]; then
    esito=0
fi
exit $esito

