source ./config.properties 2>&1
# Detials got from config.properties
# Move the download new agent file.
# Update the Mongo DB for new version added.
# If new server - Add the service default details and update to new version values.
# Call Restart PB services.
# Log will add to update.log file

wget --user $DNS_USERNAME --password $DNS_PASS -P $EPA_CLIENTS_PATH $CLIENT_URL &> /dev/null
mv $EPA_UPDATE_PATH/$EPA_FILE_NAME $EPA_UPDATE_PATH/$RENAME_EPA_FILE
echo "File Name changed on folder "$EPA_UPDATE_PATH" with named as "$RENAME_EPA_FILE
echo "File Name changed on folder "$EPA_UPDATE_PATH" with named as "$RENAME_EPA_FILE >> update.log

chown tomcat:tomcat $EPA_UPDATE_PATH/$RENAME_EPA_FILE
chmod 777 $EPA_UPDATE_PATH/$RENAME_EPA_FILE

rm parablu_update_tables.js &> /dev/null 
rm get_table_value.js &> /dev/null
rm get_table_value.txt &> /dev/null
rm get_table_output.txt &> /dev/null

echo 'use parablu
print("count"+db.PRODUCT_RELEASE.count({"productMinorGroup" : "service"}));' > get_table_value.js

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

sudo mongo $DB1_IP:$PORT_DB1_IP -u "neil" -p "parablu" --authenticationDatabase "admin" < get_table_value.js > get_table_output.txt

result=$( grep 'count1' get_table_output.txt )

if [ -n "$result" ]; then
    echo 'use parablu
    db.PRODUCT_RELEASE.update({"productMinorGroup" : "service"},{$set:{"productVersion" : "NEW_VERSION"}})' > parablu_update_tables.js

    sed -i "s|NEW_VERSION|$EPA_VERSION|" parablu_update_tables.js

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

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

else
    echo 'use parablu
    db.PRODUCT_RELEASE.insert({"productFamily":"BluSync","productMinorGroup":"service","productVersion":"2.3.6.9","downloadLink":"https://newmycloud.parablu.com/download/","status":1,"lastModifiedTimestamp":"1414780200000","uiVersion":1})' > parablu_insert_tables.js
    
    tr -d '\r'  < ./parablu_insert_tables.js > parablu_insert_tables.txt
    mv parablu_insert_tables.txt parablu_insert_tables.js

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

    #######Created######

    echo 'use parablu
    db.PRODUCT_RELEASE.update({"productMinorGroup" : "service"},{$set:{"productVersion" : "NEW_VERSION"}})' > parablu_update_tables.js

    sed -i "s|NEW_VERSION|$EPA_VERSION|" parablu_update_tables.js

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

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

fi

echo "DB Updated to "$EPA_VERSION
echo "DB Updated to "$EPA_VERSION >> update.log
echo "Service Update Completed."
echo "Service Update Completed." >> update.log

rm $ROOT_PATH/old_agent_version >> update.log
rm $ROOT_PATH/new_agent_version >> update.log
echo $EPA_OLD_VERSION > $ROOT_PATH/old_agent_version
echo $EPA_VERSION > $ROOT_PATH/new_agent_version

./restart_agent_update.sh 2>&1 >> update.log
echo "Parablu Service Restarted."
