#!/bin/bash
#
# Copyright (C) 2006-2012 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 3 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, see <http://www.gnu.org/licenses/>.
#
# $Id$
#
# A helper wizard for creating initial configuration
#

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

. $myhelperdir/common

do_version_head()
{
	cat << _END
$PACKAGE_NAME v$PACKAGE_VERSION
_END
}

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-2012 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 3 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-wizard
		Perform basic vbackup(8) configuration.

     or vbackup --help
                To get this help
_END
	do_version_bugreport
	exit 0
}

get_vbackup_conf()
{
	[ -z "$1" ] && h_fatal 1 "WTF? BUG!"
	(

	RET=""
	while [ -z "$RET" ] ; do
		h_ask_str \
"vbackup stores all backups under a common directory prefix. Please enter a
prefix where all backups will be placed under. Be carefull not to use a path
that is been backed up or you will create a backup paradox.

The best place to store backups is an external disk or a remote directory that
is localy mounted" \
			"Where to place backups?"
	done
	DESTDIR0="$RET/%D1%-%L%"
	
	cat << _KOKO > "$1"
# Per backup global configuration file
#
# This file holds per-backup configuration options

# A directory where backups will be stored
# All other DESTDIR directives are relative to this one
DESTDIR0=$DESTDIR0

# Compress backups? (yes/no)
COMPRESS="yes"

_KOKO
	)
}

do_run()
{
	h_ask_txt \
"Welcome to vbackup initial configuration wizard.
Your configuration files will be stored under:
$myconfdir

You'll now be asked a series of questions.
Default answers will be shown in brackets. You can accept them by pressing
enter."

	read -rp "Press enter to continue..." a

	cd "$wizdir"

	# We will need stdin
	exec 9<&0

	# Create a temporary directory
	T=`mktemp -d`
	if [ -z "$T" ] ; then
		h_fatal 1 "Failed to create temporary directory!"
	fi

	mkdir "$T/cfg"
	mkdir "$T/enabled"

	# Ask a name for the backup
	h_ask_str \
"You can have unlimited number of backup stategies. Each strategy must have a
unique name that will be used to distinguish it. It is possible to have a
blank strategy name which is fine for most cases and serves as the default." \
		"Enter the backup strategy name" \
		""
	STRATEGY="$RET"
	DESTBASE="$myconfdir/backup.$STRATEGY"
	RCDIR="rc.${STRATEGY}.d"
	RCD="$myconfdir/$RCDIR"
	# Check if it exists
	if [ -e "${DESTBASE}0" ] || [ -e "${DESTBASE}5}" ] ||
		[ -e "$RCD" ] ; then
		# Find a free dirname to move it to
		TNAME="$myconfdir/old"
		CNT=0
		while [ -e "$TNAME" ] ; do
			let CNT+=1
			TNAME="$myconfdir/old$CNT"
		done

		if ! h_ask_yesno \
"A backup strategy with that name already exists. I can move the existing
strategy to $TNAME. You will have to delete it yourself if it is not needed
any more" \
			"Do you want to move the existing strategy out of the way?" \
			"y" ; then
			h_msg 0 \
"
Re-run this wizard to start over
"

			# Remove tmp dir
			rm -rf "$T"

			exit 1
		fi

		if ! mkdir "$TNAME" ; then
			h_msg 2 "Failed to create directory $TNAME"
			rm -rf "$T"
			exit 1
		fi

		mv "${DESTBASE}0" "$TNAME"
		mv "${DESTBASE}5" "$TNAME"
		mv "$RCD" "$TNAME"
	fi

	# Run the wizards
	while read -r fn ; do
		(
		. "$fn"

		CFG="$T/cfg/$PRI-$NAME"

		ASK_NEXT="ENABLE"
		while ! [ -z "$ASK_NEXT" ] ; do
			w_do_ask "$ASK_NEXT"
			R="$?"
		done

		# Prepend an explanatory line
		(echo "# Auto-created - See sample.$NAME for information"
		 test -f "$CFG" && cat "$CFG" ) > "$CFG.new"
		mv -f "$CFG.new" "$CFG"

		# Keep the state of enabled methods
		# Remove this config file if not needed
		if [ "$R" = "0" ] ; then
			touch "$T/enabled/$NAME"
		else
			rm -f "$CFG"
		fi
		) <&9
	done < <( $GFIND . -maxdepth 1 \( -type f -or -type l \) -name '*-*' |\
			sort)

	# Get a temporary file
	T2=`mktemp -p /tmp/`

	# Create configuration
	while read -r fn ; do
		(
		MULTI=""

		. "$fn"

		if ! test -f "$T/enabled/$NAME" ; then
			exit
		fi

		CFG="$T/cfg/$PRI-$NAME"

		# Empty the file
		: > $T2

		# Get config
		w_get_config > $T2

		if ! [ -s "$T2" ] ; then
			rm -f "$T2"
			continue
		fi

		# Create the rc.d entry
		DCFG="$T/final/rc.d"
		test -d "$DCFG" || mkdir -p "$DCFG"

		TNAME="$PRI-$NAME.$NAME"
		cat $T2 > "$DCFG/$TNAME"
		) <&9
	done < <( $GFIND . -maxdepth 1 \( -type f -or -type l \) -name '*-*' |\
			sort)

	# Create vbackup.conf
	get_vbackup_conf "$T2"
	DCFG="$T/final/rc.d"
	cp "$T2" "$DCFG/vbackup.conf"

	# Move configuration to final directory
	DCFG="$T/final/rc.d"
	mv "$DCFG" "$RCD"

	if ! -z "$STRATEGY" ; then
		STRATEGY="$STRATEGY "
	fi

	h_ask_txt \
"Done! You can now create level 0 backups by running:

# vbackup ${STRATEGY}0

and level 5 backups by running:

# vbackup ${STRATEGY}5

Level 5 backups are incremental backups. This means that they only store
differences from the last level 0 backup. This is only possible for filesystem
backups. For all other cases where an incremental backup is not possible (like
database backups) a full backup will be made. To restore a level 5 backup you
also have to have the level 0 backup. This means that you may only delete
older same-level backups.

All configuration options are placed under $myconfdir
If you want to further customize the backup procedure you will have to have
a look at the documentation. All configuration options are documented at
plugin help screens and in the sample configuration files. You can get a list
of available plugins by running:

# vbackup --list

You can get further information about each plugin by running

# vbackup --help <plugin name>

After using vbackup feel free to send feedback to v13@v13.gr
Since this is work-in-progress, this will greatly affect the way vbackup
is developed and what new features will be available in the near future." | more
}

case "$1" in
	--help)
		do_help
		;;
	-*)
		do_help
		;;
	*)
		do_run "$1"
		;;
esac


