#!/bin/sh

alterator_api_version=1

. alterator-sh-functions

from_scm()
{
	sed -r 's,#t,YES,;s,#f,NO,i'
}

to_string()
{
	sed -r "s,YES,`_ "yes"`,;s,NO,`_ "no"`,;s,DEFAULT,`_ "default"`,"
}

DEBUG=0

r_param()
{
    [ -z "$1" ] && return 1
    local system method
    system="${in_name%/*}"
    method="${in_name##*/}"
    # FIXME: check for non root users with another HOME
    #write_debug "\n--$HOME--\n"
    HOME=/root /usr/share/synbak-utils/read-param.sh "$system" "$method" "$1"
    return 0
}

w_param()
{
    [ -z "$1" ] && return 1
    [ -z "$2" ] && return 1
    local system method
    system="${in_name%/*}"
    method="${in_name##*/}"
    # FIXME: check for non root users with another HOME
    HOME=/root /usr/share/synbak-utils/write-param.sh "$system" "$method" "$1" "$2"
    return 0
}

get_backup_destination()
{
    local system method
    system="${in_name%/*}"
    method="${in_name##*/}"
    local backup_destination="$(r_param backup_destination)"
    # Replace $system and $method
    eval "backup_destination=$backup_destination"
    echo "$backup_destination"
}

read_shedule_params()
{
    local period
    local system method
    system="${in_name%/*}"
    method="${in_name##*/}"
    for period in hourly daily weekly monthly; do
	[ -x /etc/cron."$period"/synbak ] && \
	    grep -qs "^synbak -s $system -m $method" /etc/cron.$period/synbak && \
	    write_bool_param enable_shedule "yes" && \
	    write_string_param backup_period "$period" && \
	    return 0
    done
    write_bool_param enable_shedule "no"
    write_string_param backup_period "weekly"
}

remove_backup_from_all_periods()
{
    local tempfile
    local system method
    system="${in_name%/*}"
    method="${in_name##*/}"
    for period in hourly daily weekly monthly; do
	if [ -x /etc/cron."$period"/synbak ]; then
	    if grep -qs "^synbak -s $system -m $method" /etc/cron.$period/synbak; then
		# Remove backup from this sheduler
		tempfile=`mktemp`
		grep -v -s "^synbak -s $system -m $method" /etc/cron.$period/synbak > "$tempfile" && \
		cat "$tempfile" > /etc/cron.$period/synbak
		rm -f "$tempfile"
	    fi
	fi
    done
}

