#!/usr/bin/env bash
#
# Move secrets.
#

# Load functions
LIB="`dirname $0`/../functions"
source "$LIB" maintenance $* || exit 1

# Avoid leading slash
ORIG="$(keyringer_filename `echo "$2" | sed -e "s|^/*||"`)"
DEST="`echo "$3" | sed -e "s|^/*||"`"

# Set destination
if [ ! -d "$KEYDIR/$RELATIVE_PATH/$DEST" ]; then
  keyringer_get_new_file $DEST
else
  FILE="$DEST"
fi

# Check if secret exists
if ! echo "$ORIG" | grep -q '*' && [ ! -e "$KEYDIR/$RELATIVE_PATH/$ORIG" ]; then
  echo "Secret not found: $ORIG"
  exit 1
fi

# Ensure destination path exists
mkdir -p "`dirname $KEYDIR/$RELATIVE_PATH/$DEST`"

# Run move command
keyringer_exec git "$BASEDIR" mv "keys/$RELATIVE_PATH/$ORIG" "keys/$FILE"
