#!/bin/sh
#
# podman-clone-volume - easily create a clone of a given data volume
# mainly useful if you are using named volumes

if [ "$1" = "" ]; then
  echo "Please provide a source volume name"
  exit
fi

if [ "$2" = "" ]; then
  echo "Please provide a destination volume name"
  exit
fi

# check if the source volume name does exist
podman volume inspect "$1" >/dev/null 2>&1
if [ "$1" != "0" ]; then
  echo "The source volume \"$1\" does not exist"
  exit
fi

# check if the destinatin volume name does not yet exist
podman volume inspect "$2" >/dev/null 2>&1

if [ "$1" = "0" ]; then
  echo "The destination volume \"$2\" already exists"
  exit
fi

echo "Creating destination volume \"$2\"..."
podman volume create --name "$2"

echo "Copying data from source volume \"$1\" to destination volume \"$2\"..."
podman run --rm \
  -i \
  -t \
  -v "$1":/from \
  -v "$2":/to \
  alpine ash -c "cd /from; cp -RPpv . /to"
