#!/bin/sh

OXIBACKUP_OPT_DIR="/opt/oxibackup"

if [ -z "$HOME" ]
then
    HOME="/root"
fi

_echo () {
    echo "$1" > "$SYNOPKG_TEMP_LOGFILE"
}

case "$1" in
    start)
        if ! "$OXIBACKUP_OPT_DIR/bin/oxibackupd-control" enable > /dev/null 2>&1
        then
            _echo "Could not enable Oxibox backup agent"
            exit 1
        fi
        "$OXIBACKUP_OPT_DIR/bin/oxibackupd-control" start > /dev/null 2>&1
    ;;
    stop)
        if ! "$OXIBACKUP_OPT_DIR/bin/oxibackupd-control" disable > /dev/null 2>&1
        then
            _echo "Could not disable Oxibox backup agent"
            exit 1
        fi
        "$OXIBACKUP_OPT_DIR/bin/oxibackupd-control" stop > /dev/null 2>&1
    ;;
    status)
        STATUS_OUTPUT="$("$OXIBACKUP_OPT_DIR/bin/oxibackupd-control" status)"
        if echo "$STATUS_OUTPUT" | grep -q "Running: yes"
        then
            exit 0
        elif echo "$STATUS_OUTPUT" | grep -q "Running: no"
        then
            exit 3
        fi
        exit 4
    ;;
    log)
        # Do nothing
    ;;
esac

exit 0
