#!/bin/sh

set -eu

cmd="$(basename "$0")"
base="$(dirname "$(readlink -f "$0")")/.."

cd "${base}"
mkdir -p tmp

case "$cmd" in
  backup.sh)
    tarball=tmp/backup-$(date +%s).tar.gz
    tar -caf "${tarball}" config data
    ;;
  restore.sh)
    tarball="$(find tmp/ -name backup-\*.tar.gz | sort | tail -1)"
    if [ -z "${tarball}" ] || [ ! -f "${tarball}" ]; then
      echo "E: no backup to restore"
      exit 1
    fi
    rm -rf config data
    tar -xaf "${tarball}"
    ;;
esac
