#!/bin/bash

readonly progname="${0##*/}"

## First `/` must be removed from paths to ease the use of tar

RESTORE_BLINDLY="etc/asterisk/extensions_extra.d/
                etc/dahdi/
                etc/dhcp/
                etc/network/if-up.d/xivo-routes
                etc/ntp.conf
                etc/resolv.conf
                etc/xivo-xuc.conf
                var/lib/asterisk/agi-bin/
                var/lib/xivo-provd/
                var/lib/xivo/"

PATH_TO_CHECK="etc/crontab
                etc/ldap/
                etc/hostname
                etc/hosts
                etc/network/interfaces
                etc/xivo-agentd/
                etc/xivo-agid/
                etc/xivo-amid/
                etc/xivo-auth/
                etc/xivo-call-logd/
                etc/xivo-confd/
                etc/xivo-confgend-client/
                etc/xivo-ctid/
                etc/xivo-dird/
                etc/xivo-dird-phoned/
                etc/xivo-dxtora/
                etc/xivo-purge-db/
                etc/xivo/
                etc/fail2ban/jail.d/xivo.conf
                etc/fail2ban/filter.d/asterisk-xivo.conf
                etc/fail2ban/filter.d/xivo-provd.conf
                etc/docker/
                usr/local/bin/
                usr/local/sbin/
                var/spool/cron/crontabs/
                var/lib/postgresql/11/main/*.conf
                var/lib/postgresql/11/main/conf.d/*.conf
                etc/asterisk/"

DO_NOT_RESTORE="etc/asterisk/keys
                etc/xivo/default_french_configuration.sql
                usr/local/bin/docker-compose
                etc/xivo/common.conf
                etc/docker/xivo/.env
                etc/docker/xivo/factory.env
                etc/consul/
                etc/profile.d/xivo_uuid.sh
                etc/ssl/
                etc/systemd/
                usr/share/xivo/XIVO-VERSION
                var/lib/consul/
                var/log/asterisk/
                var/spool/asterisk/"

usage() {
    echo "Usage:"
    echo "${progname} BACKUP_FILE"
    echo "This script will restore the backup"
    echo ""
    echo -e "\t BACKUP_FILE the path to the backup file"
    echo -e "\t e.g. /var/backup/xivo/data.tgz"
}

exit_abnormally() {
    usage
    exit 1
}

main() {
    if [ -z "${1}" ]; then
        echo "Missing parameter: you must give the path to data.tgz file"
        echo
        exit_abnormally
    fi

    local data_backup_file="${1}"; shift
    local data_restore_path="/var/tmp/xivo-restore"; shift

    ## Only restore RESTORE_BLINDLY
    # local restore_blindly_tar=`echo $RESTORE_BLINDLY | awk 'BEGIN{RS = "[ ]"}{sub("/","",$1)}{print $1}'`
    echo "Restoring files to be restored blindly"
    tar -zxf "${data_backup_file}" -C "/" ${RESTORE_BLINDLY}
    # Errors(not blocking?) pointing files not found in, archive (e.g. etc/network/if-up.d/xivo-routes)    

    ## Only restore PATH_TO_CHECK
    # local path_to_check_tar=`echo $PATH_TO_CHECK | awk 'BEGIN{RS = "[ ]"}{sub("/","",$1)}{print $1}'`
    mkdir "${data_restore_path}"
    echo "Restoring files to be checked in ${data_restore_path}"
    tar --exclude ${DO_NOT_RESTORE} -zxf "${data_backup_file}" -C "${data_restore_path}" ${PATH_TO_CHECK} --wildcards

    ## Generate fingerprint for files in $PATH_TO_CHECK
    ## Then create a list of changed files
    local fingerprint_for_backup="/var/tmp/fingerprint_for_backup.txt"
    local files_to_be_checked="/var/tmp/files_to_be_checked.txt"
    find "${data_restore_path}" -type f -print0 | xargs -0 sha1sum  > "${fingerprint_for_backup}"
    sed -i "s#${data_restore_path}##" "${fingerprint_for_backup}"
    sha1sum -c "${fingerprint_for_backup}" | grep "FAILED" | awk -F ":" '{print $1}' > "${files_to_be_checked}"
    gawk -i inplace -v path=${data_restore_path} '{print "vimdiff "$1" "path$1}' ${files_to_be_checked}
    echo -e "\e[1;33mYou might want to check differences between old and new files that require attention by reading ${files_to_be_checked}\e[0m"
}

main "${@}"