#!/bin/bash
#**************************************************************************
#* sbackupbkpfunc - Functions for the execution of the backup.
#* Version:     0.2.0 10/08/2006
#* File:        sbackupbkpfunc
#* Description: Executes the backup.
#* 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
#**************************************************************************/

function SetUpBackUp(){
# sets up the file of backup and the file to exclude from the backup
    ListFile=${ListFile// |/|}
    ListFile=${ListFile//| /|}
    ListNoFile=${ListNoFile// |/|}
    ListNoFile=${ListNoFile//| /|}
# sets up the file with the list of file to exclude from the backup
    echo -n > $BackUpNoFile
    if [ "$ListNoFile" ]; then
	IFS=\|
	for NameFile in $ListNoFile
	  do
	  echo $NameFile >> $BackUpNoFile
	done
	IFS=$OLDIFS
	ExcFromFile="--exclude-from="$BackUpNoFile
    fi # if [ "$ListNoFile" ]; then
# sets up the compression of the backup
    if [ "$c_BackUpComp" == "gzip" ]; then
	TipoComp="z"
    elif [ "$c_BackUpComp" == "bzip" ]; then
	TipoComp="j"
    fi
} # SetUpBackUp

function backup(){
    local ListFile=$1 NameDev=$2 ListNoFile=$3 FindFile result
    local TipoComp ExcFromFile

# exclude the backup device from the backup
    if [ "$c_BackUpNoDev" == "yes" ]; then
	if [ "$ListNoFile" ]; then
	    ListNoFile=$ListNoFile" | "
	fi
	ListNoFile=$ListNoFile$NameDev
    fi
# sets up the file and the date used for the backup
    ExcFromFile=""
    TipoComp=""
    SetUpBackUp
# executes the backup
    IFS=\|
    tar "-"$TipoComp"cvf" $NameDev $ExcFromFile $ListFile $EndFile > $BackUpLogFile 2>&1; result=$?
    IFS=$OLDIFS
#
    echo `eval_gettext "Outcome tar: \\\$result"`
    cat $BackUpLogFile | grep '^tar'
# 141= destination not existing, 2= device not ready or file to copy not present 
    if [ $result == "0" ]; then
	s_BackUpStatus="ok"
    elif [ $result == "2" ]; then
# device of destination not ready, file for the backup not existing.
# if does not come found the signalling file will come set up to ' ko-stop'
	s_BackUpStatus="ko-end"
    else
	s_BackUpStatus="ko-stop"
    fi
# searches the file that signals that all file of the backup have been elaborated
    FindFile=""
    if [ -f $BackUpLogFile ]; then
	FindFile=`tail -2 $BackUpLogFile | grep $EndFile`
    fi
    if [ "$FindFile" ]; then
	echo `eval_gettext "Found file \\\$FindFile as last file of the backup"`
    else
	s_BackUpStatus="ko-stop"
    fi

    return;
} # backup

