#!/bin/bash
#
# Copyright (C) 2006-2008 Stefanos Harhalakis
#
# This file is part of vbackup.
#
# vbackup 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.
#
# vbackup 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 vbackup; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#
# $Id: vbackup.in 2154 2009-09-24 14:32:05Z v13 $
#
# The main backup script
#

prefix="/usr"
datarootdir="${prefix}/share"
datadir="${datarootdir}"
myhelperdir="${datarootdir}/vbackup/helpers"

. $myhelperdir/common

do_version_head()
{
	cat << _END
$PACKAGE_NAME v$PACKAGE_VERSION
_END
#$PACKAGE_NAME v$PACKAGE_VERSION Copyright (c) 2006-2008 Harhalakis Stefanos
}

do_version_bugreport()
{
	cat << _END
Report bugs to $PACKAGE_BUGREPORT
_END
}

# Show version and copyright information
do_version()
{
	cat << _END
$PACKAGE_NAME v$PACKAGE_VERSION Copyright (c) 2006-2008 Harhalakis Stefanos \
$PACKAGE_BUGREPORT

    This program 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.

    This program 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.

Report bugs to $PACKAGE_BUGREPORT

_END
}

# Show generic help
do_help()
{
	do_version_head
	cat << _END
Usage:
	vbackup <backup configuration dir>
		To perform backup based on this dir

     or vbackup --list
		To list all available backup scripts

     or vbackup [-d <level>] --check <back configuration dir>
                To check configuration for all files in dir

     or vbackup --help
                To get this help

     or vbackup --help <module>
                To get module specific information

     or vbackup --version
                To get version and license information

        Directories must be given as absolute paths or they will be prepended
	with $myconfdir

	-d <level>  Set the message level to <level> (default level is \
$MESSAGE_LEVEL):
		    1: Fatal, 2: Error, 3: Warning, 4: Note, 5-7: Information
		    5: Rare messages, 6: Usefull message, 7: Not so usefull
		    10-14: Debug messages that don't flood
		    15-19: Debug messages that flood

_END
	do_version_bugreport
	exit 0
}

# Validate a configuration directory name
# $1 is the directory name
validate_dir()
{
	if [ ! -d "$1" ] ; then
		h_error "No such directory: $1"
		exit 1
	fi
}

# List available scripts
do_list()
{
	do_version_head
	$B_BINDIR/run --list
	exit 0
}

# Set the CONFDIR variable
# Also set the LEVEL variable
# $1 is the directory
set_confdir()
{
	if [ ! "$1" = "${1#.}" ] ; then
		h_error "Bad path: $1"
		h_error "Path must not begin with a dot"
		exit 1
	fi

	if [ "$1" = "${1#/}" ] ; then
		CONFDIR="${myconfdir}/backup.$1"
	else
		CONFDIR="$1"
	fi

	export LEVEL="$1"

	h_msg 6 "Using $CONFDIR"

	validate_dir "$CONFDIR"
}

# Read global configuration file if available
read_global_conf()
{
	local	G

	G="$CONFDIR/vbackup.conf"

	if ! [ -f "$G" ] ; then
		h_fatal 1 "Global backup configuration file:\n$G\ndoes not exist"
	fi

	h_msg 7 "Reading: $G"

	. $G

	# Export global variables
	h_msg 12 "Global var COMPRESS: $COMPRESS"
	export COMPRESS
}

# Common part for run/check
# $1:	run, check indicating what to do
do_run_check_common()
{
	local	F
	local	D
	local	T

	if [ "$1" != "run" ] && [ "$1" != "check" ] ; then
		h_fatal 1 "Bad argument to do_run_check_common()"
	fi

	# If this becomes one, then all scripts should exit without doing
	# aything at all, unless there is a very good reason (like umount)
	export ABORT=0

	# Transform DESTDIR0
	h_transform "$DESTDIR0"
	DESTDIR0="$R"

	export DESTDIR0

	cd "$CONFDIR"
	$GFIND . -maxdepth 1 \( -type f -or -type l \) -name '*.*' | sort |
		while read fn ; do
			F=${fn#./}
			D=`echo "$F" | awk -F . '{print $NF}'`
			T=`echo "$F" | grep '^[0-9]'`
			h_msg 11 "T - D: $T - $D"
			if [ ! -z "$T" ] && [ ! -z "$D" ] ; then
				h_msg 11 "$fn -> $D"
				if [ "$1" = "run" ] ; then
					$B_BINDIR/run "$D" "$T"
					case "$?" in
						1)
							h_error "$D exited with errors (non-fatal)"
							;;
						2)
							h_fatal -x "$D exited with errors"
							return
							;;
						3)
							h_fatal 1 "$D exited with errors"
							;;
					esac
				else
					h_msg 7 "Checking $T..."
					$B_BINDIR/run "$D" --check "$T"
				fi
			fi
		done
}

# Check configuration
# $1 is the directory
do_check()
{
	set_confdir "$1"

	read_global_conf

	do_run_check_common check
}

# Display script help
# $1 is the script name
do_script_help()
{
	do_version_head

	$B_BINDIR/run "$1" --help
}

# Run the backup configuration files
# $1 is the directory
do_run()
{
	set_confdir "$1"

	read_global_conf

	do_run_check_common run
}

if [ "x$1" = "x-d" ] ; then
	export MESSAGE_LEVEL="$2"
	shift
	shift
fi

case "$1" in
	--help)
		# Display generic help
		if [ -z "$2" ] ; then
			do_help
		else
			do_script_help "$2"
		fi
		;;
	--list)
		# List all available backup scripts
		do_list
		;;
	--check)
		# check configuration
		if [ -z "$2" ] || [ ! -z "$3" ] ; then
			do_help
		fi
		do_check "$2"
		;;
	--version)
		do_version
		;;
	-*)
		do_help
		;;
	*)
		# Assume that $1 is the configuration dir name
		if [ -z "$1" ] || [ ! -z "$2" ] ; then
			do_help
		fi

		do_run "$1"
		;;
esac


