sha256sum -c haydn-delivercore-agent-1.0.0.sha256
if [ $? -eq 0 ]; then
    echo "Integrity check succeed!"
else
    echo "Integrity check failed!"
    exit 0
fi

GETOPT_ARGS=`getopt -o k: -al key: -- "$@"`
eval set -- "$GETOPT_ARGS"
while [ -n "$1" ]
do
   case "$1" in
      -k|--key) test -z $2 || key=$2; shift 2;;
      --) break;;
      *) echo $1 , $2; shift 1;;
   esac
done

find_pid=`netstat -atpn | grep LISTEN | grep -E '8809' | grep '/java' | awk '{print $7}' | awk -F '/' '{print $1}'`
if test ! -z $find_pid; then
    echo "old process id: " $find_pid
    kill -9 $find_pid
    echo "old process stopped"
fi

echo "start serivce..."
if [ ! -z "$key" ];then
  echo "set key"
  nohup java -jar haydn-delivercore-agent-1.0.0.jar --encrypt.key=$key >./haydn-delivercore-agent.log 2>&1 &
else
  nohup java -jar haydn-delivercore-agent-1.0.0.jar >./haydn-delivercore-agent.log 2>&1 &
fi
sleep 1
tail -f ./haydn-delivercore-agent.log

for _ in 1 2 3 4
do
    if test ! -z $find_pid; then
        echo "new process id:" $find_pid
        echo "service OK"
	exit 1
    fi
done