#!/bin/sh

CURRENT_VERSION=`rpm -q --queryformat "[%{VERSION}]" sipxconfig`

function update {
  echo "Updating from version $CURRENT_VERSION to version 18.08"

  if [ `mongo --quiet --eval "db.isMaster().ismaster"` == true ] && [ `mongo --quiet --eval "db.version()"` == 2.6.7 ]
  then
    echo -e "$(tput setaf 3)$(tput bold)Upgrading primary node$(tput sgr 0)\n"
    echo "$(tput setaf 3)Step 1: exporting mongodb replica set configuration from 2.6.7 version$(tput sgr 0)"
    mongo --quiet --eval "var cfg=rs.config();cfg.version=1;printjson(cfg)" > /var/sipxdata/tmp/config.json
    echo "$(tput setaf 3)Step 2: exporting mongodb user profiles database from 2.6.7 version$(tput sgr 0)"
    mongodump --db=profiles --out=/var/sipxdata/tmp
    echo "$(tput setaf 3)Step 3: exporting registrations and subscriptions database from 2.6.7 version$(tput sgr 0)"
    mongodump --db=node --out=/var/sipxdata/tmp

    echo "$(tput setaf 3)Step 4: stopping mongod 2.6.7 instance$(tput sgr 0)"
    service mongod stop
    service mongod-arbiter stop
    kill -9 `cat /var/run/mongodb/mongodb.pid` && rm -rf /var/run/mongodb/mongodb.pid && rm -rf /tmp/mongodb-27017.sock 
    kill -9 `cat /var/run/mongodb/mongodb-arbiter.pid` && rm -rf /var/run/mongodb/mongodb-arbiter.pid && rm -rf /tmp/mongodb-27018.sock 
    rm /etc/mongodb.conf
    rm /etc/mongod-arbiter.conf
    rm -rf /var/lib/mongodb/*
    rm -rf /var/lib/mongodb-arbiter/*

    echo "$(tput setaf 3)Step 5: changing yum repo to 18.08 ...$(tput sgr 0)"
    find /etc/yum.repos.d/ -type f -exec sed -i 's/'$CURRENT_VERSION'/18.08/g' {} +  

    echo "$(tput setaf 3)Step 6: upgrading to 18.08 ...$(tput sgr 0)"
    yum clean all && yum update -y

    echo "$(tput setaf 3)Step 7: rebooting machine ...$(tput sgr 0)"
    reboot
  else
    echo -e "$(tput setaf 3)$(tput bold)Upgrading secondary node$(tput sgr 0)\n"
    echo "$(tput setaf 3)Step 1: changing yum repo to 18.08 ... $(tput sgr 0)"
    find /etc/yum.repos.d/ -type f -exec sed -i 's/'$CURRENT_VERSION'/18.08/g' {} +

    echo "$(tput setaf 3)Step 2: upgrading to 18.08 ... $(tput sgr 0)"
    yum clean all && yum update -y

    echo "$(tput setaf 3)Step 3: rebooting machine ...$(tput sgr 0)"
    reboot

  fi
}


if [ $CURRENT_VERSION == "17.10" ]
then
	echo "

	$(tput setaf 1)$(tput bold)sipXcom 18.08 Upgrade Script

	IMPORTANT: If this is a multi-server cluster, all databases except the Primary (which must be on the configuration server) should be removed.
	IMPORTANT: You should run a system backup and copy your config and voicemail backups to another system. If the upgrade fails, you will need to build a new server and restore from backup.
	IMPORTANT: Ensure that you have enough disk space available for a copy of the Mongo databases. (roughly your Config backup).
	$(tput sgr 0)
	$(tput setaf 3)
	This script will do the following:
	- Back up mongo config and dbs
	- Stop mongo instance and remove old mongo files
	- Change 17.10 (and prior) occurrences in /etc/yum.repos.d to 18.08
	- Perform yum update and then reboot the machine

	On sipxconfig service startup following steps are taken (in case there is a backup still on the disk):
	- Restore mongo config and dbs, then remove from disk
	- Reboot machine

	For other cluster servers:
	- Run the same script
	- Re-add databases that were removed.
	$(tput sgr 0)
	"
else
	echo "

	$(tput setaf 1)$(tput bold)sipXcom 18.08 Upgrade Script

	IMPORTANT: You should run a system backup and copy your config and voicemail backups to another system.
	$(tput sgr 0)
	$(tput setaf 3)
	This script will do the following:
	- Change $CURRENT_VERSION occurrences in /etc/yum.repos.d to 18.08
	- Perform yum update
	- Reboot machine

	For other cluster servers:
	- Run the same script
	$(tput sgr 0)
	"
fi

read -p "Continue? (you must enter "Yes" or "No" as shown and press Enter):" choice
case "$choice" in 
  Yes ) update;;
  No ) echo "Update canceled";;
  * ) echo "Invalid option";;
esac

