#!/bin/bash

# wait a bit
sleep 30s

data_dir="/var/lib/univention-appcenter/apps/rocketchat/data"
conf_dir="/var/lib/univention-appcenter/apps/rocketchat/conf"

if ! [[ -f "${conf_dir%/}/backupfile" ]]; then
	echo "Not an upgrade, skipping"
	exit 0
fi

dump_file="$(cat "${conf_dir%/}/backupfile")"

if ! [[ -f "$dump_file" ]]; then
	echo "No backup file found, it was expected" >&2
	exit 1
fi

echo "Restoring from $dump_file"

mongorestore --uri=mongodb://localhost:27017/rocketchat --gzip --archive --drop <"$dump_file"

rm "$dump_file" "${conf_dir%/}/backupfile"