#!/bin/sh
RETVAL=$?
SHELL_NAME="deploy"
BASEDIR=$(dirname $0)
BASEDIR=$( (
  cd "$BASEDIR"
  pwd
))
LOCK_FILE="/tmp/deploy.lock"
# 时间变量
CDATE=$(date "+%Y-%m-%d")
CTIME=$(date "+%H:%M:%S")
PUBLISH_PATH="@profiles.publish.path@"
PROJECT_NAME="@project.artifactId@"
PORT="@profiles.server.port@"

SHELL_LOG="${BASEDIR}/${SHELL_NAME}.log"

SERVICE_PATH="/usr/lib/systemd/system"
JAVA_HOME="/usr/local/java/jdk1.8"

PROJECT_EXECPATH="${PUBLISH_PATH}/${PROJECT_NAME}"
PROJECT_FILENAME="${PROJECT_NAME}.tar.gz"
PROJECT_SERVICE="${SERVICE_PATH}/${PROJECT_NAME}.service"

#写日志
writelog() {
  LOGINFO=$1
  echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >>${SHELL_LOG}
  echo ${LOGINFO}
}

#清理目标
clear_deploy() {
  SERVICE=$1
  EXECPATH=$2
  #清理后台自启服务
  rm -rf ${SERVICE}
  #清理执行文件目录
  rm -rf ${EXECPATH}
}

build_service() {
  SERVICE=$1
  EXECPATH=$2
  echo "" >${SERVICE}
  echo "[Unit]" >>${SERVICE}
  echo "Description=${PROJECT_NAME}" >>${SERVICE}
  echo "After=network.target" >>${SERVICE}
  echo "" >>${SERVICE}
  echo "[Service]" >>${SERVICE}
  echo "Environment=\"JAVA_HOME=$JAVA_HOME\"" >>${SERVICE}
  echo "Type=forking" >>${SERVICE}
  echo "ExecStart=${EXECPATH}/bin/start.sh" >>${SERVICE}
  echo "ExecStop=${EXECPATH}/bin/shutdown.sh" >>${SERVICE}
  echo "PrivateTmp=true" >>${SERVICE}
  echo "" >>${SERVICE}
  echo "[Install]" >>${SERVICE}
  echo "WantedBy=multi-user.target" >>${SERVICE}
  writelog "${PROJECT_NAME}服务创建完成!"
}

#启动服务
start_service_and_nginx() {
  writelog "${PROJECT_NAME}服务启动..."
  systemctl enable ${PROJECT_NAME}
  systemctl daemon-reload
  systemctl stop ${PROJECT_NAME} && systemctl start ${PROJECT_NAME}
  project_status=$(systemctl status "${PROJECT_NAME}"|grep Active |awk '{print $2}')

}
#部署后台服务
project_deploy() {
  writelog "${PROJECT_NAME}_deploy"
  clear_deploy ${PROJECT_SERVICE} ${PROJECT_EXECPATH}
  writelog "${PROJECT_NAME}_clear_finish"

  tar -zvxf ./${PROJECT_FILENAME} -C ${PUBLISH_PATH}
  build_service ${PROJECT_SERVICE} ${PROJECT_EXECPATH}
  start_service_and_nginx
  writelog "${PROJECT_NAME}_deploy_finish"
}

setup_java() {
  writelog "setup java"
  system_version=$(unix_is_centos)
  if [[ ${system_version} == "centos" ]]; then
    writelog "centos java clear"
   # yum -y remove java-*-openjdk* #卸载centos中的openjdk
    #yum -y remove tzdata-java.noarch
  else
    writelog "ubuntu java clear"
    apt-get purge openjdk*
    apt-get purge icedtea-* openjdk-*
    apt-get -y install libpcap-dev
  fi
  rm -rf ${JAVA_HOME}
  mkdir -p ${JAVA_HOME}
  cpu_info=$(check_cpu)
  echo "${cpu_info}"
  if [[ ${cpu_info} == *${ARM_64}* ]]; then
    tar -zvxf ${BASEDIR}/soft/java/arm_64/${JAVA_ARM_FILENAME} -C ${JAVA_HOME} --strip-components 1
    echo "arm64"
  elif [[ ${cpu_info} == *${X86_64}* ]]; then
    tar -zvxf ${BASEDIR}/soft/java/x86_64/${JAVA_FILENAME} -C ${JAVA_HOME} --strip-components 1
    echo "x86_64"
  else
    tar -zvxf ${BASEDIR}/soft/java/x86_64/${JAVA_FILENAME} -C ${JAVA_HOME} --strip-components 1
    echo "x86_64"
  fi

  echo "export JAVA_HOME=${JAVA_HOME}" >>/etc/profile
  echo "export JRE_HOME=$JAVA_HOME/jre" >>/etc/profile
  echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib" >>/etc/profile
  echo "export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin" >>/etc/profile
  source /etc/profile #使配置立即生效
  #检查Java安装和配置
  cd $BASEDIR
  writelog "setup java finish!"
}


#主函数
main() {
  #setup_java
  writelog "后台服务部署"
  project_deploy
  exit ${RETVAL}
}

main $1
