#!/bin/bash

ulimit -n 65535

##supervisor
if command -v supervisord &>/dev/null; then
	echo "supervisor 已安装: $(supervisord --version)"
else
	echo "supervisor 自动安装"
	yum install -y epel-release
	yum install -y supervisor
	systemctl enable supervisord
	cp -rf ./supervisor/supervisord.conf /etc/
	systemctl start supervisord
fi

if [ ! -d /etc/supervisord.d ]; then
	mkdir /etc/supervisord.d/
fi

cp -rf ./supervisor/super-node.ini /etc/supervisord.d/

##
ulimit -n 65535

##
current=$(date +"%Y%m%d%H%M%S")

if [ ! -d /sce ]; then
	mkdir /sce
fi

if [ ! -d /sce/super-node ]; then
	mkdir /sce/super-node
fi

if [ ! -d /sce/logs ]; then
	mkdir /sce/logs
fi

##bak
echo "bak current version to /home/sce.bak"
if [ ! -d /home/sce.bak ]; then
	mkdir /home/sce.bak
fi

if [ ! -d /home/sce.bak/super-node ]; then
	mkdir /home/sce.bak/super-node
fi

if [ ! -d /home/sce.bak/super-node/$current ]; then
	mkdir /home/sce.bak/super-node/$current
fi

cp -rf /sce/super-node /home/sce.bak/super-node/$current/

##install
if [ $# -eq 1 ]
then
	echo "upgrade super-node begin ........................"
	rm -rf /sce/super-node/super-node
	cp -rf ./super-node/super-node /sce/super-node/
else
	echo "install super-node begin ........................"
	rm -rf /sce/super-node
	cp -rf ./super-node /sce/
fi

##restart
supervisorctl update
supervisorctl restart super-node

if [ $# -eq 1 ]
then
	echo "super-node upgrade finished!!!"
else
	echo "super-node install finished!!!"
fi

