
#!/bin/bash
set -x

echo "$(date)  get env APP_NAME: $APP_NAME";
if [ -n $APP_NAME ];
then
  echo "port $HTTP_PORT"
  echo "start do $APP_NAME";
  echo "list dir /vhosts/$APP_NAME";
  ls /vhosts/$APP_NAME
  #rm -fr /pod_vhosts/$APP_NAME--*;
  rm -fr /pod_vhosts/$APP_NAME--$IMAGE_TAG;
  mkdir -p /pod_vhosts/$APP_NAME--$IMAGE_TAG;
#  rm -fr /pod_vhosts/$APP_NAME;
#  if [ ! -d "/vhosts/$APP_NAME/" ];then
#    ehco "/vhosts/$APP_NAME/ is empty"
#    ln -s /app/empty /pod_vhosts/$APP_NAME;
#  else
#    ehco "/vhosts/$APP_NAME/ exists"
#    ln -s /vhosts/$APP_NAME--$IMAGE_TAG /pod_vhosts/$APP_NAME;
#  fi

  echo "list dir and create soft link after delete";
  ls /vhosts/$APP_NAME
  echo "remove spring tomcat file";
  cp -fr /vhosts/$APP_NAME/GIT_COMMIT_ID.txt /vhosts/$APP_NAME/WEB-INF/classes/VERSION.txt;

  #judge whether contain spring.cloud.config.override-none
  dataline=$(cat /vhosts/$APP_NAME/WEB-INF/classes/config/bootstrap.yml)
  result=$(echo $dataline | grep "override-none")
  if [[ "$result" != "" ]]
  then
      echo "contain spring.cloud.config.override-none"
  else
      echo "not contain add spring.cloud.config.override-none"
      echo -e "\n\nspring.cloud.config.override-none: \${config.allow-override:false}"  >> /vhosts/$APP_NAME/WEB-INF/classes/config/bootstrap.yml;
  fi;
  #judge whether contain port
  port_str=$(echo $dataline | grep "port:")
  if [[ "$port_str" != "" ]]
  then
      echo "contain port"
  else
      echo "not contain add port"
      echo -e "\n\nserver:\n  port: $HTTP_PORT"  >> /vhosts/$APP_NAME/WEB-INF/classes/config/bootstrap.yml;
  fi;

  rm -f /vhosts/$APP_NAME/WEB-INF/classes/application*;
  rm -f /vhosts/$APP_NAME/WEB-INF/classes/config/application*;
  rm -f /vhosts/$APP_NAME/WEB-INF/lib/tomcat*;
  rm -f /vhosts/$APP_NAME/WEB-INF/lib/spring-boot-starter-tomcat*;

  #cover jar file
#  rm -f /vhosts/$APP_NAME/WEB-INF/lib/microservice-spring-boot-starter*;
#  cp -fr /pod_vhosts/microservice-spring-boot-starter-2.0.7.58.RELEASE.jar /vhosts/$APP_NAME/WEB-INF/lib/

  rm -fr /pod_vhosts/$APP_NAME--$IMAGE_TAG;
  echo "$(date)  copying file"
  `cp -fr /vhosts/$APP_NAME/ /pod_vhosts/$APP_NAME--$IMAGE_TAG/`
  echo "$(date)  list dir and create soft link";
  ls /vhosts/$APP_NAME

  rm -fr /pod_vhosts/$APP_NAME;
  if [ ! -d "/vhosts/$APP_NAME/" ];then
    echo "/vhosts/$APP_NAME/ is empty"
    ln -s /app/empty /pod_vhosts/$APP_NAME;
  else
    echo "/vhosts/$APP_NAME/ exists"
    ln -s /vhosts/$APP_NAME--$IMAGE_TAG /pod_vhosts/$APP_NAME;
  fi

  # delete history file
  counter=1
  for i in `ls -t /pod_vhosts/$APP_NAME--* | grep "$APP_NAME*" | awk -F ':' '{print $1}'`
  do
    echo "loop file: $i"
    if [[ "$counter" -gt 4 ]]; then
      echo "delete file: $i"
      rm -fr $i
    fi
    counter=$((counter+1))
  done
#  filelist=$(ls -t /pod_vhosts/$APP_NAME--* | grep "$APP_NAME*" | awk -F ':' '{print $1}');
#  echo "file list: $filelist"
#  file_arr=$(echo $filelist | tr " " "\n")
#  file_arr_size=${#file_arr[*]}
#  echo "file count $file_arr_size"
#  for i in {4..10}
#  do
#    oneFile=$file_arr[$i]
#    if [ -n ${oneFile} ]; then
#      echo "delete file ${oneFile}"
#     # rm -fr ${oneFile}
#    fi
#  done

  echo "$(date)  starting pod";
  if [ "true" = "true" ];
  then
    echo "$(date)  TOMCAT_RESTART $TOMCAT_RESTART"
    curl -XPATCH --insecure -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" "https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}/apis/apps/v1/namespaces/$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)/deployments/${TOMCAT_APP_NAME}-deployment?fieldManager=kubectl-rollout&pretty=true"  \
    --header 'Content-Type: application/strategic-merge-patch+json' \
    -d '{
        "spec": {
            "template": {
                "metadata": {
                    "annotations": {
                        "kubectl.kubernetes.io/restartedAt": "'$(date +'%Y-%m-%dT%H:%M:%S+08:00')'"
                    }
                }
            }
        }
    }'
  else
    echo "$(date)  TOMCAT_RESTART $TOMCAT_RESTART"
  fi
else
    echo "$(date)  env APP_NAME String is empty"
fi

#echo "get tomcat svc address";
#my_str=`nslookup $TOMCAT_APP_SVC |grep Address | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"`;
#echo "parse svc address: $my_str";
#if test -z $my_str;
#then
#  echo "not found Address"
#else
#  echo $my_str;
#  my_arr=($(echo $my_str | tr "Address:" "\n"));
#  for i in "${my_arr[@]}";
#  do
#    echo "======================================="$i"=======================================";
#    curl -v -s "http://"$i":8080/app.jsp?prj="$APP_NAME"&cmd=restart";
#  done
#fi
echo "end";
echo $?;
#while true;do echo running...;sleep 1000;done;
