#!/bin/bash

USER_NAME=Park

ITEM_NAME=Park_AccountService

SERVICE_PORT=8017

Control_IP=118.31.14.62

SERVICE_DIR="/opt/app/$ITEM_NAME"

CODE_DIR=/opt/code/$ITEM_NAME

VERSION=`cat /opt/code/$ITEM_NAME/version`

CODE_ZIP=/opt/code/$ITEM_NAME/$ITEM_NAME-$VERSION

SERVICE_SCRIPTS=/opt/scripts/$ITEM_NAME/microservice.sh

LOCKFILE=/opt/scripts/$ITEM_NAME/tmp/$ITEM_NAME



#source /etc/profile


update() {
    
    CODE_ZIP=/opt/code/$ITEM_NAME/$ITEM_NAME-$VERSION
     
    if [ "`ls $CODE_ZIP | wc -l`" -gt "0" ];then 
          rm -rf $SERVICE_DIR/*
          cp -r $CODE_ZIP/* $SERVICE_DIR/
	  echo "update frome $CODE_DIR code to $SERVICE_DIR"
    else
       exit 6
    fi

}

process() {
     
    [ -d $SERVICE_DIR ] || exit 6
    cd $SERVICE_DIR 
    #nohup java -DServerName=$ITEM_NAME -classpath core/*:lib/* com.xlhy.saas.cloud.yuwan.Application >/dev/null 2>&1 &
    nohup java -Xms1024M -Xmx1024M -Xmn400M -DServerName=Park_AccountService -Dspring.profiles.active=prod -classpath  core/*:lib/* com.xlhy.saas.cloud.yuwan.Application > /dev/null 2>&1 &
    retval=$?
    if [ $retval -eq 0 ];then
        echo -n $"Starting $ITEM_NAME $SERVICE_PORT: OK"
        touch $LOCKFILE
        echo
    fi
    return $retval
}

rsynca() {

    rsync -a --delete $Control_IP::$ITEM_NAME/$ITEM_NAME-$VERSION $CODE_DIR
    ls -l $CODE_DIR/$ITEM_NAME-$VERSION
}

check() {

   curl http://`hostname -i`:$SERVICE_PORT/info && echo \

   }

start() {
    
    id -u $USER_NAME >/dev/null 2>&1 || useradd $USER_NAME
    ## set user XLServer worker directory file permitted
    chown -R $USER_NAME:$USER_NAME /opt
    chmod -R 744  /opt
    #chown -R $USER_NAME /data
    #chown -R $USER_NAME /logs
    #chown -R $USER_NAME /usr/log
    #chown -R $USER_NAME /webroot
    ## use $USER_NAME user to start server
    su - $USER_NAME -c "$SERVICE_SCRIPTS process"

}

stop() {
    pid=`ps -ef|grep DServerName=$ITEM_NAME|grep -v grep|awk '{print $2}'`
 
    if [ -n "$pid" ];then
 
       kill -3 ${pid}
       kill ${pid} && sleep 3
    
       if [  -n "`ps -eo pid | grep $pid`" ];then
          kill -9 ${pid}
       fi
       rm -rf $LOCKFILE
       echo "kill pid: ${pid}"
       echo $"Stop $ITEM_NAME $SERVICE_PORT:Is ok!"
    fi
    return 0;
}



status() {

    ps -ef|grep DServerName=$ITEM_NAME|grep -v grep >/dev/null 2>&1
    retval=$?
    if [ $retval -eq 0 ];then
      echo "server is running ..."
      
    else
      echo "server stoped ..."
    fi
    return $retval
}

status_q() {
    status >/dev/null 2>&1
}

case "$1" in
    start)
        status_q && exit 0
        $1
        exit 0
        ;;
    stop)
        status_q || exit 0
        $1
        exit 0
        ;;
    status)
        status
        ;;
    process)
        status_q && exit 0
        $1
        exit 0
        ;;
    update)
        status_q && exit 0
        $1
        ;;
    rsynca)
        $1
        ;;
   check)
        status_q || exit 0
        $1
        exit 0
        ;;
    
    *)
        echo $"Usage: $0 {start|stop|status|process|rsynca|check}"
        exit 2
esac
