#!/bin/bash

# stop on first error
set -e

echo "start testing ... "
echo "TRAVIS_BRANCH: $TRAVIS_BRANCH"
echo "USER: $USER"
echo "DB: $DB"

echo "--------- starting services ----------- "
service bareos-dir start
service bareos-sd start
service bareos-fd start
sleep 10

echo "--------- checking services ----------- "
service bareos-dir status
service bareos-sd status
service bareos-fd status


# enable bash debug
set -v

BACKUP_TEST_FILE=/usr/sbin/bareos.test

echo -e "status dir" | bconsole
echo
echo "---- label a volume ----"
echo -e "label volume=testvol pool=Full" | bconsole
echo
echo "----- create some file to test backup / restore ----"
echo "bareos restore test" > ${BACKUP_TEST_FILE}
echo
echo "------ trigger backup job -----"
echo -e "run job=BackupClient1 yes\rwait" | bconsole | grep "Job queued. JobId="
echo "status dir" | bconsole
echo
echo "------ trigger restore job -----"
echo -e "restore select current\r2\rls\rmark usr\rdone\ryes\rwait" | bconsole
echo "status dir" | bconsole
grep "bareos restore test" /tmp/bareos-restores/${BACKUP_TEST_FILE}
