#!/bin/bash
set -o nounset
set -o errexit
set -e

# ======================================================================
# SCRIPT TO ENCRYPT PROPERTY VALUES TO BE USED IN WARATEK.PROPERTIES
# Do not edit this file.
# To use, run the script with the plaintext property as an argument.
# The encrypted property will be printed to the screen and needs to
# manually updated in waratek.properties
# ======================================================================

if [ ! $# -eq 1 ]; then
    echo "$# arguments supplied. 1 arguments are required!"
    echo "Usage: $0 \"plaintext property\""
    exit 1
fi

readonly SCRIPT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

readonly ENCRYPTION_LIBRARY="jasypt-1.9.3.jar"
readonly ENCRYPTION_CLASS="org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI"
readonly ENCRYPTION_ALGORITHM="PBEWITHMD5ANDDES"
readonly ENCRYPTION_SEED="p%9y#k&yFm?"

readonly command=(java -cp ${SCRIPT_DIR}/${ENCRYPTION_LIBRARY} ${ENCRYPTION_CLASS} \
    input=$1 password=${ENCRYPTION_SEED} algorithm=${ENCRYPTION_ALGORITHM} verbose=false)
readonly property=$("${command[@]}")

echo "The property was successfully encrypted."
echo "The full output below can be used as a replacement property in waratek.properties"
echo
echo "ENC(${property})"
echo

exit 0
