#!/bin/bash
#
# Copyright (C) 2013 Anders Logg and Martin Sandve Alnaes
#
# This file is part of FIAT.
#
# FIAT is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FIAT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FIAT. If not, see <http://www.gnu.org/licenses/>.
#
# First added:  2013-04-22
# Last changed: 2013-08-21
#
# This script overwrites the reference data with the current output
# and stores the new reference data as part of the FIAT reference data
# repository.

# Parameters
source ./scripts/parameters

# Get reference repository
if [ ! -d "$DATA_DIR" ]; then
    echo "Cloning reference data repository"
    if [ -n "$DATA_REPO_GIT" ]; then
        git clone $DATA_REPO_GIT
    fi
    if [ ! -d "$DATA_DIR" ]; then
        git clone $DATA_REPO_HTTPS
    fi
else
    pushd $DATA_DIR
    echo "Found existing reference data repository, pulling new data"
    git checkout master
    if [ $? -ne 0 ]; then
        echo "Failed to checkout master, check state of reference data directory."
        exit 1
    fi
    git fetch
    if [ $? -ne 0 ]; then
        echo "WARNING: Failed to fetch latest reference data from server."
    else
        git pull
        if [ $? -ne 0 ]; then
            echo "Failed to pull latest reference data from server, possibly a merge situation."
            exit 1
        fi
    fi
    popd
fi

# Check that we had success with getting reference repository
if [ ! -d "$DATA_DIR" ]; then
    echo "Failed to update reference data directory '$DATA_DIR'."
    exit 1
fi
