#!/bin/bash

SCRIPT_DIR="$(temp=$( realpath "$0"  ) && dirname "$temp")"

KEYSTORE_PATH="$SCRIPT_DIR/keystore.jks"
PROPERTIES_PATH="$SCRIPT_DIR/publish_key.properties"

echo Keystore path : $KEYSTORE_PATH
echo Properties path : $PROPERTIES_PATH

[ ! -n "$JKS_KEYSTORE" ] && echo 'Missing JKS_KEYSTORE variable!'&& exit 1
[ ! -n "$JKS_KEYSTORE_PASSWORD" ] && echo 'Missing JKS_KEYSTORE_PASSWORD variable!' && exit 1

# Write keystore
echo $JKS_KEYSTORE | base64 -d > "$KEYSTORE_PATH"

# Write keystore config
cat > "$PROPERTIES_PATH" <<_EOF
storePassword=$JKS_KEYSTORE_PASSWORD
keyPassword=$JKS_KEYSTORE_PASSWORD
keyAlias=moneymgr
storeFile=$KEYSTORE_PATH
_EOF