#!/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 snapshot (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 snapshot was taken"
         ;;
    4) #There was an error
        case $errorcode in
            1) send_mail "ERROR" "The application is not configured"
                  ;;
            2) send_mail "ERROR" "A 'take snapshot' process is already running"
                  ;;
            3) send_mail "ERROR" "Can't find snapshots folder (is it on a removable drive ?)"
                  ;;
            4) send_mail "ERROR" "A snapshot for 'now' already exist"
                  ;;
            5) send_mail "ERROR" "Error while taking a snapshot"
               ;;
            6) send_mail "ERROR" "New snapshot taken but with errors"
               ;;
            *) send_mail "ERROR" "Unknown error number"
               ;;
        esac
        ;;
esac
