#!/bin/bash

pv_volume=50Gi
pv_path="/data/es"
backup_volume=50Gi
backup_path="/data/esbackup"
pv_yaml="es-pv.yaml"
backup_pv_yaml="backup-es-pv.yaml"

echo "==================================================="
echo "$pv_volume"
echo "$pv_path"
echo "$backup_volume"
echo "$backup_path"
echo "==================================================="

old_pv_volume=`cat $pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`
old_pv_path=`cat $pv_yaml | grep "path:" | uniq -c | awk -F " " '{print $3}' | awk -F "0" '{print $1}' | uniq -c | awk -F " " '{print $2}'`

echo "###################################################"
echo "$old_pv_volume"
echo "$old_pv_path"

new_pv_path=${pv_path//\//\\\/}
old_pv_path=${old_pv_path//\//\\\/}

sed -i "s/$old_pv_volume/$pv_volume/g" $pv_yaml
sed -i "s/$old_pv_path/$new_pv_path/g" $pv_yaml

old_backup_pv_volume=`cat $backup_pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`
old_backup_pv_path=`cat $backup_pv_yaml | grep "path:" | uniq -c | awk -F " " '{print $3}' | awk -F "0" '{print $1}' | uniq -c | awk -F " " '{print $2}'`
new_backup_path=${backup_path//\//\\\/}
old_backup_pv_path=${old_backup_pv_path//\//\\\/}

echo "###################################################"
echo "$old_backup_pv_volume"
echo "$old_backup_pv_path"

sed -i "s/$old_backup_pv_volume/$backup_volume/g" $backup_pv_yaml
sed -i "s/$old_backup_pv_path/$new_backup_path/g" $backup_pv_yaml

echo "###################################################"
echo "###################################################"
cat $pv_yaml
cat $backup_pv_yaml
