#! /bin/bash

set -e

if [ -z $1 ]; then
   echo "$0 backup.tar.gz"
   exit 1
fi

zip=$(realpath "$1")

mkdir -p /tmp/restore
cd /tmp/restore
unzip "$zip"

if [ "$(find /data -maxdepth 1 -name "*.flf" | wc -l)" == "1" ] ; then
    echo "skip license files"
    rm -f data/*.flf
    rm -f data/*.fle
fi

cp -r data/* /data


if [ ! -z $2 ] ; then
    echo "restore network settings mode is $2"
    omgsetnetwork $2
elif [ "$(find /data -maxdepth 1 -name "networksettingsbackup.*" | wc -l)" == "1" ] ; then
    echo "restore network settings"
    omgsetnetwork --restore
else
    echo "merge network settings"
    omgsetnetwork --merge
fi

cd - 2>/dev/null >/dev/null

rm -rf /tmp/restore
