#!/usr/bin/env bash
CWD="${0%/*}"
VAR=$CWD/../var
SNAPSHOTS=$CWD/../snapshots

otherargs=()
while [[ "$#" > 0 ]]; do case $1 in
  -f|--force) force=1;;
  *) otherargs+=($1);;
esac; shift; done

name=${otherargs[${#otherargs[@]} - 1]}

if [ -z "$name" ]; then
  echo Pass the name of the snapshot as the last argument.
  exit 1
fi

if [ -e $SNAPSHOTS/$name ]; then
  if [ "$force" ]; then
    rm -rf $SNAPSHOTS/$name.tgz
  else
    echo A snapshot with that name already exists. Please remove it, use a different name, or run this script with --force.
    exit 1
  fi
fi

tar czf $SNAPSHOTS/$name.tgz -C $VAR/chaindata .
