#!/bin/bash
# SPDX-FileCopyrightText: © 2012-2014 Germar Reitze
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of the program "Back In Time" which is released under GNU
# General Public License v2 (GPLv2). See LICENSES directory or go to
# <https://spdx.org/licenses/GPL-2.0-or-later.html>.

# Script should return 0 if everything is alright. Returncode !0 will cancel
# the running backup (BIT version >1.1.0).

email="foo@bar"
profile_id="$1"
profile_name="$2"
reason="$3"
errorcode="$4"

function send_mail {
    subject="Backintime $profile_name: $1"
    shift
    echo -e "$(date) \n$@" | mail -s "$subject" $email
}

function log_id {
    if [ $profile_id -gt 1 ]; then
        echo $profile_id
    else
        echo ""
    fi
}

case $reason in
    1) send_mail "Backup process begins"
         ;;
    2) send_mail "Backup process ends" "$(cat ~/.local/share/backintime/takesnapshot_$(log_id).log)"
         ;;
    3) send_mail "A new backup was created"
         ;;
    4) #There was an error
        case $errorcode in
            1) send_mail "ERROR" "The application is not configured"
                  ;;
            2) send_mail "ERROR" "A 'create backup' process is already running"
                  ;;
            3) send_mail "ERROR" "Can't find backups directory (is it on a removable drive ?)"
                  ;;
            4) send_mail "ERROR" "A backup for 'now' already exist"
                  ;;
            5) send_mail "ERROR" "Error while creating a backup"
               ;;
            6) send_mail "ERROR" "New backup created but with errors"
               ;;
            *) send_mail "ERROR" "Unknown error number"
               ;;
        esac
        ;;
esac
