#!/usr/bin/env bash

. /usr/share/dosis2/bin/shell-support-functions

systemtype="$1"
serial="$2"
customfile="/opt/dosis2/etc/custom/dosisConfig.xml"

dafetype="`/opt/dosis2/etc/personality/common-scripts/system-to-dafe-type \"${systemtype}\"`"
dafetag="`/opt/dosis2/etc/personality/common-scripts/system-to-dafe-tag \"${systemtype}\" \"${serial}\"`"

function validateip() {
    ip="$1"
    for i in `seq 4`; do
        byte=`echo "${ip}" | cut -d. -f${i}`
        if [ ${byte} -gt 255 ]; then
            return 1
        fi
    done
    return 0
}

if [ "${systemtype}" = "site-server" ] || [[ "${systemtype}" =~ ^.*standalone.*$ ]]; then
    siteserverip="127.0.0.1"
else
    while [ true ]; do
        serverip="`/opt/dosis2/etc/personality/common-scripts/ask \
            \"Site Server IP: \" \
            \"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$\"`"

        [ $? = 0 ] && validateip ${serverip} && break

        echo -e "\033[31mSite Server IP must be a valid IP\033[0m"
    done
    siteserverip="`echo ${serverip} | sed -e 's/^\s+//g' -e 's/\s+$//g'`"
fi


############
# DafeType #
############
echo "Updating ${customfile} [DafeType]..."
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DafeType"] > value' \
    --value "${dafetype}" \
    --create \
    --text
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DafeType"] > type' \
    --value "string" \
    --create \
    --text
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DafeType"] > description' \
    --value "Which type of automation equipment (equipment.dafe_types)" \
    --create \
    --text


###########
# DafeTag #
###########
echo "Updating ${customfile} [DafeTag]..."
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DafeTag"] > value' \
    --value "${dafetag}" \
    --create \
    --text
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DafeTag"] > type' \
    --value "string" \
    --create \
    --text
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DafeTag"] > description' \
    --value "Which Machine this is in the cluster" \
    --create \
    --text


############
# DBServer #
############
echo "Updating ${customfile} [DBServer]..."
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DBServer"] > value' \
    --value "${siteserverip}" \
    --create \
    --text
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DBServer"] > type' \
    --value "string" \
    --create \
    --text
/opt/dosis2/bin/xml-select change "${customfile}" --output "${customfile}" \
    --select 'config > section[name="general"] > property[name="DBServer"] > description' \
    --value "Name of the server the database is located" \
    --create \
    --text
