#!/bin/bash
#**************************************************************************
#* scriptbkpfunc - Functions for the execution of the backup and the shutdown.
#* Version:     0.3.1 26/08/2008
#* File:        scriptbkpfunc
#* 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-8 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 MakeFile(){
    local FileName=$1 FileType=$2 AttrType=$3 FileStat

    if [ "$FileType" == "DIR" ]; then
	if ! [ -d $FileName ]; then
	    mkdir $FileName
	fi
    else
	if ! [ -f $FileName ]; then
	    echo -n > $FileName
	fi
    fi
    FileStat=`stat -c %a $FileName`
    if [ $AttrType != $FileStat ]; then
	chmod $AttrType $FileName
    fi
} # MakeFile

# check the attributes of file
function CheckAttr(){
    local FileName=$1 TypeCheck=$2 result FileStat OwnerAttr GroupAttr OtherAttr

    result=0

    FileStat=`stat -c %a "$FileName"`
    OwnerAttr=${FileStat: -3 :1}
    GroupAttr=${FileStat: -2 :1}
    OtherAttr=${FileStat: -1}

    if [ "$TypeCheck" == "CONF" ]; then
	if ! [ -f "$FileName" ]; then
	    result=1
	    echo `eval_gettext "File \\\$FileName is not accessible"`
	fi
	if [ $result == "0" ]; then
	    if [ $(($OwnerAttr & 1)) != "0" ] || [ $(($GroupAttr & 3)) != "0" ] || [ $(($OtherAttr & 3)) != "0" ]; then
		result=1
		echo `eval_gettext "File \\\$FileName is executable or, from group or other, writable"`
	    fi
	fi
    else
	if [ ${FileName: 0 :1} != "/" ] && [ ${FileName: 0 :2} != "./" ]; then
	    result=1
	    echo `eval_gettext "File \\\$FileName is without path"`

	fi
	if [ $result == "0" ]; then
	    if ! [ -x $FileName ]; then
		result=1
		echo `eval_gettext "File \\\$FileName is not accessible or not executable"`
	    fi
	fi
	if [ $result == "0" ]; then
	    if [ $(($GroupAttr & 3)) != "0" ] || [ $(($OtherAttr & 3)) != "0" ]; then
		result=1
		echo `eval_gettext "File \\\$FileName is executable or writable from group or other"`
	    fi
	fi
    fi

    return $result
} # CheckAttr

function CheckFile(){
    local FileCheck=$1 FindFile

    s_CheckFileStatus="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_CheckFileStatus="ok"
    fi
} # CheckFile

function GetConf(){
    local FileName=$1 group=$2 pref=$3 result FindVar VarName VarVal FileName

    result=0
    FindVar="false"
    IFS=$'\015'
    while read stat key val; do
	VarName=$pref$key
	VarVal=$val
#	if [ "${val: 0: 1}" == "\`" ]; then
#	    FileName=${val//\`/}
#	    CheckAttr $FileName "SCRIPT"
#	    if [ $? == "0" ]; then
# warning, this is one possible security hole
#		VarVal=`$FileName`
#	    else
#		result=1
#	    fi
#	fi
	eval "export '$VarName'='$VarVal'"
	if [ $FindVar == "false" ]; then
	    FindVar="true"
	fi
    done < <($GesConfProg -v 2 -ps "$IFS" -vs " | " -fm $FileName -l $group "" K)
    IFS=$OLDIFS

# check if set name is existent in configuration file
    if [ $FindVar == "false" ]; then
#    if [ $pref == "c_" ] && [ $FindVar == "false" ]; then
	result=1
    fi

    return $result
} # GetConf

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

    if [ "$pref" == "s_" ]; then
	echo -e\
        $group "BackUpStatus" \"$s_BackUpStatus\""\n"\
	$group "BackUpDate" \"$s_BackUpDate\""\n"\
        $group "Ok_BackUpStatus" \"$s_Ok_BackUpStatus\""\n"\
	$group "Ok_BackUpDate" \"$s_Ok_BackUpDate\""\n"\
	$group "CheckFileStatus" \"$s_CheckFileStatus\""\n"\
	$group "CheckFileDate" \"$s_CheckFileDate\""\n"\
        $group "ShutStatus" \"$s_ShutStatus\""\n"\
	$group "ShutDate" \"$s_ShutDate\" |\
	$GesConfProg - -fm $FileName -s
    fi
} # SetConf

# read and set up the parameters for the operation of the program
function InitProg(){
    local PrecDir AttFile FindDir FindDir1 FindDir2 FindFile DirAttr FileStat
# set up the parameters for the operation of the program
    RunPath="/var/run/"$ProgName
    VarPath="/var/lib/"$ProgName
    LogPath="/var/log/"$ProgName
# sets up the file that signals that all file of the backup have been elaborated, 
# the path is anomalous for distinguishing the file from similar files. 
    EndFile=$LogPath"/./ENDED"

    FindDir="false"
    DirAttr="1777"
    if [ -d $RunPath ]; then
	FileStat=`stat -c %a $RunPath`
	if [ $DirAttr == $FileStat ]; then
	    FindDir="true"
	fi
    fi
# creation of the work directory
    if [ $FindDir != "true" ]; then
	if [ $UID == "0" ]; then
	    ProgResult=-1
	    MakeFile $RunPath "DIR" $DirAttr
	    MakeFile $VarPath "DIR" $DirAttr
	    MakeFile $LogPath "DIR" $DirAttr
	    MakeFile $EndFile "FILE" "644"
	    echo `eval_gettext "Completed installation"`
	    echo
	else
	    ProgResult=1
	    echo `eval_gettext "Incomplete installation, is required execute scriptbkp from root user"`
	    echo
       fi
    fi
    if [ $ProgResult == "0" ]; then
# search the configuration file
	FindFile=$ProgName".conf"
	PrecDir=`dirname $ProgPath`
	FindDir=$HOME"/."$ProgName
	FindDir1=$ProgPath
	FindDir2=$PrecDir"/etc"$ProgName
	if [ ${ProgPath: -5} == "/sbin" ] || [ ${ProgPath: -4} == "/bin" ]; then
	    FindDir1=$PrecDir"/etc/"$ProgName
	    FindDir2=$ProgPath
	fi
# check the configuration file
	AttFile=$FindDir"/"$FindFile
	if ! [ -f $AttFile ]; then
	    AttFile=""
	fi
	if ! [ $AttFile ]; then
	    AttFile=$FindDir1"/"$FindFile
	    ConfFile=$AttFile
	    if ! [ -f $AttFile ]; then
		AttFile=""
	    fi
	fi
	if ! [ $AttFile ]; then
	    AttFile=$FindDir2"/"$FindFile
	    if ! [ -f $AttFile ]; then
		AttFile=""
	    fi
	fi
	if [ $AttFile ]; then
	    ConfFile=$AttFile
	fi
	CheckAttr "$ConfFile" "CONF"
	if [ $? != "0" ]; then
	    ProgResult=1
	fi
    fi
#
    FindFile="gesconf"
    AttFile=$FindFile
    if ! [ `which $FindFile 2>/dev/null` ]; then
	AttFile=$ProgPath"/"$FindFile
	if ! [ -f $AttFile ]; then
	    AttFile=""
	fi
    fi
    if ! [ $AttFile ]; then
	ProgResult=1
	echo `eval_gettext "File \\\$FindFile not accessible"`
    fi
    GesConfProg=$AttFile
#
# date in aa-m-g numerical format
    oggi=`date +"%Y-%m-%d"`
} # InitProg

# 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
