#!/bin/bash
set -e

if [ "$#" -ne 2 ]; then
    echo "Illegal number of parameters";
    echo "Usage: configure-custom-cert '/path/to/current/keystore' '/path/to/new/keystore'"
    exit 1
fi

ks_current="$1"
echo "Current keystore location - $ks_current"

ks_current_bkp="$ks_current.bkp"
echo "Keystore backup location - $ks_current_bkp"

ks_custom="$2"
echo "Custom keystore tmp location - $ks_custom"

# create the backup
/usr/bin/mv -f "$ks_current" "$ks_current_bkp"
echo "Successfully created the backup file!"

# move the new/custom ks to the location of the current one
/usr/bin/mv -f "$ks_custom" "$ks_current"
echo "Successfully move replaced the keystore file!"

# change the ownership & permissions of the custom cert
/usr/bin/chown --reference="$ks_current_bkp" "$ks_current"
/usr/bin/chmod --reference="$ks_current_bkp" "$ks_current"
echo "Successfully changed the ownership!"