#!/bin/bash
source ./config.properties 2>&1
# Detials got from config.properties
# Shell script created to connect with mongo and get the Old version name

rm get_version_find.js &> /dev/null
rm get_version_find.txt &> /dev/null
rm get_version_find_output.txt &> /dev/null

echo 'use parablu;
db.PRODUCT_RELEASE.find({"productMinorGroup" : "service"}).forEach(function(re){print("out " +re.productVersion)});' > get_version_find.js

tr -d '\r'  < ./get_version_find.js > get_version_find.txt
mv get_version_find.txt get_version_find.js

sudo mongo $DB1_IP:$PORT_DB1_IP -u "neil" -p "parablu" --authenticationDatabase "admin" < get_version_find.js > get_version_find_output.txt #&> /dev/null

old_version_mongo=$(grep '^out.*$' get_version_find_output.txt | cut --complement -d " " -f 1)
sed -i "s|EPA_OLD_VERSION.*|EPA_OLD_VERSION='"$old_version_mongo"' |g" ./config.properties

sleep 5

./getNewVersion.sh
