#!/bin/bash

newfile=$1
oldfile=$2

if [ -z "$newfile" -o -z "$oldfile" ]; then
	echo "usage: $0 [newfile] [oldfile]"
	exit 1
fi

if [ ! -e "$newfile" ]; then
	echo "error: [newfile] isn't exist!"
	exit 1
fi

if [ ! -e "$oldfile" ]; then
	echo "error: [oldfile] isn't exist!"
	exit 1
fi

execName=`basename $oldfile`

if [ "$execName" = "role" -o "$execName" = "nasips" ]; then
	pid=`pgrep restart_service`
	[ ! -z "$pid" ] && kill -9 $pid
	killall -9 $execName
	cp $newfile $oldfile -a
	sync
	rm -f $newfile
	(/sbin/restart_service >/dev/null &)
elif [ "$execName" = "tnas.link.key" -o "$execName" = "tnas.link.crt" -o "$execName" = "cn.tnas.link.key" -o "$execName" = "cn.tnas.link.crt" ]; then
	cp $newfile $oldfile -a
	sync
	rm -f $newfile
	(service nginx reload >/dev/null &)
else
	killall -9 $execName
	cp $newfile $oldfile -a
	sync
	rm -f $newfile
fi

exit 0
