#!/bin/bash
#
# recover.sh
# $Id: krecover-oss.sh,v 1.2 2006/08/30 01:09:21 lrupp Exp $
#

CONFIG=/etc/sysconfig/schoolserver
LOGFILE=/var/log/oss-recover.log

function check_mount() {
	mount | grep -q "$1" || return 1 
}

function usage() {
	kdialog --msgbox "Usage: `basename $0`\n\n Please select the recover targets in the dialog and press OK"
}

if [ ! -f $CONFIG ]; then
	kdialog --sorry "Could not find: ".$CONFIG
	exit 1
fi

. $CONFIG


if [ "$SCHOOL_BACKUP_CHECK_MOUNT" = "yes" ]; then
	if ! check_mount $SCHOOL_BACKUP_FULL_DIR; then
		kdialog --title "OSS Recover" --sorry $SCHOOL_BACKUP_FULL_DIR." is not mounted!"
	fi

	if ! check_mount $SCHOOL_BACKUP_INC_DIR; then
		kdialog --title "OSS Recover" --sorry $SCHOOL_BACKUP_INC_DIR." is not mounted!"
	fi
fi

LDAP=""
HOMEDIRS=""
EMAILS=""
PROXY=""
SAMBA=""
SSL=""
SSH=""
TFK=""
MOODLE="" 
JOOMLA="" 
EGROUPWARE="" 
OX="" 
ETC="no"

LAST_BACKUP=`find $SCHOOL_BACKUP_INC_DIR/ -maxdepth 0 -type d -exec ls -rt {} \; | tail -n1`
BACKUP_DIR=$SCHOOL_BACKUP_INC_DIR/$LAST_BACKUP
# check for restoreable data
if [ -e "$SCHOOL_BACKUP_FULL_DIR/etc" ]; then
	ETC="yes"
fi

if [ -f "$SCHOOL_BACKUP_FULL_DIR/SLAPCAT.gz" ]; then
	LDAP="1 LDAP-Database on"
fi

if [ -d "$SCHOOL_BACKUP_FULL_DIR/home" ]; then
	HOMEDIRS="2 Home-directories on"
fi

if [ -d "$SCHOOL_BACKUP_FULL_DIR/var/spool/imap/" -a "$ETC" = "yes" ]; then
	EMAILS="3 Emails on"
fi

if [ -d "$SCHOOL_BACKUP_FULL_DIR/var/lib/squidGuard/db/custom/" -a "$ETC" = "yes" ]; then
	PROXY="4 Proxy-Data on"
fi

if [ -d "$SCHOOL_BACKUP_FULL_DIR/var/lib/samba/" -a "$ETC" = "yes" ]; then
	SAMBA="5 Samba-Data on"
fi

if [ "$ETC" = "yes" ]; then
	SSL="6 SSL-Certificates on"
fi

if [ -d "$SCHOOL_BACKUP_FULL_DIR/root/" -a "$ETC" = "yes" ]; then
	SSH="7 SSH-Keys on"
fi

if [ -f "$SCHOOL_BACKUP_FULL_DIR/tfk.gz" ]; then
	TFK="8 Time4Kids on"
fi

if [ -f "$SCHOOL_BACKUP_FULL_DIR/moodle.gz" ]; then
	MOODLE="9 Moodle on"
fi

if [ -f "$SCHOOL_BACKUP_FULL_DIR/joomla.gz" ]; then
	JOOMLA="10 Joomla on"
fi

if [ -f "$SCHOOL_BACKUP_FULL_DIR/egroupware.gz" ]; then
	EGROUPWARE="11 eGroupware on"
fi

if [ -f "$SCHOOL_BACKUP_FULL_DIR/openexchange.gz" ]; then
	OX="12 openXchange on"
fi



for ARG in `kdialog --title "OSS Recover" --separate-output --checklist "Daten für Wiederherstellung wählen:" \
	$LDAP \
	$HOMEDIRS \
	$EMAILS \
	$PROXY \
	$SSL \
	$SSH \
	$TFK \
	$MOODLE \
	$JOOMLA \
	$EGROUPWARE \
	$OX`; do
 case $ARG in 
	1)
	 ARGS="$ARGS --ldap"
	;;
	2)
	 ARGS="$ARGS --home"
	;;
	3)
	 ARGS="$ARGS --mail"
	;;
	4)
	 ARGS="$ARGS --proxy"
	;;
	5)
	 ARGS="$ARGS --samba"
	;;
	6)
	 ARGS="$ARGS --ssl"
	;;
	7)
	 ARGS="$ARGS --ssh"
	;;
	8)
	 ARGS="$ARGS --tfk"
	;;
	9)
	 ARGS="$ARGS --moodle"
	;;
	10)
	 ARGS="$ARGS --joomla"
	;;
	11)
	 ARGS="$ARGS --egroupware"
	;;
	12)
	 ARGS="$ARGS --openxchange"
	;;
	*)
	 ARGS=""
	;;
 esac
done

if [ "$ARGS" != "" ]; then
	if `kdialog --title "OSS Recover" --yesno "Ready to start Recovery.\n \
			  Some services will become temporary unavailable and all current data will be lost.\n\n \
			  Are you really shure?"`; then
	if [ -x $SCHOOL_BACKUP_FULL_DIR/oss_recover.sh ]; then
		sh $SCHOOL_BACKUP_FULL_DIR/oss_recover.sh $ARGS
	else
		oss_recover.sh $ARGS
	fi
fi
