#!/usr/bin/env bash

PROPERTY_NAME=$1

# Useful directories
SCRIPTDIR=$(dirname $0)
HOME_DIR=$(cd "${SCRIPTDIR}/.."; pwd -P)

# Useful files
PROPERTIES_FILE=$HOME_DIR/etc/custom.system.properties

# Check file exists where it is supposed to
if [ ! -f $PROPERTIES_FILE ]; then
    echo Cannot find file $PROPERTIES_FILE >&2
    exit 1
fi

# Extract the value of a property from a Java properties file
# This function does not handle multi-line properties
# If the property does not exist, print  error message to stderr
value=$(grep "^$PROPERTY_NAME\s*=\s*" "$PROPERTIES_FILE" | cut -d = -f2)

if [ ! -z "$value" ]; then
    echo $value
else
    echo "ERROR: $PROPERTY_NAME is not a property of $PROPERTIES_FILE" >&2
    exit 2
fi