on_message()
{
	write_debug "\n$(set | grep '^in_')\n"
	case "$in_action" in
		read)
		    case "$in__objects" in
			config)
			    local name method
			    name="${in_name%/*}"
			    method="${in_name##*/}"
			    write_string_param name "$name"
			    write_string_param method "$method"
			    write_string_param backup_source "$(r_param backup_source)"
			    write_string_param backup_destination "$(r_param backup_destination)"
			    write_string_param backup_exclude "$(r_param backup_exclude)"
			    write_string_param backup_erase_after "$(r_param backup_erase_after)"
			    write_bool_param backup_incremental "$(r_param backup_incremental)"
			    write_bool_param report_email "$(r_param report_email)"
			    write_bool_param report_email_on_errors "$(r_param report_email_on_errors)"
			    write_string_param report_email_rcpt "$(r_param report_email_rcpt)"
			    read_shedule_params
			    ;;
		    esac
		    ;;
		list)
		    case "$in__objects" in
			backup-periods)
			    write_enum_item "hourly"  "`_ "hourly"`"
			    write_enum_item "daily"  "`_ "daily"`"
			    write_enum_item "weekly"  "`_ "weekly"`"
			    write_enum_item "monthly"  "`_ "monthly"`"
			    ;;
			config-list)
			    /usr/share/synbak-utils/list-methods.sh | while read method; do
				for config in `ls ~/.synbak/"$method"/*.conf`; do
				    system="${config##*/}"
				    system="${system%.conf}"
				    write_table_item \
					system_name "$system/$method" \
					name "$system/$method"
				done
			    done
			    ;;
			method-list)
			    /usr/share/synbak-utils/list-methods.sh | write_enum
			    ;;
			backup-list)
			    local backup_destination="$(get_backup_destination)"
			    [ -d "$backup_destination" ] && ls "$backup_destination" | while read i; do
				write_table_item backup_name "$i"
			    done
			    ;;
			restore-backup-list)
			    local backup_destination="$(get_backup_destination)"
			    local method="${in_name##*/}"
			    case "$method" in
				tar)
				    [ -d "$backup_destination" ] && ls "$backup_destination" | sort -r | write_enum
				    ;;
				rsync)
				    [ -d "$backup_destination" ] && ls "$backup_destination" | write_enum
				    ;;
			    esac
			    ;;
		    esac
		    ;;
		delete_backup)
		    local IFS=';'
		    local method="${in_name##*/}"
		    local backup_destination="$(get_backup_destination)"
		    for s in $in_backup_name; do
			case "$method" in
			    tar)
				rm -f "$backup_destination/$s"
				;;
			    rsync)
				rm -rf "$backup_destination/$s"
				;;
			esac
		    done
		    ;;
		do_backup)
		    local system method
		    system="${in_name%/*}"
		    method="${in_name##*/}"
		    # FIXME: check for non root users with another HOME
		    HOME=/root /usr/share/synbak-utils/do-backup.sh "$system" "$method" &> /dev/null
		    ;;
		do_restore)
		    local system method
		    system="${in_name%/*}"
		    method="${in_name##*/}"
		    local backup_destination="$(get_backup_destination)"
		    [ -n "$in_backup_name" ] && \
			# FIXME: check for non root users with another HOME
			HOME=/root /usr/share/synbak-utils/do-restore.sh "$system" "$method" "$backup_destination/$in_backup_name" &> /dev/null
		    ;;
		configure)
		    w_param backup_source "$in_backup_source"
		    w_param backup_destination "$in_backup_destination"
		    w_param backup_exclude "$in_backup_exclude"
		    w_param backup_erase_after "$in_backup_erase_after"
		    if test_bool "$in_backup_incremental"; then
			w_param backup_incremental yes
		    else
			w_param backup_incremental no
		    fi
		    if test_bool "$in_report_email"; then
			w_param report_email yes
		    else
			w_param report_email no
		    fi
		    if test_bool "$in_report_email_on_errors"; then
			w_param report_email_on_errors yes
		    else
			w_param report_email_on_errors no
		    fi
		    w_param report_email_rcpt "$in_report_email_rcpt"
		    ;;
		delete)
		    local IFS=';'
		    local system method
		    for s in $in_system_name; do
			system="${s%/*}"
			method="${s##*/}"
			[ -f ~/.synbak/"$method"/"$system".conf ] && \
			    rm -f ~/.synbak/"$method"/"$system".conf
		    done
		    ;;
		process_shedule)
		    local system method
		    system="${in_name%/*}"
		    method="${in_name##*/}"
		    # We don't know about previous backup_period so we remove this backup from all periods (shedulers)
		    # Also: one backup should be in one sheduler
		    remove_backup_from_all_periods
		    # Prepare script if not exists or not executable
		    if [ ! -x /etc/cron.$in_backup_period/synbak ]; then
			echo -e "#!/bin/sh\n" > /etc/cron.$in_backup_period/synbak
			chmod 700 /etc/cron.$in_backup_period/synbak
		    fi
		    if test_bool "$in_enable_shedule"; then
			[ "$method" = "tar" ] && method="tar -M bz2"
			echo "synbak -s $system -m $method" >> /etc/cron.$in_backup_period/synbak
		    fi
		    ;;
		new)
		    if [ -z "$in_system_name" ]; then
			write_error "`_ "Empty system name disallowed"`"
		    else
			if [ -f ~/.synbak/"$in_backup_method"/"$in_system_name".conf ]; then
			    write_error "`_ "Configuration"` '$in_system_name/$in_backup_method' `_ "already exists"`"
			else
			    # FIXME: check for non root users with another HOME
			    HOME=/root /usr/share/synbak-utils/create-config.sh "$in_system_name" "$in_backup_method"
			fi
		    fi
		    ;;
	esac
}

message_loop
