#!/bin/bash
#**************************************************************************
#* ScriptBackUpFunc - Functions for the execution of the backup and the shutdown.
#* Version:     0.1.9 19/02/2006
#* File:        sbackupfunc
#* Description: Functions for the reading of the configurations and for 
#*              the execution of the backup and 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
#**************************************************************************/

function IfMakeFile(){
    FileName=$1 FileType=$2

    if [ "$FileType" == "DIR" ]; then
	if ! [ -d $FileName ]; then
	    mkdir $FileName
	    chmod 750 $FileName
	fi
    else
	if ! [ -f $FileName ]; then
	    echo -n > $FileName
	    chmod 660 $FileName
	fi
    fi
} # IfMakeFile()

function CheckFile(){
    local FileCheck=$1 FindFile

    s_CheckFileStato="ko"
# verification if are modified file during the backup
    FileCheck=${FileCheck// |/|}
    FileCheck=${FileCheck//| /|}
    IFS=\|
    find $FileCheck -newer $PidFile > $ChkFileLogFile
    IFS=$OLDIFS
# if there are file that have been modified during the backup
    FindFile=""
    if [ -f $ChkFileLogFile ]; then
	FindFile=`tail -1 $ChkFileLogFile`
    fi
    if [ "$FindFile" ]; then
	echo `eval_gettext "Found modified file during the backup, to control \\\$ChkFileLogFile"`
	echo `eval_gettext "Last 10 file:"`
	tail -10 $ChkFileLogFile
    else
	s_CheckFileStato="ok"
    fi
} # CheckFile()

function CheckGoShut(){
    local esito
# 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 $StatFile $oggi; esito=$?
	if [ $esito != "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()

function CheckGoBackUp(){
# 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 $StatFile $oggi; esito=$?
	if [ $esito == "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 IsBackUpOk(){
    local StatFile=$1 oggi=$2 esito=0
# verification if the backup of the period has been correctly executed
    if [ $s_BackUpData != $oggi ] || [ $s_BackUpStato != "ok" ]; then
	esito=1
    fi
    if [ $s_CheckFileStato == "ko" ]; then
	esito=1
    fi

    return $esito
} # IsBackUpOk()

function GetConf(){
    local FileName=$1 group=$2 pref=$3 VarName
    IFS=$'\015'
    while read stat key val; do
	VarName=$pref$key
	eval "export '$VarName'='$val'"
    done < <($GesConfProg -v 2 -ps "$IFS" -vs " | " -fm $FileName -l $group "" K)
    IFS=$OLDIFS
} # GetConf()

function SetConf(){
    local FileName=$1 group=$2 pref=$3

    if [ "$pref" == "s_" ]; then
	echo -e\
        $group "BackUpStato" \"$s_BackUpStato\""\n"\
	$group "BackUpData" \"$s_BackUpData\""\n"\
        $group "Ok_BackUpStato" \"$s_Ok_BackUpStato\""\n"\
	$group "Ok_BackUpData" \"$s_Ok_BackUpData\""\n"\
	$group "CheckFileStato" \"$s_CheckFileStato\""\n"\
	$group "CheckFileData" \"$s_CheckFileData\""\n"\
        $group "ShutStato" \"$s_ShutStato\""\n"\
	$group "ShutData" \"$s_ShutData\" |\
	$GesConfProg - -fm $FileName -s
    fi
} # SetConf()

function SetBackUpName(){
    local BackUpName=$1 OptName=$2
# sets up the name of the backup and of the file that they it depends
    PidFile=$RunPath"/"$BackUpName".pid"
    BackUpNoFile=$LogPath"/"$BackUpName".nofile"
    StatFile=$VarPath"/"$ProgName".status"
    BackUpLogFile=$LogPath"/"$BackUpName".log"
    ChkFileLogFile=$LogPath"/"$BackUpName".check.log"

    IfMakeFile $StatFile "FILE"
# read and sets up the configuration of the backup

    GetConf $ConfFile $BackUpName "c_"
    if [ "$OptName" ]; then
	GetConf $ConfFile $OptName "c_"
    fi
# read and sets up the status of the backup
    GetConf $StatFile $BackUpName "s_"

    if ! [ $c_BackUpMode ]; then
	c_BackUpMode="null"
    fi
    if ! [ $c_ShutMode ]; then
	c_ShutMode="null"
    fi
#
    if ! [ $s_BackUpData ]; then
	s_BackUpData="null"
    fi
    if ! [ $s_BackUpStato ]; then
	s_BackUpStato="null"
    fi
    if ! [ $s_CheckFileData ]; then
	s_CheckFileData="null"
    fi
    if ! [ $s_CheckFileStato ]; then
	s_CheckFileStato="null"
    fi
#
    if [ $c_BackUpMode == "null" ] && [ $c_ShutMode == "null" ]; then
	echo `eval_gettext "Not existent configuration backup/shutdown"`
    else
	if [ $c_BackUpMode == "null" ]; then
	    echo `eval_gettext "Not existent configuration backup"`
	elif [ $c_ShutMode == "null" ]; then
	    echo `eval_gettext "Not existent configuration shutdown"`
	fi
	IfMakeFile $BackUpNoFile "FILE"
	IfMakeFile $BackUpLogFile "FILE"
	IfMakeFile $ChkFileLogFile "FILE"
    fi
} # SetBackUpName()

function SetProg(){
    local ProgFile=$1 PrecDir AttFile FindDir FindDir2 FindFile
# sets up the parameters for the operation of the program
    RunPath="/var/run/"$ProgName
    VarPath="/var/lib"
    LogPath="/var/log/"$ProgName

    IfMakeFile $RunPath "DIR"
    IfMakeFile $LogPath "DIR"
# searches the configuration file
    PrecDir=`dirname $ProgPath`
    FindDir=$ProgPath
    FindDir2=$PrecDir"/etc"
    if [ ${ProgPath: -5} == "/sbin" ] || [ ${ProgPath: -4} == "/bin" ]; then
	FindDir=$PrecDir"/etc"
	FindDir2=$ProgPath
    fi
#
    FindFile="sbackup.conf"
    AttFile=$FindDir"/"$FindFile
    if ! [ -f $AttFile ]; then
	AttFile=""
    fi
    if ! [ $AttFile ]; then
	AttFile=$FindDir2"/"$FindFile
	if ! [ -f $AttFile ]; then
	    AttFile=""
	fi
    fi
    if ! [ $AttFile ]; then
	echo `eval_gettext "File \\\$FindFile not accessible"`
    fi
    ConfFile=$AttFile
#
    FindFile="gesconf"
    AttFile=$FindFile
    if ! [ `which $FindFile 2>/dev/null` ]; then
        AttFile=$ProgPath"/"$FindFile
	if ! [ -f $AttFile ]; then
	    AttFile=""
	fi
    fi
    if ! [ $AttFile ]; then
	echo `eval_gettext "File \\\$FindFile not accessible"`
    fi
    GesConfProg=$AttFile
#
# date in aa-m-g numerical format
    oggi=`date +"%Y-%m-%d"`
    c_BackUpMode="null"
    c_ShutMode="null"
    s_BackUpData="null"
    s_BackUpStato="null"
    s_CheckFileData="null"
    s_CheckFileStato="null"
    GoBackUp="true"
    GoShut="true"
} # SetProg()

# return the pid of the process of backup with BackUpName name
function GetMeRun(){
    local ProgName=$1 PidFile=$2 PidProc 

    pid=""
    if [ -f $PidFile ]; then
	PidProc=`head -1 $PidFile`
	if [ $PidProc ]; then
	    FindProc $ProgName "false" $PidProc
	fi
    else
	pid=""
    fi
} # GetMeRun()

function FindProc(){
    local ProgName=$1 FullMatch=$2 PidProc=$3 ListaName StrName MatchOk
# return the pid (the first pid) of the ProgName process that corresponds to 
# string MatchName or to the pid PidProc 
    pid=""
    MatchOk="false"
    if [ "$ProgName" ]; then
# removes the eventual space before and after the separator
	ListaName=${ProgName// |/|}
	ListaName=${ListaName//| /|}
	while read AttPid var2 var3 var4 prog; do
	    if [ $FullMatch == "false" ]; then
		MatchOk="true"
	    fi
# verification if the name of the program corresponds to the single name
	    if [ $MatchOk != "true" ] && [ "$prog" == "$ProgName" ]; then
		MatchOk="true"
	    fi
# verification if the name of the program corresponds to the name with the separator
	    if [ $MatchOk != "true" ]; then
		StrName=${ListaName//$prog|/|}
		StrName=${StrName//|$prog/|}
		if [ "$StrName" != "$ListaName" ]; then
		    MatchOk="true"
		fi
	    fi
# verification if the pid of the program is that demanded
	    if [ $PidProc ]; then
		MatchOk="false"
		if [ $AttPid == $PidProc ]; then
		    MatchOk="true"
		fi
	    fi
	    if [ $MatchOk == "true" ]; then
		pid=$AttPid
#echo "$pid $prog"
		break;
	    fi
	done < <(ps ax | grep -E "$ListaName" | grep -v grep)
    fi # if [ "$ProgName" ]; then
} # FindProc()
