#!/bin/bash

DATE=$(date); echo "$DATE:executing manager upgrade script" > logs/MANAGER-UPGRADER-SCRIPT.log

UPGRADE_ME=$(cat upgrade.me)
echo "$UPGRADE_ME:fail" > upgrade.me

DATE=$(date);echo "$DATE:stopping motadata service" >> logs/MANAGER-UPGRADER-SCRIPT.log

service motadata stop
DATE=$(date);echo "$DATE:motadata service stopped" >> logs/MANAGER-UPGRADER-SCRIPT.log

PID=`ps -ef | grep -v grep | grep "motadata-manager"| awk '{print $2}'`

if [[ "" !=  "$PID" ]]; then
  DATE=$(date);echo "$DATE:motadata service is still alive, killing it forcefully" >> logs/MANAGER-UPGRADER-SCRIPT.log
  kill -9 $PID
fi

DATE=$(date);echo "$DATE:taking backup" >> logs/MANAGER-UPGRADER-SCRIPT.log
rm -rf backup/manager-artifacts/*
mkdir -p backup/manager-artifacts
cp -r lib motadata-manager backup/manager-artifacts/

mkdir -p /motadata/tmp
if grep -q "java.io.tmpdir" /motadata/motadata/motadata-agent.config; then
    echo "java.io.tmpdir already present in script"
else
    echo "java.io.tmpdir does not exist......So adding it"
	sed -i  "/option -d64/a\option -Djava.io.tmpdir=\/motadata\/tmp" /motadata/motadata/motadata-agent.config
fi
if grep -q "java.io.tmpdir" /motadata/motadata/motadata-manager.config; then
    echo "java.io.tmpdir already present in script"
else
    echo "java.io.tmpdir does not exist......So adding it"
	sed -i  "/option -d64/a\option -Djava.io.tmpdir=\/motadata\/tmp" /motadata/motadata/motadata-manager.config
fi

motadata_service_file () {
cat <<EOFG | tee $1
[Unit]
Description=Motadata Agent Service
After=network.target

[Service]
Environment="PATH=/motadata/python-embedded/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/motadata/go/bin" "PYTHONPATH=/motadata/motadata" "GOROOT=/motadata/go" "GOPATH=/motadata/gopath" "GO111MODULE=off" "GOCACHE=/root/.cache/go-build" "GOTMPDIR=/motadata/tmp"
WorkingDirectory=/motadata/motadata
ExecStart=/motadata/motadata/motadata-manager
#ExecStop=/motadata/motadata/shutdown.sh
KillMode=mixed
LimitCORE=infinity
LimitNOFILE=500000
TimeoutSec=360
#Restart=always
#RestartSec=5

[Install]
WantedBy=multi-user.target
EOFG

}
if grep -q "Ubuntu" /etc/os-release; then
    echo "The system is running Ubuntu. updating service file on ubuntu"
    motadata_service_file /lib/systemd/system/motadata.service

else
    echo "The system is not ubuntu"
        if [[ $(awk -F= '$1=="ID" { print $2 ;}' /etc/os-release | tr -d '"') == sles ]]; then
        	echo "Found a Suse enterprice linux !"
		motadata_service_file /usr/lib/systemd/system/motadata.service
        else
        	echo "No Suse linux, it is RHEl/centos"
		motadata_service_file /lib/systemd/system/motadata.service
        fi
fi
systemctl daemon-reload

DATE=$(date);echo "$DATE:updating manager artifacts" >> logs/MANAGER-UPGRADER-SCRIPT.log
rm -rf motadata-manager
cp downloads/manager-artifacts/linux/motadata-manager .

if [ -d "downloads/manager-artifacts/lib" ]; then
  	DATE=$(date);echo "$DATE:updating lib" >> logs/MANAGER-UPGRADER-SCRIPT.log
	  rm -rf lib ; cp -r downloads/manager-artifacts/lib .
fi

cp downloads/manager-artifacts/traceroute /usr/bin/
cp downloads/manager-artifacts/trip /usr/bin/
cp -r downloads/manager-artifacts/javaplugindir /motadata/motadata/

DATE=$(date);echo "$DATE:starting motadata service" >> logs/MANAGER-UPGRADER-SCRIPT.log
service motadata start

sleep 5

if (( $(ps -ef | grep -v grep | grep "motadata-manager" | wc -l) >= 1 ))
then
	DATE=$(date);echo "$DATE:motadata service started successfully" >> logs/MANAGER-UPGRADER-SCRIPT.log

	echo "$UPGRADE_ME:succeed" > upgrade.me
else
	DATE=$(date);echo "$DATE:motadata manager upgrade failed" >> logs/MANAGER-UPGRADER-SCRIPT.log
	service motadata stop

	ps -ef | grep -v grep | grep "motadata-manager"| awk '{print $2}' | xargs kill -9

	DATE=$(date);echo "$DATE:restoring manager artifacts" >> logs/MANAGER-UPGRADER-SCRIPT.log
	rm -rf motadata-manager lib
	cp -r backup/manager-artifacts/lib .
  cp backup/manager-artifacts/motadata-manager .

  DATE=$(date);echo "$DATE:starting motadata service" >> logs/MANAGER-UPGRADER-SCRIPT.log
	service motadata start
fi

